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;
  2. // Grab the client's handler instance.
  3. $clientHandler = $client->getConfig('handler');
  4. // Create a middleware that echoes parts of the request.
  5. $tapMiddleware = Middleware::tap(function ($request) {
  6. echo $request->getHeader('Content-Type');
  7. // application/json
  8. echo $request->getBody();
  9. // {"foo":"bar"}
  10. });
  11. $response = $client->request('PUT', '/put', [
  12. 'json' => ['foo' => 'bar'],
  13. 'handler' => $tapMiddleware($clientHandler)
  14. ]);