5

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?

5
  • 1
    You're caching PHP files ? Commented Jun 7, 2013 at 19:05
  • Yes, caching PHP output to html files Commented Jun 7, 2013 at 19:08
  • 1
    It isn't so clear what you're doing here. Currently, when a client vists your page you're using PHP to REQUEST a different web-resource, which sends back a JSON response, which you're then sending through to the client who visited your page? Why not separate the page's PHP from the JSON REQUEST, and have the client do a second async request when their page has loaded? Commented Jun 7, 2013 at 19:08
  • Sorry for not being more clear. My page makes an api call to songkick.com and returns a json response with tour dates for an artist which I then format and display on the page. How would I separate the page's PHP from JSON request with a second async request? Commented Jun 7, 2013 at 19:14
  • This depends largely on whether the JSON is publicly reachable, or if you're using an API key to access it. If you're using an API key, you definitely shouldn't send that down to the client to start making their own requests. Commented Jun 7, 2013 at 19:22

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll take a look at apc caching. At the moment I'm just using a very basic page cache function
1

The approach you're taking is referred to as 'bootstrapping'. It's generally good practice to de-couple components where possible however, in your case, if you're bootstrapping a large amount of data to the page, it may be better to leave it as is and cache the resulting html (Cloudflare is my favorite tool for this).

Running an asynch javascript request for a big hunk of data can result in undesirable front-end load time - especially if you have prerequisite libraries like jQuery to load first.

The larger the bootstrapped JSON, the better the case to use PHP.

1 Comment

Thanks Dave, yeah the javascript request was really increasing the load time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.