【问题标题】:Guzzle - How to set json Header?Guzzle - 如何设置 json 标头?
【发布时间】:2016-11-07 00:45:28
【问题描述】:

似乎没有为请求标头设置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();

我该如何解决这个问题?

【问题讨论】:

    标签: php guzzle


    【解决方案1】:

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

    所以,试试

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

    【讨论】:

    • 唷。你的回答救了我。谢谢!
    猜你喜欢
    • 2021-03-27
    • 2016-09-07
    • 2015-07-18
    • 1970-01-01
    • 2013-07-19
    • 2012-06-19
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多