phpfacebookfacebook-graph-apifacebook-likefacebook-page

get likers of Facebook page and get count of those who like more than 200 pages


I'm trying to get a "list" of random likers who follow a Facebook Page. I'm using this code to get some fans (not random fans, but this is something else).

<?php
function fetch_fb_fans($fanpage_name, $no_of_retries = 10, $pause = 500000){
    $ret = array();
    /* get page info from graph */
    $fanpage_data = json_decode(file_get_contents('http://graph.facebook.com/' . $fanpage_name), true);
    if(empty($fanpage_data['id'])){
        /* invalid fanpage name */
        return $ret;
    }
    $matches = array();
    $url = 'http://www.facebook.com/plugins/fan.php?connections=100&id=' . $fanpage_data['id'];
    $context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0')));
    for($a = 0; $a < $no_of_retries; $a++){
        $like_html = file_get_contents($url, false, $context);
        preg_match_all('{href="https?://www\.facebook\.com/([a-zA-Z0-9._-]+)" data-jsid="anchor" target="_blank"}', $like_html, $matches);
        if(empty($matches[1])){
            /* failed to fetch any fans - convert returning array, cause it might be not empty */
            return array_keys($ret);
        }else{
            // merge profiles as array keys so they will stay unique
            $ret = array_merge($ret, array_flip($matches[1]));
        }
        // don't get banned as flooder
        usleep($pause);
    }
    return array_keys($ret);
}
/*
print_r(fetch_fb_fans('cocacola', 2, 400000));
prints 73 unique fan names as array
*/

$contador = 0;
foreach (fetch_fb_fans('cocacola', 2, 400000) as $fan) {


    $pageContent = file_get_contents('http://graph.facebook.com/'.$fan.'');
    $parsedJson  = json_decode($pageContent);
    echo $parsedJson->username ."<br/>";
}
?>

Code from: Facebook API: Get fans of / people who like a page

This code give me some usernames. Now, my question, after searching Google... Is, can I get the number of pages that follow every user?

I know that Graph API let me know my likes but when I try to see other user likes it throws me an OAuthException error. I supose that I'm not doing right. So I will apreciate some explanation about how to do this. I searched Google but I don't understand how it works.

Thanks.


Solution

  • The Facebook documentation is unfortunately not very clear: https://developers.facebook.com/docs/graph-api/reference/v2.2/user

    However, getting the likes from a user requires:

    Without an access token for the user you cannot see what pages they like.

    While not supported, you could perhaps use a page scraper to find this information if they have it public.

    Based on your question, it's not clear whether users log in to your app or if you're just trying to get information from one of your own pages, or another page. If you don't have users logging into your app, I'm afraid there's no way at all to get this information apart from a page scraper.