解析 JSON 时出现随机错误


random error on parsing json

我使用当前函数解析json文件:

   function getRemoteJson($uri, $decode=true, $noob=false){
     if($uri){
       $fp = fopen($uri, "r");
       $json = trim(file_get_contents($fp));
       fclose($fp);
      if($decode) return json_decode($json, true);
      else return $json;
      }
     else return false;
   }

这适用于此文件:http://webcast-a.live.sportingpulse.com/matches/3886/09/18/90/33S64Il4cTaxQ/data.json

但是当我使用那个时,它会返回我 null :http://webcast-a.live.sportingpulse.com/matches/3886/09/18/94/84Q4GEVlZs4rg/data.json

解析此文件时没有错误代码(即json_last_error()返回JSON_ERROR_NONE)

当我使用在线工具检查格式时,文件被解析......

如果您有任何线索,谢谢

好吧,我找到了麻烦...我在另一台服务器上尝试过,但遇到了JSON_ERROR_UTF8错误。

因此,如果按如下方式修改代码:

function getRemoteJson($uri, $decode=true, $noob=false, $utf8_error = false){
 if($uri):
   $fp = fopen($uri, "r");
   if($utf8_error) $json = utf8_encode(trim(stream_get_contents($fp))); 
        else $json = trim(stream_get_contents($fp)); 
        fclose($fp);
        $data = json_decode($json, true);
        if(is_array($data)) return $data;   
        else
            if(!$utf8_error) return getRemoteJson($uri, $decode, true, true);
            else return false;
    else:
        return false;
    endif;
    }