How to cache wp_remote_get response to generate response code 304

I am using WordPress’s wp_remote_get() method to retrieve some content from an API server. The data is returned as expected but the response code is always 200 OK.

What I need is, to get response code 304 when the data is not modified.

Read More

If I directly open the API url in web browser, it sure sends 304 response on subsequent requests. I have verified this using chrome developer tools.

But when, I use PHP code to get the content of URL, the response is always 200 OK

The bottom line is I don’t want to fetch data from API, if the data is not modified. As it is obvious, I may need some kind of caching here if the browser is not caching that that request.

The tricky part is, the request is generated from PHP server each time and I am not sure if there is any possibility that PHP communicates with browser cache before generating such requests.

I also tried curl, but the response code was again 200 always.

Related posts

1 comment

  1. You need to include a request header If-Modified-Since, for example:

    If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
    

    You may also wish to include the Cache-Control header, which must be passed through any proxy (though may not be supported by HTTP/1.0 caches):

    Cache-Control: max-age=31536000
    

    For more information, see the HTTP/1.1 reference section on Headers at http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Comments are closed.