小编use*_*353的帖子

PHP:使用curl读取时,json_decode返回NULL

我需要解码一个JSON片段,以便我可以开始操作JSON,但是当我尝试使用curl读取时json_decode返回null.

PHP代码:

$username='xx';
$password='xx';
$headers = array(
'Content-Type:application/json',
    'accept: application/json',
    'Authorization: Basic '. base64_encode($username.":".$password),
    'x-myobapi-exotoken: xx'
);

$url = "http://example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);

echo '<pre>';
echo $output; //works fine till here

echo "<br><br>Break <br>";

//all the commented part has been tried

//$my_array = json_decode(str_replace ('\"','"', $output), true);
//var_dump($my_array);
// print_r(json_decode(substr($output,3))); //tried already

$obj = json_decode($output);
var_dump($obj);//gives NULL
print …
Run Code Online (Sandbox Code Playgroud)

php json curl

0
推荐指数
1
解决办法
1471
查看次数

标签 统计

curl ×1

json ×1

php ×1