How to make PHP Curl work to using http cache? [solved]


#1

I’ve been using Http-Cache following slim documentation…

It works very well in browser and ajax. output header is response 304 means it load from cache.

But, after I have test using php curl to call my api.
It seems http-cache not working for php curl or i do wrong to create function curl to work using http cache.

Here is my function curl to call the API:

function testCurlWithCache($url){
            //open connection
	    	$ch = curl_init($url);
            
            //curl options
		curl_setopt($ch, CURLOPT_HTTPHEADER,1);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
	    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
            
            //execute response
		$response = curl_exec($ch);

            //close connection
    		curl_close($ch);

	    	return $response;
    }

Please help me to make curl work using http-cache. Thanks


#2

Have you tried to set the correct HTTP header in curl?

For example:

$lastmod = 'If-Modified-Since: Wed, 24 Feb 2016 22:38:22 GMT';
$etag = 'If-None-Match: "ffffffffec3c286a-json"';

curl_setopt($ch, CURLOPT_HTTPHEADER , array($lastmod, $etag));

PS: $lastmod and $etag contains just a pseudo value. You need to copy this values from the previous curl request. More details.


#3

Should I copy etag value from last request curl with using session?

Or better i write the output response json to file with expired time?

What is better? Thanks


#4

Solved… I use guzzle…