json

摘要

json 选项用来轻松将JSON数据当成主体上传, 如果没有设置Content-Type头信息的时候会设置成 application/json 。

类型

能够 json_encode() 操作的PHP类型。

默认值

None

常量

GuzzleHttp\RequestOptions::JSON

1. $response = $client->request('PUT', '/put', ['json' => ['foo' => 'bar']]);

这里的例子使用了 tap 中间件用来查看发送了什么请求。

1. use GuzzleHttp\Middleware;

3. // Grab the client's handler instance.
4. $clientHandler = $client->getConfig('handler');
5. // Create a middleware that echoes parts of the request.
6. $tapMiddleware = Middleware::tap(function ($request) {
7. echo $request->getHeader('Content-Type');
8. // application/json
9. echo $request->getBody();
10. // {"foo":"bar"}
11. });

13. $response = $client->request('PUT', '/put', [
14. 'json'    => ['foo' => 'bar'],
15. 'handler' => $tapMiddleware($clientHandler)
16. ]);