A curious case of JSON data being out-of-sync
Update: this didn’t solve the problem. It was a bug in Drupal itself.
I am using Drupal as an admin system for a project. This allows me to quickly set up database tables and bespoke editing views, along with JSON feeds of the data that I can use elsewhere.
The Drupal server is on the client’s server, which is with one of the big service providers. A curious problem arose when data in the front-end system wasn’t syncing with the data on the Drupal server. Upon investigation I could that accessing the JSON data directly from by browser gave the correct response, but for some reason the front-end system wasn’t seeing it.
I put together a little test PHP file, something like this:
<?PHP
$jsonString = file_get_contents ( 'https://www.website.com/datafeed?_format=json' );
echo ( $jsonString );
?>
Surprisingly, it showed old (days old!) data, not the data in the Drupal database.
So the server must be responding differently to the two different requests. I thought about updating the HTTP headers of the file_get_contents
request but realised that since it is on the service provider server I may not be able to do the necessary configuration changes. Of course there is a much simpler solution - make the request unique, forcing the server to returning fresh every time. This is known as cache busting.
<?PHP
$url = "https://www.website.com/feed?_format=json&nocache=" . time();
$jsonString = file_get_contents($url);
echo $jsonString;
?>
So problem solved.