Php 从json解码中检索NULL
Ik获得了以下代码以通过API检索一些json: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
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)