I'm currently working on a site that uses a PHP function to get JSON data and display it on the page. However, when loading the page, it freezes until it has successfully gotten the response, which looks strange because it hasn't loaded the closing html tags yet.
I could make an ajax call with Javascript which would happen asynchronously after page load, but as the pages are static I am caching them with PHP so this way the response wouldn't be cached.
Is there either a way to make the PHP JSON call happen after page load with PHP or could I cache the javascript JSON response?
I'd remove the JSON fetch from being performed inline and use JavaScript to do an AJAX call. From there, you can run the JSON through a standalone PHP script on your site and add some additional caching, like apc, to speed the PHP call up.
On apc caching, you'll need mod_apc installed. Look up the apc_fetch and apc_store function calls which you can use to cache the JSON without having to make a costly call so often.
If you're doing a GET request where all of your API parameters are in the url, you could do something like this to speed up repeat AJAX requests.
$url = "http://songkick.com/api/url/to/whatever";
$apcKey = "url:$url";
$data = apc_fetch($apcKey);
if(!$data) {
$data = file_get_contents($url); //or curl, or whatever you're using.
apc_store($apcKey, $data); //save for next time.
}
echo $data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With