PHP code example of http-interop / http-factory-discovery
1. Go to this page and download the library: Download http-interop/http-factory-discovery 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/ */
http-interop / http-factory-discovery example snippets
use Http\Factory\Discovery\HttpClient;
/** @var \Psr\Http\Client\ClientInterface */
$client = HttpClient::client();
namespace Acme\Middleware;
use Http\Factory\Discovery\HttpFactory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as Handler;
class CatchErrors extends MiddlewareInterface
{
/** @var ResponseFactoryInterface */
private $responseFactory;
/** @var StreamFactoryInterface */
private $streamFactory;
public function __construct(
ResponseFactoryInterface $responseFactory = null,
StreamFactoryInterface $streamFactory = null
) {
$this->responseFactory = $responseFactory ?? HttpFactory::responseFactory();
$this->streamFactory = $streamFactory ?? HttpFactory::streamFactory();
}
public function process(Request $request, Handler $handler): Response
{
try {
return $handler->handle($request);
} catch (\Throwable $error) {
$stream = $this->streamFactory->createStream($e->getMessage());
$response = $this->responseFactory->createResponse(500);
$response = $response->withHeader('content-type', 'text/plain');
$response = $response->withBody($stream);
return $response;
}
}
}
namespace Acme;
use Http\Factory\Discovery\HttpClient;
use Http\Factory\Discovery\HttpFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
class Api
{
/** @var ClientInterface */
private $client;
/** @var RequestFactoryInterface */
private $requestFactory;
public function __construct(
ClientInterface $client = null,
RequestFactoryInterface $requestFactory = null
) {
$this->client = $client ?? HttpClient::client();
$this->requestFactory = $requestFactory ?? HttpFactory::requestFactory();
}
public function query(): string
{
$request = $this->requestFactory->createRequest('GET', 'http://acme.com/api');
return $this->client->sendRequest($request)->getBody()->getContents();
}
}
use Acme\RequestFactory;
use Http\Factory\Discovery\FactoryLocator;
use Psr\Http\Message\RequestFactoryInterface;
FactoryLocator::register(RequestFactoryInterface::class, RequestFactory::class);
use Acme\Client;
use Http\Factory\Discovery\ClientLocator;
use Psr\Http\Client\ClientInterface;
ClientLocator::register(ClientInterface::class, Client::class);
use Http\Factory\Discovery\FactoryLocator;
use Http\Factory\Guzzle\UriFactory;
use Psr\Http\Message\UriFactoryInterface;
FactoryLocator::unregister(UriFactoryInterface::class, UriFactory::class);
use Http\Factory\Discovery\ClientLocator;
use Http\Adapter\Guzzle6\Client;
use Psr\Http\Client\ClientInterface;
ClientLocator::unregister(ClientInterface::class, Client::class);
use Http\Factory\Discovery\HttpFactory;
use Psr\Http\Message\UriFactoryInterface;
// Clear a single interface
HttpFactory::clearCache(UriFactoryInterface::class);
// Clear all interfaces
HttpFactory::clearCache();
use Http\Factory\Discovery\HttpClient;
use Psr\Http\Client\ClientInterface;
// Clear a single interface
HttpClient::clearCache(ClientInterface::class);
// Clear all interfaces
HttpClient::clearCache();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.