Facebook graph extremely slow in PHP Facebook graph extremely slow in PHP curl curl

Facebook graph extremely slow in PHP


file_get_contents can be very slow in PHP because it doesn't send/process headers properly, leading to the HTTP connection not getting closed properly when the file transfer is complete. I have also read about DNS issues, though I don't have any information about that.

The solution that I highly recommend is to either use the PHP SDK, which is designed for making API calls to Facebook, or make use of cURL (which the SDK uses). With cURL you can really configure a lot of aspects of the request, since it's basically designed for making API calls like this.

PHP SDK information: https://developers.facebook.com/docs/reference/php/

PHP SDK source: https://github.com/facebook/facebook-php-sdk

If you choose to do it without the SDK, you could look at how they make use of cURL in base_facebook.php. here is some sample code you could use to fetch using cURL:

function get_url($url){   $ch = curl_init();   curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HEADER, FALSE);  // Return contents only   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  // return results instead of outputting   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10) // Give up after connecting for 10 seconds    curl_setopt($ch, CURLOPT_TIMEOUT, 60);  // Only execute 60s at most   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // Don't verify SSL cert   $response = curl_exec($ch);   curl_close($ch);   return $response;}$contents = get_url("https://graph.facebook.com/$id?access_token=$accessToken");

The function will return FALSE on failure.

I see that you said you've used the PHP SDK, but maybe you didn't have cURL set up. Try installing or updating it, and if it still seems to be slow, you should use

curl_setopt($ch, CURLOPT_HEADER, TRUE);curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

and check out the output.


I wondered what would happen if I did two subsequent curl_exec() calls without doing a curl_close(), enabling the use of HTTP Keep-Alive.

The test code:

$ch = curl_init('https://graph.facebook.com/xxx');curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// FIRST REQUESTcurl_exec($ch);print_r(curl_getinfo($ch));// SECOND REQUESTcurl_exec($ch);print_r(curl_getinfo($ch));curl_close($ch);

Below are the results, showing parts of the output from curl_getinfo():

// FIRST REQUEST[total_time] => 0.976259[namelookup_time] => 0.008271[connect_time] => 0.208543[pretransfer_time] => 0.715296// SECOND REQUEST[total_time] => 0.253083[namelookup_time] => 3.7E-5[connect_time] => 3.7E-5[pretransfer_time] => 3.9E-5

The first request is pretty slow, almost one whole second, similar to your experience. But from the time of the second request (only 0.25s) you can see how much difference the keep-alive made.

Your browser uses this technique as well of course, loading the page in a fresh instance of your browser would take considerably longer.


Just two thoughts:

  1. Have you verified that the browser doesn't have a presistent connection to facebook? That the browser hasn't cached the DNS lookup (you could try adding graph.facebook.net to your hosts-file to rule in/out DNS)

  2. You are of course running the php code from the same system/environment as your browser (not from a vm, not from another host? Also that php is running with the same scheduling priorties as your browser? (same nice level etc))