dougu5847 2015-07-18 13:22
浏览 58
已采纳

将JSON转换为数组PHP时出错

Hello So i have this call.

$preciobitcoin = curlCall('https://www.bitstamp.net/api/ticker/');

and i can perfectly get $preciobitcoin['last'].

but this one

$preciodolar = curlCall('https://s3.amazonaws.com/dolartoday/data.json');

return as a string instead of an array

this is the code of the function which is not working this is the code of the function which is not working

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;

}

  • 写回答

2条回答 默认 最新

  • doujiang2643 2015-07-18 13:46
    关注

    Probably is an encoding issue. Here is an example that works correctly on my localhost.

    $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 ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.