ddrv / http-client
无cURL依赖的简单HTTP客户端
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
无cURL依赖的简单HTTP客户端。需要在php.ini
文件中启用allow_url_fopen
选项。
; php.ini allow_url_fopen = On
安装
安装此包,你喜欢的psr-7实现和你喜欢的psr-17实现。
composer require ddrv/http-client:^2.0
使用
<?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();
配置
<?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授权
<?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