mjelamanov/psr18-guzzle
最新稳定版本:1.7.0
Composer 安装命令:
composer require mjelamanov/psr18-guzzle
包简介
A PSR-18 adapter for guzzle 6 client
README 文档
README
A PSR-18 adapter for Guzzle 6 client.
Requirements
PHP 7.0 or above.
Installation
composer require mjelamanov/psr18-guzzle
Example
require_once __DIR__ . '/vendor/autoload.php'; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Request; use Mjelamanov\GuzzlePsr18\Client; $yourOptionalConfig = []; $guzzleClient = new GuzzleClient($yourOptionalConfig); $client = new Client($guzzleClient); // create an adapter instance $request = new Request('GET', 'http://example.com'); // A PSR-7 request instance $response = $client->sendRequest($request); // Sending request var_dump($response->getStatusCode(), (string) $response->getBody()); // Prints response
Error handling
How PSR-18 clients should handle errors, see here.
This library tries to follow to the that recommendation but Guzzle's custom handlers may break this compatibility.
In this case of you can tell me through "new issue" or send me a "pull request".
By default Guzzle client throws exceptions for HTTP 400 and 500 errors and this adapter is caught them and return
response as is. You can disable this behavior by http_errors option but the result will be the same.
... use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\NetworkExceptionInterface; use Psr\Http\Client\RequestExceptionInterface; ... try { $response = $client->sendRequest($request); } catch (NetworkExceptionInterface $e) { // Network issues } catch (RequestExceptionInterface $e) { // When request is invalid } catch (ClientExceptionInterface $e) { // In all other cases }
Test
composer test
License
The MIT license. Read LICENSE file.
统计信息
- 总下载量: 181.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 2
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-18