教程 > laravel 教程 > 阅读:87

laravel http客户端——迹忆客-ag捕鱼王app官网

简介

laravel 在 guzzle http client 基础上封装了一个优雅的、最小化的 api,从而方便开发者快速创建 http 请求与其他 web 应用进行通信。laravel 对 guzzle 的封装围绕的是最常用的场景,并且致力于提供更好的开发体验。

开始使用之前,需要确保已经在项目中安装过 guzzle 扩展包依赖,默认情况下,laravel 会自动包含这个依赖:

$ composer require guzzlehttp/guzzle

创建请求

要创建请求,可以使用 getpostputpatch 以及 delete 方法。首先,让我们看看如何创建最基本的 get 请求:

use illuminate\support\facades\http;
​
$response = http::get('http://blog.test');

get 方法返回一个 illuminate\http\client\response 实例,该实例上提供了多个对响应进行「透视」的方法:

$response->body() : string;
$response->json() : array;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->servererror() : bool;
$response->clienterror() : bool;
$response->header($header) : string;
$response->headers() : array;

illuminate\http\client\response 对象还实现了 php arrayaccess 接口,这样一来,就可以直接在响应实例上访问 json 响应数据:

return http::get('http://blog.test/users/1')['name'];

请求数据

当然,在 post、get 和 patch 请求中发送额外请求数据很常见,这些方法可以接收数组格式请求数据作为第二个参数。默认情况下,数据会以 application/json 内容类型发送:

$response = http::post('http://blog.test/users', [
    'name' => 'steve',
    'role' => 'network administrator',
]);

获取请求的query 参数

发出get请求时,可以直接将查询字符串附加到 url 或将键/值对数组作为第二个参数传递给该get方法:

$response = http::get('http://test.com/users', [
    'name' => 'taylor',
    'page' => 1,
]);

发送表单 url 编码请求

如果我们想使用 application/x-www-form-urlencoded内容类型发送数据(一般 html 表单都是这个格式),需要在创建请求前调用 asform 方法:

$response = http::asform()->post('http://blog.test/users', [
    'name' => 'sara',
    'role' => 'privacy consultant',
]);

发送原始请求正文

如果想在发出请求时提供原始请求正文,则可以使用withbody方法:

$response = http::withbody(
    base64_encode($photo), 'image/jpeg'
)->post('http://test.com/photo');

multi-part 请求

对于文件上传请求,需要以 multi-part 内容类型发起请求,这可以通过在创建请求前调用 attach 方法完成。该方法接收文件名和内容作为参数,还可以传递第三个参数作为期望文件名:

$response = http::attach(
    'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
)->post('http://blog.test/attachments');

除了传递文件原生内容外,还可以传递流资源:

$photo = fopen('photo.jpg', 'r');
​
$response = http::attach(
    'attachment', $photo, 'photo.jpg'
)->post('http://blog.test/attachments');

请求头

可以使用 withheaders 方法添加请求头到请求,withheaders 方法接收数据格式是键值对数组:

$response = http::withheaders([
    'x-first' => 'foo',
    'x-second' => 'bar'
])->post('http://blog.test/users', [
    'name' => '学院君',
]);

认证

我们可以使用 withbasicauthwithdigestauth 方法设置认证方式:

// basic authentication...
$response = http::withbasicauth('taylor@laravel.com', 'secret')->post(...);
​
// digest authentication...
$response = http::withdigestauth('taylor@laravel.com', 'secret')->post(...);

bearer tokens

如果你想要在请求中快速添加 authorization bearer token 头,可以使用 withtoken 方法:

$response = http::withtoken('token')->post(...);

###超时 timeout方法可用于指定等待响应的最大秒数:

$response = http::timeout(3)->get(...);

如果超过给定的超时,将抛出illuminate\http\client\connectionexception异常实例。

重试

如果我们想要 http 客户端在客户端或服务端发生错误时自动重发请求,可以使用 retry 方法。该方法接收两个参数 —— 重试次数和两次重试之间的时间间隔(ms):

$response = http::retry(3, 100)->post(...);

如果所有重试也失败,则抛出 illuminate\http\client\requestexception 异常。

错误处理

不同于 guzzle 默认的行为,laravel 的 http 客户端封装并不会在客户端或服务端发生错误时抛出异常,我们需要使用 successful、clienterror 或者 servererror 方法来判断是否返回错误:

// determine if the status code was >= 200 and < 300...
$response->successful();
​
// determine if the response has a 400 level status code...
$response->clienterror();
​
// determine if the response has a 500 level status code...
$response->servererror();

