Zttp

来源:互联网 发布:python ctp 接口 编辑:程序博客网 时间:2024/06/08 00:41

在PHP的项目中,如果你需要通过代码来发起 HTTP 请求,根据千锋PHP整理的最新文章,相信很多人对 GuzzleHttp 这个 Package 很熟悉,然而其实在使用 Guzzle 的时候,我们依然可以做得更简便一点的,比如我们可以使用 Zttp,这是基于 Guzzle 的另外一个 HTTP Package。

大致来看看 Zttp 的用法:

1.比如发送一个携带 headers 的 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 还是方便,Nice and clean!

2.携带 Form 表单参数的 POST 请求:

response=Zttp::asFormParams()−>post(url, [

'foo' => 'bar',

'baz' => 'qux',

]);

3.发起 Patch 请求:

response=Zttp::patch(this->url(‘/patch’), [

'foo' => 'bar',

'baz' => 'qux',

]);

4.发起 PUT 请求:

response=Zttp::put(this->url(‘/put’), [

'foo' => 'bar',

'baz' => 'qux',

]);

~~~

5.发起 DELETE 请求:

response=Zttp::delete(this->url(‘/delete’), [

'foo' => 'bar',

'baz' => 'qux',

]);

~

6.添加一个可接受的 Header:

~

response=Zttp::accept(′banana/sandwich′)−>post(url);

7.阻止重定向:

response=Zttp::withoutRedirecting()−>get(url);

你可以看到,上面的这些事例代码其实可以包含了大部分的应用场景,如果说你还需要更复杂的使用方式,你可以到 Github kitetail/zttp 查看;而且,即使你还想使用 Guzzle,你依然是可以使用 Guzzle 的,所以我可以负责任地向大家推荐一下这个 Zttp 的 package.

更多PHP相关技术请搜索千锋PHP,做真实的自己,用良心做教育。

互联网+时代,时刻要保持学习,携手千锋PHP,Dream It Possible。