简体   繁体   English

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

[英]PHP Http Post request with json response: no valid json

If I send the following request to an API, it says, that I use no valid JSON string. 如果我将以下请求发送到API,则表明我不使用有效的JSON字符串。 How can I transform my JSON in a valid PHP post request? 如何在有效的PHP发布请求中转换JSON? 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
}');

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',
    ]
]);

When I let php decode the json it won't give any errors, so the first thing that comes into mind is the München having an umlaut. 当我让php解码json时,它不会给出任何错误,因此首先想到的是慕尼黑的变音符号。 What happens if you try this without using the umlaut? 如果不使用变音符号尝试此操作会怎样?

Having said that, I recommend you using an PHP array and encoding that into a json string instead of just typing the json string. 话虽如此,我建议您使用PHP数组并将其编码为json字符串,而不是仅输入json字符串。 This is because PHP can check the syntax for the array and you know whether it's right or wrong immediately. 这是因为PHP可以检查数组的语法,并且立即知道它是对还是错。 If option A doesn't work, this might fix your problem instead. 如果选项A不起作用,则可能会解决您的问题。

It would look like this: 它看起来像这样:

$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));

Looks to me like you should be using the json option if your intention is to POST JSON data 在我看来就像您应使用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,
    ]
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM