这是使用 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);
| 欢迎光临 一起源码网 (https://www.171739.xyz/) | Powered by Discuz! X3.3 |