逻辑上,我想弄清楚为什么我的代码不工作。file_exists()和过期的缓存不遵循逻辑
foreach($list as $persona){
//If file exists
if(file_exists('templates/cache/' . $persona['toolbar_id'] . 'txt')){
//file is expired older than 10 seconds for testing purposes
if(time() - filemtime('/templates/cache/' . $persona['toolbar_id'] . '.txt') >= 10) {
// jSON URL which should be requested
$json_url = 'example';
// Initializing curl
$ch = curl_init($json_url);
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
);
// Setting curl options
curl_setopt_array($ch, $options);
// Getting results
$result = curl_exec($ch); // Getting jSON result string
file_put_contents('templates/cache/' . $persona['toolbar_id']. '.txt', $result);
$result = json_decode($result, true);
$result = $result[0];
echo 'expired-recreating cache';
}
$result = json_decode(file_get_contents('templates/cache/' . $persona['toolbar_id']. '.txt'), true);
$result = $result[0];
}
// jSON URL which should be requested
$json_url = ''';
// Initializing curl
$ch = curl_init($json_url);
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
// Setting curl options
curl_setopt_array($ch, $options);
// Getting results
$result = curl_exec($ch); // Getting jSON result string
file_put_contents('templates/cache/' . $persona['toolbar_id']. '.txt', $result);
$result = json_decode($result, true);
$result = $result[0];
echo 'didnt exist';
}
我总是结束了与回波的结果“didnt存在。”任何帮助在这里非常感谢。
你的意思是“didnt存在”没有任何其他进一步的回声?因为我没有看到任何退出或中断 – Scuzzy
其实际上在一个foreach()中,所以有多个结果。 – lockdown
好吧,我只是注意到,对于循环的每一次迭代,回声的“没有存在”,无论逻辑如何。你正在检查时间,然后下载文件,然后用空的$ json_url命中第二个curl请求? – Scuzzy