Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从json解码中检索NULL_Php_Curl - Fatal编程技术网

Php 从json解码中检索NULL

Php 从json解码中检索NULL,php,curl,Php,Curl,Ik获得了以下代码以通过API检索一些json: public function get_json($jsonurl, $ttl = 3600) { $cache_file = $this->cache_dir . DIRECTORY_SEPARATOR . md5($jsonurl); $function = __FUNCTION__; if (file_exists($cache_file) && filemtime($cache_fi

Ik获得了以下代码以通过API检索一些json:

    public function get_json($jsonurl, $ttl = 3600) {
    $cache_file = $this->cache_dir . DIRECTORY_SEPARATOR . md5($jsonurl);
    $function = __FUNCTION__;

    if (file_exists($cache_file) && filemtime($cache_file) < time() - $ttl) {
        unlink($cache_file);
    }

    if (!file_exists($cache_file)) {            
        file_put_contents($this->cache_dir . DIRECTORY_SEPARATOR . 'cache.log', $jsonurl . ': ' . $cache_file . "\n", FILE_APPEND); 
        try{
            $response = $this->fetchUrl($jsonurl);
        } catch(Exception $ex){
            return true;
        }

        file_put_contents($cache_file, $response);
    }
    elseif(file_exists($cache_file)){
        $response = file_get_contents($cache_file);
    }
    $json_output = json_decode($response, true);

    if(empty($json_output)){
        unlink($cache_file);
    }

    return $json_output;
}

private function fetchUrl($url) {
    $curl = curl_init($url);
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true
    ));
    $response = curl_exec($curl);
    if (!$response) {
        $exception = new Exception(curl_error($curl) ?: 'Empty response', curl_errno($curl));
        curl_close($curl);
        throw $exception;
    }
    curl_close($curl);
    return $response;
}
公共函数get_json($jsonurl,$ttl=3600){ $cache\u file=$this->cache\u dir.DIRECTORY\u SEPARATOR.md5($jsonurl); $function=\uuuuu function\uuuuuu; 如果(file_存在($cache_file)&&filemtime($cache_file)cache\u dir.DIRECTORY\u分隔符.'cache.log',$jsonurl.:'.$cache\u file.\n',file\u APPEND); 试一试{ $response=$this->fetchUrl($jsonurl); }捕获(例外$ex){ 返回true; } 文件内容($cache\u file,$response); } elseif(文件存在($cache\u文件)){ $response=file\u get\u contents($cache\u file); } $json_output=json_decode($response,true); if(空($json_输出)){ 取消链接($cache_文件); } 返回$json_输出; } 私有函数fetchUrl($url){ $curl=curl\u init($url); curl_setopt_数组($curl,数组( CURLOPT_RETURNTRANSFER=>true, CURLOPT_FOLLOWLOCATION=>true )); $response=curl\u exec($curl); 如果(!$response){ $exception=新异常(curl_error($curl):“空响应”,curl_errno($curl)); curl_close($curl); 抛出$exception; } curl_close($curl); 返回$response; } 在本地它可以正常工作,但在实时服务器上我检索到NULL。当我将jsonurl放入浏览器时,它返回有效的json。那么它可能与fetchUrl函数和curl_setopt_数组有关


请帮我解决这个问题

var\u dump($response)的输出是什么
json_解码之前
?你确定它没有使用缓存文件,并且它是否包含有效的json?看起来有时候我会从以下开始得到奇怪的html:每次在live server上清除缓存,但没有结果trangely这也是在json:string(1497)“Loading…”string(56474)之前“var_dump($response)的输出是什么?”
json_解码之前
?你确定它没有使用缓存文件,并且它是否包含有效的json?看起来有时候我会从以下开始得到奇怪的html:每次在live server上清除缓存,但没有结果Trangely这也是在json:string(1497)“加载…”string(56474)之前