如果我向API发送以下请求,它说,我没有使用有效的JSON字符串。如何在有效的PHP帖子请求中转换我的JSON?我用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
}');
答案 0 :(得分:0)
答案 1 :(得分:0)
当我让php解码json时它不会给出任何错误,所以首先想到的是慕尼黑有一个变音符号。如果您在不使用变音符号的情况下尝试此操作会发生什么?
话虽如此,我建议您使用PHP数组并将其编码为json字符串,而不是仅键入json字符串。这是因为PHP可以检查数组的语法,并且您可以立即知道它是对还是错。如果选项A不起作用,则可能会解决您的问题。
看起来像这样:
$data = [
'category' => 'ETW',
'date' => '2017-03-02',
'address' => [
'nation' => 'DE',
'street' => 'abc',
'house_number' => 7,
'zip' => '80637',
'town' => 'Munchen'
],
'construction_year' => 1932,
'living_area' => '117.90',
'elevator' => false,
'garages' => false,
];
$res->getBody(json_encode($data));
答案 2 :(得分:0)
如果您的目的是 POST JSON数据
,我认为您应该使用json
选项
$client->post('https://fghfgh', [
'auth' => ['user', 'pw'],
'json' => [
'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,
]
]);