2011-08-15 3 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 exist'라는 메시지가 출력됩니다. 여기에 도움을 주시면 대단히 감사하겠습니다. 솔루션이 될 수 있습니다

+0

무슨 뜻 다른 더 에코없이 "존재 didnt는" ? 어떤 출구 나 휴식을 볼 수 없기 때문에 – Scuzzy

+0

실제로 foreach() 내에 있으므로 여러 결과가 있습니다. – lockdown

+0

그래, 방금 루프의 반복마다 논리에 관계없이 "존재하지 않는"것으로 나타났습니다. 빈 $ json_url을 사용하여 두 번째 컬 요청을 한 다음 파일을 다운로드 한 다음 시간을 확인하고 있습니까? – 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

관련 문제