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

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

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

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

谢谢,如果你有任何线索

4

1 回答 1

0

好的,我发现了问题...我在另一台服务器上尝试过,但出现 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;
    }
于 2013-11-10T08:31:08.420 回答