使用Zttp简化Guzzle 调用
Zttp是AdamWathan为了让代码更富表现力以及简化常见用例而写的一个Guzzle的封装。
这是使用Zttp去Post一个自定义头部内容请求的一个例子:
$response=Zttp::withHeaders(['Fancy'=>'Pants'])->post($url,[ 'foo'=>'bar', 'baz'=>'qux', ]); $response->json();
如果用一个与Guzzle差不多的东西写这个请求的话,大概这样写:
$client=newClient();
$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吧!
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下