json_decode等于null

时间:2015-12-01 20:27:38

标签: php json curl

如何获得更好的错误消息以调试我的问题?

<?php

// Setup cURL
$ch = curl_init('http://api.mirai.so/external/test');
curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
));

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    echo "Error!";
}
else {
$result = var_dump(json_decode($response, TRUE));
echo $result;
}
?>

它应该做的是从.json文件中获取数据并回显它们。

2 个答案:

答案 0 :(得分:1)

更新了curl选项并设置了ini user_agent。经过测试和工作。

<?php
ini_set('user_agent','MSIE 4\.0b2;'); // this is required. otherwise you'll get a 401

// Setup cURL
$url = "http://api.mirai.so/external/test";
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_RETURNTRANSFER => TRUE,
));
$response = curl_exec($ch); // Send the request

// Check for errors
IF ($response === FALSE) {
    echo "Error!";
}ELSE{
  $result = json_decode($response, true);
  die("<pre>".print_r($result, true)."</pre>"); // prettier
}
?>

答案 1 :(得分:0)

您要将var_dump函数(返回null)的结果分配给$result变量。 你为什么这样做?

您应该将json_decode结果分配给$result

$result = json_decode($response, TRUE);
相关问题