moccalotto/hayttp

使用流畅的 API 简化 HTTP API 请求

09.11 2017-07-03 12:01 UTC

README

Build Status

轻松实现 HTTP 请求!

  • 轻量级,快速,占用空间小。
  • 语法优美,易于使用且直观。
  • 简化 7 个 RESTful HTTP 方法。
  • 支持真实文件(和 blob)上传。
  • 基本认证。
  • 不可变。
  • 惊人的高级选项
    • 在 CURL 和 php 原生 HTTP 流之间进行选择。
    • 创建自己的 http 传输引擎(例如 guzzle 包装器)。
    • 选择 ssl/tls 方案和版本。
    • 创建自定义有效载荷。

安装

要将此包添加到您的项目中的本地依赖项,只需将 moccalotto/hayttp 添加到您的项目 composer.json 文件中的依赖项即可,如下所示:

{
    "require": {
        "moccalotto/hayttp": "~1.0"
    }
}

或者,在您的 shell 中执行以下命令。

composer require moccalotto/hayttp

用法

use Hayttp\Hayttp;

$response = Hayttp::get($url)->send();

REST 方法

Hayttp 实际上是一个工厂,可以创建和初始化 Request 对象。它为 7 个 RESTful HTTP 方法中的每一个都提供了方法。

执行 GET 请求

$response = Hayttp::get($url)->send();

一个更有趣的 POST 示例。

$response = Hayttp::post($url)
    ->acceptJson()
    ->sendJson([
        'this' => 'associative',
        'array' => 'will',
        'be' => 'converted',
        'to' => 'a',
        'json' => 'object',
    ]);

一个接受 XML 响应体的 DELETE 请求。

$response = Hayttp::delete($url)
    ->acceptXml()
    ->send();

解码响应

您可以将响应有效载荷解析/反序列化为原生 PHP 数据结构。Hayttp 目前支持 json、xml 和 rfc3986。

以下是一个将响应解析为 json 的示例。Json 对象转换为 stdClass 对象,json 数组转换为 php 数组。

$stdClass = Hayttp::get($url)
    ->acceptJson()
    ->send()
    ->jsonDecoded();

以下是一个将响应解码为 SimpleXmlElement 的示例。

$simpleXmlElement = Hayttp::get($url)
    ->acceptXml()
    ->send()
    ->xmlDecoded();

将 URL 编码的字符串解码为关联数组

$array = Hayttp::get($url)
    ->send()
    ->urlDecoded();

解码响应,从 Content-Type 标头推断数据类型

$variable = Hayttp::get($url)->send()->decoded();

辅助函数

您可以使用全局 hayttp 方法访问默认的 hayttp 实例。

$body = hayttp()->withTimeout(10)
    ->post($url)
    ->ensureJson()
    ->sendJson(['foo' => 'bar',])
    ->decded();

您还可以使用 hayttp_* 方法进行即时请求。

// All the calls below are equivalent

$response = hayttp_get($url);

$response = Hayttp::get($url)
                ->ensure2xx()
                ->send();

$response = hayttp()->get($url)
                ->ensure2xx()
                ->send();

以下是一些如何使用 hayttp_* 方法的示例

// All the calls below are equivalent
$xml = new SimpleXmlElement('<root><groot>Boot</groot></root>');

$response = hayttp_post($url, $xml);

$response = Hayttp::post($url)
                ->ensure2xx()
                ->sendXml($xml);

$response = hayttp()->post($url)
                ->ensure2xx()
                ->sendXml($xml);

发布 json

// All the calls below are equivalent
$data = ['foo' => ['bar' => ['baz']]];

$response = hayttp_post($url, $data);

$response = Hayttp::post($url)
                ->ensure2xx()
                ->sendJson($data);

$response = hayttp()->post($url)
                ->ensure2xx()
                ->sendJson($data);

上传原始文本

// All the calls below are equivalent
$raw = file_get_contents($path);

$response = hayttp_put($url, $raw);

$response = Hayttp::put($url)
                ->ensure2xx()
                ->sendRaw($raw, 'application/octet-stream');

$response = hayttp()->put($url)
                ->ensure2xx()
                ->sendRaw($raw, 'application/octet-stream');