dousigan0499 2017-03-12 21: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 21: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条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突