denismitr / net-call
易于使用和模拟的HTTP客户端,围绕Guzzle封装了大多数常见的HTTP调用功能。
v0.1
2018-01-20 20:10 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^6.0
- tightenco/collect: ^5.4
Requires (Dev)
- laravel/lumen-framework: 5.5.*
- mockery/mockery: ^1.0
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-23 08:57:58 UTC
README
NetCall是一个便捷且易于使用的HTTP客户端。它是一个围绕Guzzle的包装器,适用于大多数常见用例。它旨在使开发和测试更加容易和愉快。
作者
Denis Mitrofanov
安装
composer require denismitr/net-call
用法
$response = NetCall::new()->get('http://www.google.com?foo=bar'); // NetCallResponseInterface methods $response->body(); // : string $response->json(); // : array $response->header('some-key'); $response->headers(); // : array $response->status(); // : int $response->isSuccess(); // : bool $response->isOk(); // : bool $response->isRedirect(); // : bool $response->isServerError(); // : bool
// request params will be json encoded by default $response = NetCall::new()->post('http://test.com/post', [ 'foo' => 'bar', 'baz' => 'qux', ]); $response->json(); // array with json response data
从参数
$response = NetCall::new()->asFormData()->post('http://myurl.com/post', [ 'foo' => 'bar', 'baz' => 'qux', ]);
多部分
$response = NetCall::new()->asMultipart()->post('http://myurl.com/multi-part', [ [ 'name' => 'foo', 'contents' => 'bar' ], [ 'name' => 'baz', 'contents' => 'qux', ], [ 'name' => 'test-file', 'contents' => 'test contents', 'filename' => 'test-file.txt', ], ]);
带有头部
$response = NetCall::new() ->withHeaders(['Custom' => 'Header']) ->get('http://myurl.com/get');
设置接受头部
$response = NetCall::new() ->accept('application/json') ->post('http://myurl.com/post');
支持补丁请求
$response = NetCall::new()->patch('http://myurl.com/patch', [ 'foo' => 'bar', 'baz' => 'qux', ]);
异常
在4xx和5xx上不抛出异常:请使用响应状态方法。
重定向
默认情况下跟随重定向
要禁用此功能
$response = NetCall::new()->noRedirects()->get('http://myurl.com/get'); $response->status(); // 302 $response->header('Location'); // http://myurl.com/redirected
认证
基本认证
$response = NetCall::new() ->withBasicAuth('username', 'password') ->get('http://myurl.com/basic-auth');
摘要认证
$response = NetCall::new() ->withDigestAuth('username', 'password') ->get('http://myurl.com/digest-auth');
超时
设置超时
NetCall::new()->timeout(1)->get('http://myurl.com/timeout'); // If more then a second passes // \Denismitr\NetCall\Exceptions\NetCallException is thrown