[英]Error Converting JSON to array PHP
您好,我有這個電話。
$preciobitcoin = curlCall('https://www.bitstamp.net/api/ticker/');
而且我可以完美地獲得$preciobitcoin['last'].
但是這個
$preciodolar = curlCall('https://s3.amazonaws.com/dolartoday/data.json');
作為字符串而不是數組返回
這是功能不起作用的代碼這是功能不起作用的代碼
function curlCall($url, $params = null, $contentType = 'application/json', $options = array()) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 4);
if (!is_null($params) && !is_null($options['key']) && !is_null($options['sig'])) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: '.$contentType, 'key: '.$options['key'], 'sig: '.$options['sig']));
//
} else if (!is_null($params) && !empty($params)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: '.$contentType));
} else {
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: '.$contentType));
}
// Allow for custom requests
if (isset($options['custom_request']) && !empty($options['custom_request'])) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $options['custom_request']);
}
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; cryptoGlance ' . CURRENT_VERSION . '; PHP/' . phpversion() . ')');
$curlExec = curl_exec($curl);
if ($curlExec === false || curl_errno($curl)) {
$data = array();
} else {
$data = json_decode($curlExec, true);
}
if (empty($data)) {
// return non-jsonfied data
return $curlExec;
}
curl_close($curl);
return $data;
}
可能是編碼問題。 這是一個可以在我的本地主機上正常工作的示例。
$str = file_get_contents( 'https://s3.amazonaws.com/dolartoday/data.json' );
var_dump( mb_detect_encoding( $str ) );
$str = mb_convert_encoding( $str, "UTF-8" );
var_dump( mb_detect_encoding( $str ) );
var_dump( json_decode( $str ));
快速調用json_last_error()會返回此
格式錯誤的UTF-8字符,可能編碼錯誤
該文件中可能有一些隱藏的壞字符(不是utf8)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.