rmiller / hal-guzzle-response
将 Hal 资源添加到 Guzzle 响应中
0.4.0
2015-09-10 09:36 UTC
Requires
- php: >=5.4
- guzzlehttp/guzzle: ~5.0
- nocarrier/hal: ~0.9
Requires (Dev)
- phpspec/phpspec: ~2.1@dev
- rmiller/exemplify-extension: ~0.2
- rmiller/phpspec-run-extension: ~0.1
This package is not auto-updated.
Last update: 2024-09-14 15:52:55 UTC
README
Guzzle 事件订阅者,用于装饰响应以提供 Hal 资源。
如果您注册了监听器,它将装饰来自 Guzzle 的响应,为它们提供一个 hal
方法。这将返回一个从响应体中创建的 Nocarrier\Hal 资源。
安装
需要扩展
$ composer require rmiller/hal-guzzle-response:~0.1
用法
附加监听器
$client = new \GuzzleHttp\Client(); $emitter = $client->getEmitter(); $subscriber = new \RMiller\HalGuzzleResponse\GuzzleSubscriber(); $emitter->attach($subscriber);
从响应中获取 Hal 资源
$response = $client->get($url); $hal = $response->hal(); //\Nocarrier\Hal
注意:将尝试将响应体从 JSON 和 XML 转换为 Hal 资源。如果都不成功,将抛出 \RuntimeException
异常。这发生在调用 hal
时,因此可以安全地将其他响应包装起来,而不调用 hal
。