开发者

file_exists() and expired cache not following logic

开发者 https://www.devze.com 2023-03-27 21:56 出处:网络
Logically I am trying to figure out why my code is not working. foreach($list as $persona){ //If file exists

Logically I am trying to figure out why my code is not working.

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 res开发者_如何学Goults
                $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';
}

I am always ending up with the results of the echo 'didnt exist.' Any help here is greatly appreciated.


my first observation that might be solution, you have:

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

Don't you miss a dot before txt? I believe it should be:

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

In every other method, for example here:

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

you have .txt :)

0

精彩评论

暂无评论...
验证码 换一张
取 消