zttp php,使用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 的测试文件 中还有几个简单的示例供你查看。 目前这个包还在开发中,有兴趣的童鞋建议直接上 GitHub 吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值