十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍使用Zttp简化Guzzle调用的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Zttp 是 Adam Wathan 为了让代码更富表现力以及简化常见用例而写的一个 Guzzle 的封装。
这是使用 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);
以上是“使用Zttp简化Guzzle调用的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!