mekras/psr7-client

此包已被弃用且不再维护。作者建议使用php-http/curl-client包代替。

PSR-7 兼容的 HTTP 客户端库

维护者

详细信息

github.com/mekras/psr7-client

此包尚未发布版本,信息较少。


README

PSR-7 兼容的 HTTP 客户端库。

Latest Stable Version License Build Status Coverage Status

基于 cURL 的简单 PSR-7 兼容 HTTP 客户端库。

注意!

此包将被php-http/curl-client 替代。

迁移到 Httplug

  1. php-http/httplug 添加到项目需求中。
  2. 将构造函数中的参数类型提示中的 Mekras\Http\Client\CurlHttpClientMekras\Interfaces\Http\Client\HttpClientInterface 替换为 Http\Client\HttpClient
  3. 将方法调用中的 send() 替换为 sendRequest

Mekras\Http\Client\CurlHttpClient 支持使用 Mekras\Interfaces\Http\Client\HttpClientInterfaceHttp\Client\HttpClient 接口,因此迁移可以逐步进行。

在弃用最后一个 Mekras\Interfaces\Http\Client\HttpClientInterface 使用后

  1. 将任一 php-http/client-implementation 添加到项目中并进行配置。
  2. Mekras\Http\Client\CurlHttpClient 的实例替换为所选的 php-http/client-implementation 的实例。
  3. 从您的 composer.json 中删除 mekras/psr7-client 的需求。

支持的库

使用方法

use GuzzleHttp\Psr7\Request;
use Mekras\Http\Client\Connector\GuzzleConnector;
use Mekras\Http\Client\CurlHttpClient;

$client = new CurlHttpClient(new GuzzleConnector());
$request = new Request('GET', 'http://example.org/');
$response = $client->send($request);
echo $response->getBody()->getContents());

选项

可以通过构造函数的第二个参数设置选项。可用的选项包括:

  • connection_timeout (int) — 连接超时时间(秒);
  • curl_options (array) — 自定义 cURL 选项;
  • decode_content (bool) — 查看 CURLOPT_ENCODING;
  • follow_redirects (bool) — 自动遵循 HTTP 重定向;
  • max_redirects (int) — 要遵循的最大嵌套重定向数;
  • ssl_verify_peer (bool) — 使用 SSL 时验证对等方
  • timeout (int) — 总超时时间(秒)。
  • use_cookies (bool) — 保存和发送 cookie;
use Mekras\Http\Client\Connector\GuzzleConnector;
use Mekras\Http\Client\CurlHttpClient;

$client = new CurlHttpClient(
    new GuzzleConnector(),
    [
        'timeout' => 60,
        'curl_options' => [
            CURLOPT_CAPATH => '/path/to/ca'
        ]
    ]
);