borsch/http-client

PSR-18 HTTP 客户端实现。

v1.0 2018-11-21 07:32 UTC

This package is auto-updated.

Last update: 2024-09-21 21:45:05 UTC


README

PSR-18 HTTP 客户端的一个简单实现。

此包是 Borsch 框架的一部分。

安装

通过 Composer

$ composer require borsch/http-client 

使用

require 'vendor/autoload.php';

use Borsch\Http\Client;
use Borsch\Http\Request; // PSR-7 Request implementation from any package (see below)

$client = new Client();
$client->setResponseClassName(
    \Borsch\Http\Response::class
);

$request = new Request('GET', 'https://dog.ceo/api/breeds/list/all');
$response = $client->sendRequest($request);

方法

->setResponseClassName(): Client

您必须提供一个 PSR-7 ResponseInterface 的实现。
Borsch\Http\Client 将使用此实现来创建响应实例。

例如,如果您使用的是 Slim PHP PSR-7 实现,则您的代码应如下所示

require 'vendor/autoload.php';

use Borsch\Http\Client;
use Slim\Psr7\Response as SlimPhpResponse;

$client = new Client();
$client->setResponseClassName(SlimPhpResponse::class);

也可以提供一个 PSR-17 ResponseFactoryInterface 的实现,例如使用 Nyholm/psr7

require 'vendor/autoload.php';

use Borsch\Http\Client;
use Nyholm\Psr7\Factory\Psr17Factory;

$client = new Client();
$client->setResponseClassName(Psr17Factory::class);

Borsch\Http\Client 将调用方法 createResponse() 以获取 ResponseInterface 实例。

->getResponseClassName(): string

返回在方法 setResponseClassName() 中提供的 ResponseInterface 或 ResponseFactoryInterface。

->setCurlOption(int $option, $value): Client

Borsch\Http\Client 使用 cURL,因此您可以在发送请求之前提供一些选项。Borsch\Http\Client 已经使用请求的值设置了这些选项,因此您无法覆盖它们

  • CURLOPT_CUSTOMREQUEST
  • CURLOPT_URL
  • CURLOPT_HTTP_VERSION
  • CURLOPT_POSTFIELDS
  • CURLOPT_HTTPHEADER
  • CURLOPT_RETURNTRANSFER
  • CURLOPT_ENCODING
  • CURLOPT_HEADERFUNCTION

示例

require 'vendor/autoload.php';

use Borsch\Http\Client;
use Nyholm\Psr7\Factory\Psr17Factory;

$client = new Client();
$client->setResponseClassName(Psr17Factory::class);

$request = (new Psr17Factory())->createRequest('GET', 'https://dog.ceo/api/breeds/list/all');
$response = $client
    ->setCurlOption(CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem') // will work
    ->setCurlOption(CURLOPT_CUSTOMREQUEST, 'POST') // will not work as already used by Borsch\Http\Client
    ->sendRequest($request);

->setCurlOptions(array $options): Client

使用提供的数组一次性设置多个 cURL 选项。
必须为以下形式

$options = [
    CURLOPT_CAINFO => dirname(__FILE__).'/cacert.pem',
    // etc.
]

->getCurlOptions(): array

获取您已设置的 cURL 选项。

许可协议

MIT License

Copyright (c) 2018 Alexandre DEBUSSCHERE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.