2013-11-09 81 views
0

我使用当前函数解析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

但是,这回我的空当我使用一个: http://webcast-a.live.sportingpulse.com/matches/3886/09/18/94/84Q4GEVlZs4rg/data.json

没有错误代码时,我分析这个文件(即json_last_error()返回JSON_ERROR_NONE)

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

谢谢如果您有任何线索

+0

是第二JSON文件中读取? var_dump($ json)是否返回任何内容?阅读文件时,您正在禁止错误...取出@符号,看看它是否会导致任何错误。 – Gavin

+0

这里我var_dump($ json)后返回我:null 当我使用该工具:http://json.parser.online.fr/ 该文件被正确解析 –

+1

然后它不被读取。删除@符号并查看原因。 – Gavin

回答

0

OK,我发现麻烦......我的hve尝试另一个服务器上我得到了一个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; 
    }