dousigan0499 2017-03-12 13:07
浏览 106
已采纳

带有json响应的PHP Http Post请求:没有有效的json

If I send the following request to an API, it says, that I use no valid JSON string. How can I transform my JSON in a valid PHP post request? I'm using guzzle:

$client = new Client();
$res = $client->request('POST', 'https://fghfgh', [
    'auth' => ['user', 'pw']
]);
$res->getStatusCode();
$res->getHeader('application/json');


$res->getBody('{
     "category": "ETW",
     "date": "2017-03-02",
     "address": {
         "nation": "DE",
         "street": "abc",
         "house_number": "7",
         "zip": "80637",
         "town": "München"
     },
     "construction_year": "1932",
     "living_area": "117.90",
     "elevator": false,
     "garages": false
}');
  • 写回答

3条回答 默认 最新

  • dscrn1974 2017-03-12 13:17
    关注

    as mentioned in the documentation

    you need to pass the required headers to your response object as follows:

    $res = $client->request('POST', 'https://fghfgh', [
        'auth' => ['user', 'pw'],
        'headers' => [
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ]
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部