时间:2021-07-01 10:21:17 帮助过:4人阅读
这是使用 Zttp 去 Post 一个自定义头部内容请求的一个例子:
$response = Zttp::withHeaders(['Fancy' => 'Pants'])->post($url, [ 'foo' => 'bar', 'baz' => 'qux', ]); $response->json();
如果用一个与 Guzzle 差不多的东西写这个请求的话,大概这样写:
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => [
'Fancy' => 'Pants',
],
'form_params' => [
'foo' => 'bar',
'baz' => 'qux',
]
]);
json_decode($response->getBody());相较之下,Zttp 简化了代码的写法,还能很简单地返回 JSON 格式的内容。
下面是 使用 Zttp 的几个例子:
带参数的 Post 请求#
$response = Zttp::asFormParams()->post($url, [ 'foo' => 'bar', 'baz' => 'qux', ]);
Patch 请求#
$response = Zttp::patch($this->url('/patch'), [
'foo' => 'bar',
'baz' => 'qux',
]);Put 请求#
$response = Zttp::put($this->url('/put'), [
'foo' => 'bar',
'baz' => 'qux',
]);Delete 请求#
$response = Zttp::delete($this->url('/delete'), [
'foo' => 'bar',
'baz' => 'qux',
]);添加请求头#
$response = Zttp::accept('banana/sandwich')->post($url);防止重定向#
$response = Zttp::withoutRedirecting()->get($url);
相关推荐:
PHP HTTP 客户端和框架:Guzzle
关于guzzle安装问题
laravel 怎么使用guzzlehttp/guzzle
以上就是Zttp简化Guzzle调用实例分享的详细内容,更多请关注Gxl网其它相关文章!