邏輯上,我想弄清楚爲什麼我的代碼不工作。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