2011-08-15 17 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