rokde/http-client

此软件包已被放弃,不再维护。未建议替代软件包。

一个无任何依赖的 http 客户端库。仅使用纯 PHP。

1.1.0 2019-03-14 16:18 UTC

This package is auto-updated.

Last update: 2024-01-15 03:34:15 UTC


README

Latest Stable Version Latest Unstable Version License Total Downloads

此 http 客户端库不依赖 curl 或 guzzle 等软件。它使用纯 PHP 函数处理最常用的 http 请求。

安装

composer require rokde/http-client

使用

您有两个选项

  1. 使用辅助函数 http()
  2. 使用由该软件包提供的完整类堆栈

我建议使用 http() 函数,以保持软件包的目的:简洁。

使用 http()

// GET (get from uri)
/** @var string $response */
$response = http('https://httpbin.org/get');

// POST (post data to uri)
/** @var \Rokde\HttpClient\Response $response */
$response = http()->post(['data' => 'value'], 'https://httpbin.org/post');

使用类版本

$client = new \Rokde\HttpClient\Client();

$request = new \Rokde\HttpClient\Request('https://httpbin.org/get', 'GET', [
  'accept' => 'application/json',
  'x-verify-test' => 'true',
]);

$response = $client->send($request);

if ($response->isOk()) {
  $resultString = $response->content();
}

更多使用示例可以在该软件包的 /tests 文件夹中找到。

测试

使用以下命令运行测试

composer test