indigophp / http-adapter
Requires
- php: >=5.4.0
- psr/http-message: ~0.5.1
Requires (Dev)
- guzzle/guzzle: ~3.9.0
- guzzlehttp/guzzle: ~5.0.0
- henrikbjorn/phpspec-code-coverage: 1.0.*@dev
- kriswallsmith/buzz: v0.13
- league/event: dev-master
- league/url: ~3.3.0
- phpspec/phpspec: 2.1.*@dev,>=2.1.0-RC2
- tedivm/stash: dev-feature/pool-trait
Suggests
- league/url: Used for base URL handling in Client
This package is not auto-updated.
Last update: 2022-02-01 12:41:48 UTC
README
提供常见 HTTP 客户端库的适配器。
为什么还需要另一个 HTTP 库?
提议的 HTTP 消息 PSR 将非常出色……但这不会让任何人创建 HTTP 客户端无关的软件包。例如,你不能对 ClientInterface
进行类型提示。你只能操作相同的消息。PSR Meta 还提到了适配器软件包作为有效的方法。有许多很酷的适配器软件包,但它们都基于作者的需求实现简单逻辑。你可以将这个软件包视为一个简单的 Adapter
,它为你提供了对实现 HTTP 客户端库的常见功能的完全控制。然而,必须注意的是,具体功能不在范围之内。例如:如果你需要使用 Guzzle 特定功能,那么请依赖它。
此软件包还提供了一个最新 PSR 接口的简单实现。
安装
通过 Composer
$ composer require indigophp/http-adapter
用法
简单用法
你可以在你的应用程序中直接使用任何适配器。
use Indigo\Http\Adapter; class MyAdapterAware { /** * @var Adapter */ private $adapter; public function __construct(Adapter $adapter) { $this->adapter = $adapter; } public function get() { $request = new Request; $request->setUrl('http://foo.com'); return $this->adapter->send($request); } }
你也可以使用客户端类来进行最常见的客户端使用。(Guzzle 只作为示例使用)
use GuzzleHttp\Client as GuzzleClient; use Indigo\Http\Adapter\Guzzle4; use Indigo\Http\Client; $adapter = new Guzzle4(new GuzzleClient); $client = new Client($adapter); $client->get('http://foo.com');
高级用法
对于测试,你可以使用 Mock
适配器。
use Indigo\Http\Adapter\Mock; use Psr\Http\Message\RequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; // ... your testing logic // you can directly pass the Response object to the constructor $adapter = new Mock(function(Request $request) { return new Response; }); // Optionally $adapter->setResponse(new Response); // ... your testing logic
你可以用各种装饰器装饰你的适配器。
事件装饰器
事件装饰器触发两个事件
- 之前(请求)
- 完成(响应)
这两个事件都接收 Adapter
和 Request
。完成事件还包含 Response
。
use Indigo\Http\Adapter\Event; use Indigo\Http\Event; $adapter = new Event($decoratedAdapter); // Optionally // $adapter->setEmitter($emitter); $adapter->addListener('before', function(Event\Before $event) { $adapter = $event->getAdapter(); $request = $event->getRequest(); // ... do something with the adapter and the request }); $adapter->addListener('complete', function(Event\Complete $event) { $adapter = $event->getAdapter(); $request = $event->getRequest(); $response = $event->getResponse(); // ... do something with the adapter, request and the response });
你还可以使用与 Event
适配器一起使用的 Subscriber
。
use Indigo\Http\Adapter\Event; use Indigo\Http\Subscriber\Auth; $adapter = new Event($decoratedAdapter); // This will always attach authentication data to your requests $adapter->addSubscriber(new Auth('username', 'password', Auth::BASIC));
目前使用 league/event 作为事件后端。
缓存装饰器
你可以使用本地缓存来存储返回的 Response
。根据缓存的项,你可以向服务器发送带有 If-Modified-Since
和 If-None-Match
(响应中需要 ETag
标头)头的 Request
。如果服务器返回 304 状态,则返回缓存的项,否则将其缓存以供将来使用。
use Indigo\Http\Adapter\Cache; $adapter = new Cache($decoratedAdapter); // Optionally // $adapter->setPool($pool); // Status: 200 OK $response = $adapter->send($request); // Status: 304 Not Modified // Returned from cache $response = $adapter->send($request);
目前使用 Stash 作为缓存后端。
异常
有两种主要的异常类型
AdapterException
:当出现某种适配器问题时抛出。RequestException
:如果响应本身是错误响应(4xx、5xx)或请求无法完成(未返回响应)时抛出。
测试
$ phpspec run
贡献
请参阅贡献指南获取详细信息。
鸣谢
灵感来源于
- Guzzle
- fXmlRpc
许可
MIT许可证(MIT)。请参阅许可文件获取更多信息。