PHP code example of psr-discovery / http-factory-implementations

1. Go to this page and download the library: Download psr-discovery/http-factory-implementations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

psr-discovery / http-factory-implementations example snippets


use PsrDiscovery\Discover;

// Returns a PSR-17 RequestFactoryInterface instance
$requestFactory = Discover::httpRequestFactory();

// Returns a PSR-17 ResponseFactoryInterface instance
$responseFactory = Discover::httpResponseFactory();

// Returns a PSR-17 StreamFactoryInterface instance
$streamFactory = Discover::httpStreamFactory();

// Returns a PSR-17 UploadedFileFactoryInterface instance
$uploadedFileFactory = Discover::httpUploadedFileFactory();

// Returns a PSR-17 UriFactoryInterface instance
$uriFactory = Discover::httpUriFactory();

// Returns a PSR-7 RequestInterface instance
$request = $requestFactory->createRequest('GET', 'https://example.com');

use PsrDiscovery\Discover;

$requestFactory = Discover::httpRequestFactory();

if ($requestFactory === null) {
    // No suitable HTTP RequestFactory implementation was discovered.
    // Fall back to a default implementation.
    $requestFactory = new DefaultRequestFactory();
}

use PsrDiscovery\Discover;

// $httpResponseFactory1 !== $httpResponseFactory2 (default)
$httpResponseFactory1 = Discover::httpResponseFactory();
$httpResponseFactory2 = Discover::httpResponseFactory();

// $httpResponseFactory1 === $httpResponseFactory2
$httpResponseFactory1 = Discover::httpResponseFactory(singleton: true);
$httpResponseFactory2 = Discover::httpResponseFactory(singleton: true);

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr17\RequestFactories;

// Prefer the a specific implementation of PSR-17 over others.
RequestFactories::prefer('nyholm/psr7');

// Return an instance of Nyholm\Psr7\Factory\Psr17Factory,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$factory = Discover::httpRequestFactory();

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr17\ResponseFactories;

// Only discover a specific implementation of PSR-17.
ResponseFactories::use('nyholm/psr7');

// Return an instance of Nyholm\Psr7\Factory\Psr17Factory,
// or null if it is not available.
$factory = Discover::httpResponseFactory();