var_dump在json_decode之后返回NULL Json对象

时间:2020-08-09 14:21:29

标签: php rest curl libcurl

您好,我在终端机中进行了curl POST操作(我也在POSTMAN中尝试过),以便将一些数据发布到php文件中

curl调用为:

curl -iX POST http://localhost/rest_api/index.php 
  -H 'Content-Type: application/json'   
  -d '{
      "id": "urn:ngsi-ld:Building:store005",
      "type": "Building"
     }'

响应为:

HTTP/1.1 200 OK
Date: Sun, 09 Aug 2020 14:14:37 GMT
Server: Apache/2.4.43 (Unix) OpenSSL/1.1.1g PHP/7.2.32 mod_perl/2.0.11 Perl/v5.32.0
X-Powered-By: PHP/7.2.32
Access-Control-Allow-Origin: *
Content-Length: 105
Content-Type: application/json;charset=utf-8;

array(2) {
  ["id"]=>
  string(29) "urn:ngsi-ld:Building:store005"
  ["type"]=>
  string(8) "Building"
}

我的index.php文件也是:

<?php 
   header('Access-Control-Allow-Origin: *');
   header('Content-Type: application/json;charset=utf-8;');

   $json = file_get_contents('php://input');
   $object = json_decode($json, true);
   die(var_dump($object));

?>

问题在于网站上的var_dump为NULL,而它应该打印json对象。 我的错在哪里?

1 个答案:

答案 0 :(得分:0)

查看响应正文:

MEDIA_ROOT = '/my/path/public_html/media'
MEDIA_URL = '/media/'

那不是JSON。看起来像array(2) { ["id"]=> string(29) "urn:ngsi-ld:Building:store005" ["type"]=> string(8) "Building" } 。检查您呼叫的端点!

相关问题