抛出异常

如果我们持有一个响应实例,并且在该响应是客户端或服务端错误的情况下想要抛出一个 illuminate\http\client\requestexception 异常实例,可以使用 throw 方法:

$response = http::post(...);
​
// throw an exception if a client or server error occurred...
$response->throw();
​
return $response['user']['id'];

illuminate\http\client\requestexception 实例包含一个公开的 $response 属性,你可以通过它来检查返回的响应。

throw 方法会在没有错误发生时返回响应实例,所以你可以通过方法链的形式在上面定义更多其他操作:

return http::post(...)->throw()->json();

guzzle 选项

我们可以使用 withoptions 方法指定更多额外的 guzzle 请求选项,该方法接收数组格式参数来指定多个选项:

$response = http::withoptions([
    'debug' => true,
])->get('http://blog.test/users');

测试

很多 laravel 服务都提供了助力开发者轻松优雅编写测试代码的功能,laravel http 客户端封装库也不例外,通过 http 门面的 fake 方法,你可以轻松构造 http 客户端并在发起请求后返回预定义(通过桩文件或者模板代码设置)响应。

伪造响应

例如,要构建一个 http 客户端针对任意请求都返回空实体、200 状态码响应,可以调用不传入任何参数的 fake 方法实现:

use illuminate\support\facades\http;
​
http::fake();
​
$response = http::post(...);

伪造指定 url

我们还可以传递数组到 fake 方法,该数组的键表示你希望伪造的请求 url 模式字符串,该数组的值表示与之关联的响应。在 url 模式字符串中可以使用 * 作为通配符。在预定义响应中,我们使用的是 response 方法来构建响应:

http::fake([
    // stub a json response for github endpoints...
    'github.com/*' => http::response(['foo' => 'bar'], 200, ['headers']),
​
    // stub a string response for google endpoints...
    'google.com/*' => http::response('hello world', 200, ['headers']),
]);

如果我们想要指定兜底 url 模式来处理所有未匹配请求,可以使用 *作为键:

http::fake([
    // stub a json response for github endpoints...
    'github.com/*' => http::response(['foo' => 'bar'], 200, ['headers']),
​
    // stub a string response for all other endpoints...
    '*' => http::response('hello world', 200, ['headers']),
]);

伪造响应序列

有时候,我们可能需要指定单个 url 以特定顺序返回多个伪造响应,这可以通过使用 http::sequence 方法构建响应来实现:

http::fake([
    // stub a series of responses for github endpoints...
    'github.com/*' => http::sequence()
                            ->push('hello world', 200)
                            ->push(['foo' => 'bar'], 200)
                            ->pushstatus(404),
]);

当响应序列中的所有响应都已经被消费,新请求进来会抛出异常,如果你想要在序列为空的情况下指定默认响应,可以使用 whenempty 方法:

http::fake([
    // stub a series of responses for github endpoints...
    'github.com/*' => http::sequence()
                            ->push('hello world', 200)
                            ->push(['foo' => 'bar'], 200)
                            ->whenempty(http::response()),
]);

如果我们想要伪造响应序列但不需要指定特定的 url 模式,可以使用 http::fakesequence 方法:

http::fakesequence()
        ->push('hello world', 200)
        ->whenempty(http::response());

伪造回调

如果我们需要更复杂的逻辑来判断特定端点返回什么响应,可以传递一个回调函数到 fake 方法。该回调函数接收一个 illuminate\http\client\request 实例并返回一个对应的响应实例:

http::fake(function ($request) {
    return http::response('hello world', 200);
});

检查请求

伪造响应时,我们可能想要检查客户端请求以便确保应用发送的是正确的数据或者请求头。这可以通过在调用 http::fake 之后调用 http::assertsent 方法来实现。

assertsent 方法接收一个回调函数作为参数,该回调函数需要传入 illuminate\http\client\request 实例然后返回一个布尔类型的值,用来表明请求是否和预期匹配。为了让测试通过,至少要发出一个和预期匹配的请求:

http::fake();
http::withheaders([
    'x-first' => 'foo',
])->post('http://blog.test/users', [
    'name' => '学院君',
    'role' => 'developer',
]);
http::assertsent(function ($request) {
    return $request->hasheader('x-first', 'foo') &&
           $request-> == 'http://blog.test/users' &&
           $request['name'] == '学院君' &&
           $request['role'] == 'developer';
});

查看笔记

扫码一下
查看教程更方便
网站地图