2

I am struggling to understand how to make use of the paging mechanism with batch requests to Facebook's Graph API.

My issue with the code below is that despite attempting to track an offset for each facebook_id that is in my database, I'm unable to determine when a particular request has come up empty and therefore shouldn't be requested again.

I'm parsing the facebook_id's from the paging array in the responses. At the point when no more posts are available, FB returns an empty data array and therefore I'm unable to recognize which request has no more pages, and thus reduce the subsequent requests. Each time I'm querying FB with $fb_batch_limit urls despite the fact not all requests have returned a paging token from the previous call.

How would a real programmer tackle this problem?

    $posts = array();

    //$fb_batch_limit = 30;
    $fb_batch_limit = 2;
    $fb_req_limit=10;

    $facebook = new Facebook(array(
            'appId'  => $fb_app_id,
            'secret' => $fb_app_secret
    ));
    $facebook->setAccessToken($fb_user_token);
    $index = 0;
    $since = '1Jan14';
    $until = 'today';

    $num_records_res = $oDB->select("SELECT count(*) as count FROM facebook f");
    $num_records_row = mysqli_fetch_array($num_records_res);
    $num_records = $num_records_row['count'];
    while($index < $num_records) {
            printf("# %d to %d\n", $index, $index+$fb_batch_limit);
            $res = $oDB->select("SELECT facebook_id FROM facebook f ".
                                "LIMIT $index, $fb_batch_limit");
            $index = $index + $fb_batch_limit;
            $offsets = array();
            while($rr = mysqli_fetch_array($res)){
                    $offsets[$rr['facebook_id']] = 0;
            }
            if (!count($offsets)) {
                    printf("no more accounts\n");
                    break;
            }

            $r=1;
            do {
                    $urls = array();
                    foreach ($offsets as $fbid => $offset) {
                            $tmp["method"] = 'GET';
                            $tmp["relative_url"] = "/".$fbid."/feed?fields=likes.limit(1).summary(true)&offset=$offset&limit=$fb_req_limit";//&since=$since&until=$until";
                            $tmp["include_headers"] = false;
                            $urls[] = $tmp;
                    }
                    $numurls = count($urls);
                    printf("$r: fetching %d urls have %d posts\n", $numurls, count($posts));
                    $response = $facebook->batch($urls);
                    foreach ($response as $result) {
                            if (array_key_exists("error", $result)) {
                                    //FIXME handle rate limits somehow
                                    print_r($result);
                                    die;
                            }
                            if (array_key_exists("paging", $result)) {
                                    $fbid = substr(parse_url($result['paging']['previous'])['path'],6);
                                    $fbid = substr($fbid, 0, -5);

                                    if (($count = count($result['data'])) > 0) {
                                            $posts = array_merge($posts, $result['data']);
                                            if (array_key_exists("paging", $result) && array_key_exists("next", $result["paging"])) {
                                                    if (array_key_exists($fbid, $offsets)) {
                                                            $offsets[$fbid] += $count;
                                                    }
                                            }
                                    }
                                    else {
                                            printf("$fbid complete with ". 
                                                     $offsets[$fbid]." posts\n");
                                            unset($offsets[$fbid]);
                                    }
                            }
                            else {
                                    // how to stop requesting this single url?
                                    print_r($result);
                                    echo "--\n";
                                    $numurls--;
                            }
                    }
            } while (++$r && $numurls);
    }

0

Your Answer

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

Browse other questions tagged or ask your own question.