codezero / courier
简单的HTTP请求接口,可选缓存
Requires
- php: >=5.4.0
- codezero/curl: 1.*
- illuminate/contracts: ~5.0
- phpfastcache/phpfastcache: dev-final
Requires (Dev)
- phpspec/phpspec: ~2.0
README
此包提供了一组易于使用的函数,用于在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
异常。
缓存
要启用缓存,有两个条件
- Courier需要使用
CodeZero\Courier\Cache\Cache
类实例化,并需要提供一个有效的CodeZero\Courier\Cache\CacheManager
实现。这是自动完成的。 - 缓存分钟数必须大于零(默认: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)。有关更多信息,请参阅许可文件。