Haru
Haru

Reputation: 1381

Facebook Graph API Slow for Larger Data Calls

This is what I am doing in PHP to get access to a bunch of group posts on Facebook. I'm then implementing a search function to search these posts.

        $url2 = 'https://graph.facebook.com/'. $group_id . '/feed' . '?limit=30&access_token=' . $_SESSION['access_token'] ;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "$url2");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $json = curl_exec($ch);
        $data = json_decode($json, TRUE);

So I broke it down and just found that putting the graph url into my browser yields a slow response. Limit set to 30 is ok, but up it to 300 and it is slow, up it to 1,000 and it crawls.

I've looked into paging but I would like to grab a large amount of data so I can search it. Caching really wouldn't work because it still takes so long to load that initial data.

Is there anyway to speed this up or am I stuck at the limitation of the Facebook Graph API?

Upvotes: 3

Views: 1447

Answers (2)

Jessie Frazelle
Jessie Frazelle

Reputation: 635

You could batch your requests so that you only set the curl once for the batched request, instead of looking through numerous curls http://developers.facebook.com/blog/post/2011/03/17/batch-requests-in-graph-api/

    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET"; 
    $my_url = "YOUR_URL";

    $code = $_REQUEST["code"];

    if(empty($code)) {
      $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url);

      echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code;

    $access_token = file_get_contents($token_url);
    $batched_request = '[{"method":"GET","relative_url":"me"},' . '{"method":"GET","relative_url":"me/friends?limit=50"}]';

    $post_url = "https://graph.facebook.com/" . "?batch=" . $batched_request . "&access_token=" . $access_token . "&method=post";
    echo $post_url;

    $post = file_get_contents($post_url);
    echo '<p>Response: <pre>' . $post . '</pre></p>';

Upvotes: 7

kittycat
kittycat

Reputation: 15045

You can use CURLOPT_ENCODING per the documentation:

The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent.

This way cURL will be telling Facebook, "hey I understand compressed data, please send me compressed data".

$url2 = 'https://graph.facebook.com/'. $group_id . '/feed' . '?limit=30&access_token=' . $_SESSION['access_token'] ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, ''); // enable compression, keep empty to send all supported types
$json = curl_exec($ch);
$data = json_decode($json, TRUE);

Upvotes: 1

Related Questions