简体   繁体   中英

How to check how many of my friends like particular page on facebook using PHP SDK

I'm workin on an app that would award its users points for each of their friends liking my page.

I need a way to check how many of user's friends like my page. I developed this piece of code to check each my friends for liking a particular page:

    $friends = $facebook->api('/me/friends'); //list my friends
    $time_start = microtime(true);
    echo "<pre>";
    $i = 0; //counter of likes
    $b = 0; //counter of request in single batch API request
    $batch_max = 50 // facebook allows max 50 requests in single batch API call
    $page_id = '187941397895831'; //example page ID, i'm checking how many of my    friends like this page
    $batch = array(); //array used for creating batch API request
    $data = array(); //array collecting all API requests results
    foreach ($friends['data'] as $friend) {
        $req = array(
            'method' => 'GET',
            'relative_url' => '/'.$friend['id'].'/likes'
        );
        $batch[] = json_encode($req);
        $b++;
        if ($b == 50) {
            $params = array(
                'batch' => '[' . implode(',',$batch) . ']'
            );
            $data[] = $facebook->api('/', 'POST', $params);
            $b = 0;
            $batch = array();
        }
    }
    $params = array(
        'batch' => '[' . implode(',',$batch) . ']'
    );
    $data[] = $facebook->api('/', 'POST', $params);
    foreach ($data as $data_set) { //iterate through results of each api request
        foreach ($data_set as $friend_likes) { //iterate through data of particular friend
            $likes_array = json_decode($friend_likes['body'], true);
            foreach ($likes_array['data'] as $single_like) { //iterate through all likes of a single friend
                if ($single_like['id'] == $page_id) { //if page id found in user's likes increase counter
                    $i++;
                }
            }
        }
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo $time."\n";
    echo $i;
    echo "</pre>";
    exit;

In final version I will not be checking my friends but friends of each of my users. My code executes in 14 seconds, if I had to iterate through, let's say, 100 users it'd be 1400 seconds, which is waaaay to long. Is there better way to do that? I'm newbie to facebook API, so I could miss something obvious :)

Ok, so here's the working piece of code. You need to know access token for each of users being checked. It executes in ~0.6 seconds instead of 14 seconds for previous code.

$queryResults = array();
$facebook->setAccessToken($user->getAccessToken());
$fql = "SELECT '' FROM page_fan WHERE page_id = 'xxxxxxxxxxxx' AND uid IN (SELECT uid2 FROM friend WHERE uid1 = '".$user->getFbId()."')";
$fql = urlencode($fql);

try {
    $queryResults = $facebook->api(
        '/fql?q='.$fql
    );
} catch (FacebookApiException $e) {
    $log->setMessage(print_r($e,true));
    $logMapper->save($log);
}

if (!empty($queryResults)) {
    $user->setPoints(count($queryResults['data']));
    $usersMapper->save($user);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM