ddrv / http-client
simple HTTP client without cURL dependency
Fund package maintenance!
www.paypal.me/ddrv
v2.0.3
2020-12-14 12:47 UTC
Requires
- php: >=7.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- nyholm/psr7: ^1.3
- php-http/client-integration-tests: ^2.0
- squizlabs/php_codesniffer: ^3.5
Suggests
- psr/http-factory-implementation: Choice factory for your psr-7 objects
- psr/http-message-implementation: Choice your favorite psr-7 implementation
- webclient/ext-cache: Wrapper for caching results
- webclient/ext-cookie: Wrapper for cookies
- webclient/ext-log: Wrapper for logging
- webclient/ext-protocol-version: Wrapper for auto change protocol version
- webclient/helper-form: Easy creating PSR-7 requests
Provides
README
ddrv/http-client
Simple HTTP client without cURL dependency. Required allow_url_fopen
option in php.ini
file.
; php.ini allow_url_fopen = On
Install
Install this package, your favorite psr-7 implementation and your favorite psr-17 implementation.
composer require ddrv/http-client:^2.0
Using
<?php use Ddrv\Http\Client\Client; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseFactoryInterface; /** * @var ResponseFactoryInterface $responseFactory * @var int $timeout = 60 * @var int $redirects = 0 */ $http = new Client($responseFactory, $timeout, $redirects); /** @var RequestInterface $request */ $response = $http->sendRequest($request); $code = $response->getStatusCode(); $phrase = $response->getReasonPhrase(); $headers = $response->getHeaders(); $someHeader = $response->getHeader('Content-Type'); $body = $response->getBody()->__toString();
Configuration
<?php use Ddrv\Http\Client\Client; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\UriInterface; /** * @var ResponseFactoryInterface $responseFactory * @var UriInterface $proxy */ $http = new Client($responseFactory); $http->setFollowRedirects(0); // Set 0 follow redirects (disable). $http->setTimeOut(10); // Set connection timeout 10 seconds $http->setProxy($proxy); // Set proxy $http->setProxy(); // Unset proxy
SSL Authorization
<?php use Ddrv\Http\Client\Client; use Psr\Http\Message\ResponseFactoryInterface; /** * @var ResponseFactoryInterface $responseFactory */ $http = new Client($responseFactory); $http->setSslAuth('host', 'ssl.crt', 'ssl.key'); // without password $http->setSslAuth('host', 'ssl.crt', 'ssl.key', 'p@s$w0rd'); // with password $http->unsetSslAuth('host'); // disable