codezero/courier

此包已被放弃,不再维护。没有建议的替代包。

简单的HTTP请求接口,可选缓存

2.1.1 2015-02-27 19:40 UTC

This package is auto-updated.

Last update: 2023-06-11 11:43:58 UTC


README

GitHub release License Build Status Scrutinizer Total Downloads

此包提供了一组易于使用的函数,用于在PHP中发送HTTP请求。

功能

  • 易于使用的GET、POST、PUT、PATCH和DELETE函数(见用法
  • 请求时发送可选的数据和头信息
  • 请求时使用可选的基本认证
  • 可选地缓存GET和POST响应
  • Laravel 5 ServiceProvider已包含!

安装

通过Composer安装此包

"require": {
	"codezero/courier": "2.*"
}

实现

手册

目前只有一个Courier实现:CurlCourier,它使用codezero-be/curl和PHP cURL扩展。

use CodeZero\Courier\CurlCourier;

$courier = new CurlCourier();

Laravel 5

安装后,更新您的config/app.php文件,在providers数组中包含对包的service provider的引用

'providers' => [
    'CodeZero\Courier\CourierServiceProvider'
]

注入Courier

将Courier实例注入到您自己的类中

use CodeZero\Courier\Courier; //=> The interface

class MyClass {

    private $courier;

    public function __construct(Courier $courier)
    {
        $this->courier = $courier;
    }
}

Laravel将自动找到Courier类,如果您已注册service provider。或者,您可以手动注入实例:$myClass = new MyClass($courier);

发送请求

配置请求

$url = 'http://my.site/api';
$data = ['do' => 'something', 'with' => 'this']; //=> Optional
$headers = ['Some Header' => 'Some Value']; //=> Optional

启用缓存

可选的缓存请求/响应的分钟数。在过期之前,Courier不会实际击中请求的URL,而是返回缓存的响应!如果您使用Laravel,则使用Laravel的Cache。如果不使用Laravel,则使用phpFastCache

$cacheMinutes = 30; //=> Default = 0 (no caching)

处理异常

如果目标服务器发送HTTP错误响应 >= 400,将抛出CodeZero\Courier\Exceptions\HttpException

您的类也可以实现CodeZero\Courier\HttpExceptionHandler接口并添加一个handleHttpException方法。如果您将您的类引用传递给Courier,则不会抛出异常。相反,HttpException将被传递到您的handleHttpException方法,因此您可以选择抛出自己的异常或返回任何值给原始调用者。

$handler = $this; //=> A reference to your class (optional)

发送请求:(以下任一项)

$response = $this->courier->get($url, $data, $headers, $cacheMinutes, $handler);
$response = $this->courier->post($url, $data, $headers, $cacheMinutes, $handler);
$response = $this->courier->put($url, $data, $headers, $handler);
$response = $this->courier->patch($url, $data, $headers, $handler);
$response = $this->courier->delete($url, $data, $headers, $handler);

所有这些方法都将返回一个CodeZero\Courier\Response类的实例。

读取响应

获取响应体

$body = $response->getBody();

获取附加请求信息

$httpCode = $response->getHttpCode(); //=> "200"
$httpMessage = $response->getHttpMessage(); //=> "OK"
$responseType = $response->getResponseType(); //=> "application/json"
$responseCharset = $response->getResponseCharset(); //=> "UTF-8" 

转换响应

您可以将JSON或序列化响应转换为关联数组或通用的PHP对象数组。如果您发送API请求,则很可能返回application/json数据。Flickr API的php_serial响应类型为application/binary且可序列化。

要转换此数据,只需运行

$array = $response->toArray();
echo $array[0]['some']['nested']['key'];

$objects = $response->toObjects();
echo $objects[0]->some->nested->key;

如果由于某些原因转换失败,将抛出CodeZero\Courier\Exceptions\ResponseConversionException异常。

缓存

要启用缓存,有两个条件

  1. Courier需要使用CodeZero\Courier\Cache\Cache类实例化,并需要提供一个有效的CodeZero\Courier\Cache\CacheManager实现。这是自动完成的。
  2. 缓存分钟数必须大于零(默认:0)

缓存时间

每个请求都可以有不同的缓存时间。只需在方法调用中指定它即可

$this->courier->get($url, $data, $headers, $cacheMinutes); 

在运行时清除缓存

$this->courier->forgetCache();

基本认证

如果您需要使用基本认证,您可以在发送请求之前启用它

$this->courier->setBasicAuthentication('username', 'password');

您也可以撤销这个操作

$this->courier->unsetBasicAuthentication();

异常

请求问题

如果请求无法执行,将抛出CodeZero\Courier\Exceptions\RequestException异常。这可能是因为您的请求配置不正确(不支持的协议等)。

响应问题

如果存在HTTP响应错误 >= 400,将抛出CodeZero\Courier\Exceptions\HttpException异常。除非您正在使用handleHttpException回调。在这种情况下,您必须将此方法添加到您的类中

public function handleHttpException(HttpException $exception)
{
    $errorMessage = $exception->getMessage();
    $errorCode = $exception->getCode();
    $response = $exception->response();
    $responseBody = $response->getBody();

    // If it's JSON:
    $array = $response->toArray();

    // throw your exception
    // or return something
}

测试

$ vendor/bin/phpspec run

安全

如果您发现任何与安全相关的问题,请通过电子邮件ivan@codezero.be而不是使用问题跟踪器。

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件

Analytics