Guzzle - 如何设置json Header?

时间:2016-11-06 16:41:57

标签: php guzzle

似乎尚未为请求标头设置Accept: application/json。我没有得到json的回应。

$params = [
    'client_id'     => 'xxxx',
    'client_secret' => 'xxxxxxxxxx',
    'code'          => $request->get('code'),
    'state'         => $request->get('state'),
];

$client = new Client(['headers' => ['Accept: application/json']]);
$response = $client->post($tokenUrl, [
    'form_params' => $params,
]);

echo $response->getBody();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

根据http://docs.guzzlephp.org/en/latest/request-options.html,您应该将标题写为关联数组。

所以,试试

$client = new Client(['headers' => ['Accept' => 'application/json']]);