您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Zttp简化Guzzle调用实例分享

2025/6/18 17:12:44发布10次查看
zttp 是 adam wathan 为了让代码更富表现力以及简化常见用例而写的一个 guzzle 的封装。在 php 的项目中,如果你需要通过代码来发起 http 请求,相信很多人对 guzzlehttp 这个 package 很熟悉,然而其实在使用 guzzle 的时候,我们依然可以做得更简便一点的,本文我们就和大家分享zttp简化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);
相关推荐:
php http 客户端和框架:guzzle
关于guzzle安装问题
laravel 怎么使用guzzlehttp/guzzle
以上就是zttp简化guzzle调用实例分享的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product