2011-08-15 14 views
0

邏輯上,我想弄清楚爲什麼我的代碼不工作。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存在。」任何幫助在這裏非常感謝。

+0

你的意思是「didnt存在」沒有任何其他進一步的回聲?因爲我沒有看到任何退出或中斷 – Scuzzy

+0

其實際上在一個foreach()中,所以有多個結果。 – lockdown

+0

好吧,我只是注意到,對於循環的每一次迭代,回聲的「沒有存在」,無論邏輯如何。你正在檢查時間,然後下載文件,然後用空的$ json_url命中第二個curl請求? – Scuzzy

回答

3

我,可能是解決先觀察,你有:

if(file_exists('templates/cache/' . $persona['toolbar_id'] . 'txt')){ 

不要錯過TXT前一個點?我相信它應該是:

if(file_exists('templates/cache/' . $persona['toolbar_id'] . '.txt')){ 

在所有其他方法,例如這裏:

file_put_contents('templates/cache/' . $persona['toolbar_id']. '.txt', $result); 

你有.txt :)

+0

這有助於它的一部分,但會導致filemtime()stat失敗。 – lockdown

+0

通過語法錯誤後我需要的東西。謝謝 – lockdown