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

171,035

Solution 1

There is a "way" to get some part of fan list with their profile ids of some fanpage without token.

  1. Get id of a fanpage with public graph data: http://graph.facebook.com/cocacola - Coca-Cola has 40796308305. UPDATE 2016.04.30: Facebook now requires access token to get page_id through graph so you can parse fanpage HTML syntax to get this id without any authorization from https://www.facebook.com/{PAGENAME} as in example below based on og tags present on the fanpage.
  2. Get Coca-Cola's "like plugin" iframe display directly with some modified params: http://www.facebook.com/plugins/fan.php?connections=100&id=40796308305
  3. Now check the page sources, there are a lot of fans with links to their profiles, where you can find their profile ids or nicknames like: http://www.facebook.com/michal.semeniuk .
  4. If you are interested only in profile ids use the graph api again - it will give you profile id directly: http://graph.facebook.com/michal.semeniuk UPDATE 2016.04.30: Facebook now requires access token to get such info. You can parse profile HTML syntax, just like in the first step meta tag is your best friend: <meta property="al:android:url" content="fb://profile/{PROFILE_ID}" />

And now is the best part: try to refresh (F5) the link in point 2.. There is a new full set of another fans of Coca-Cola. Take only uniques and you will be able to get some nice, almost full list of fans.

-- UPDATE 2013.08.06 --

Why don't you use my ready PHP script to fetch some fans? :)

UPDATE 2016.04.30: Updated example script to use new methods after Facebook started to require access token to get public data from graph api.

function fetch_fb_fans($fanpage_name, $no_of_retries = 10, $pause = 500000 /* 500ms */){
    $ret = array();
    // prepare real like user agent and accept headers
    $context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-encoding: gzip, deflate, sdch\r\nAccept-language: en-US,en;q=0.8,pl;q=0.6\r\n')));
    // get page id from facebook html og tags for mobile apps
    $fanpage_html = file_get_contents('https://www.facebook.com/' . $fanpage_name, false, $context);
    if(!preg_match('{fb://page/(\d+)}', $fanpage_html, $id_matches)){
        // invalid fanpage name
        return $ret;
    }
    $url = 'http://www.facebook.com/plugins/fan.php?connections=100&id=' . $id_matches[1];
    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\._-]+)" class="link" 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('TigerPolska', 2, 400000));

Solution 2

You can get fans using new facebook search: https://www.facebook.com/search/321770180859/likers?ref=about

Solution 3

Use this.

https://www.facebook.com/browse/?type=page_fans&page_id=<your page id>

It will return up to 500 of the most recent likes.

http://www.facebook.com/browse/?type=page_fans&page_id=<your page id>&start=400

Each page will give you 100 fans. Change start value to (0, 100, 200, 300, 400) to get the first 500. If start is >= 401, the page will be blank :(

Solution 4

According to the Facebook documentation it's not possible to get all the fans of a page:

Although you can't get a list of all the fans of a Facebook Page, you can find out whether a specific person has liked a Page.

Solution 5

For s3m3n's answer, Facebook fans plugin (e.g. LAMODA) has limitation now, you get less and less new fans on continuous requests. You may try my modified PHP script to visualize results: https://gist.github.com/liruqi/7f425bd570fa8a7c73be#file-facebook_fans_by_plugin-php

Another approach is Facebook graph search. On search result page: People who like pages named "Lamoda" , open Chrome console and run JavaScript:

var run = 0;
var mails = {}
total = 3000; //滚动次数,可以自己根据情况定义

function getEmails (cont) {
    var friendbutton=cont.getElementsByClassName("_ohe");
    for(var i=0; i<friendbutton.length; i++) {
        var link = friendbutton[i].getAttribute("href");

        if(link && link.substr(0,25)=="https://www.facebook.com/") {
            var parser = document.createElement('a');
            parser.href = link;
            if (parser.pathname) {
                path = parser.pathname.substr(1);
                if (path == "profile.php") {
                    search = parser.search.substr(1);
                    var args = search.split('&');
                    email = args[0].split('=')[1] + "@facebook.com\n";
                } else {
                    email = parser.pathname.substr(1) + "@facebook.com\n";
                }
                if (mails[email] > 0) {
                    continue;
                }
                mails[email] = 1;
                console.log(email);
            }
        }
    }
}

function moreScroll() {
    var text="";
    containerID = "BrowseResultsContainer"
    if (run > 0) {
        containerID = "fbBrowseScrollingPagerContainer" + (run-1);
    }
    var cont = document.getElementById(containerID);
    if (cont) {
        run++;
        var id = run - 2;
        if (id >= 0) {
            setTimeout(function() {
                containerID = "fbBrowseScrollingPagerContainer" + (id);
                var delcont = document.getElementById(containerID);
                if (delcont) {
                getEmails(delcont);
                delcont.parentNode.removeChild(delcont);
                }
                window.scrollTo(0, document.body.scrollHeight - 10);
            }, 1000);
        }
    } else {
        console.log("# " + containerID);
    }
    if (run < total) {
        window.scrollTo(0, document.body.scrollHeight + 10);
    }
    setTimeout(moreScroll, 2000);
}//1000为间隔时间,也可以根据情况定义

moreScroll();

It would load new fans and print user id/email, remove old DOM nodes to avoid page crash. You may find this script here

Share:
171,035
pesho
Author by

pesho

Updated on July 05, 2022

Comments

  • pesho
    pesho almost 2 years

    I'd like to get a list of users who like a certain page or a fan of it.

    The FB API documentation states that you can only get the count of the fans of a certain page using the social graph, but not a list of the fans.

    A discussion here Retrieve Facebook Fan Names suggests that one could use an FQL query like SELECT user_id FROM like WHERE object_id="YOUR PAGE ID" to get the number of people who liked the page, but for the same page, it gives an empty response "{}".

    So I was wondering if anyone has an idea if this can be done.

  • Sanket
    Sanket almost 12 years
    How to get it through Graph api or fql ? I guess there is no way we can get profile name who liked the page.
  • James P.
    James P. over 11 years
    Where did you find out about this like plugin ?
  • s3m3n
    s3m3n over 11 years
    @JamesPoulson it's official FB like plugin to embed on external webpages, there is even generator: developers.facebook.com/docs/reference/plugins/like-box
  • Matical
    Matical about 11 years
    facebook.com/plugins/fan.php?connections=100&id=40796308305 i think they set the limit to 100 instead of allowing to grab 10000 users at a time...
  • s3m3n
    s3m3n almost 11 years
    Why don't you try my ready php script for fetching fb fans without authorization?
  • High schooler
    High schooler over 10 years
    Your script is very interesting but does it not violate the FB TOS?
  • s3m3n
    s3m3n over 10 years
    @KingofGames not sure about it. Stackoverflow examples are for education purposes, I'm not using this script on my production servers so I haven't checked TOS.
  • Hayko Koryun
    Hayko Koryun over 10 years
    Looks like the selection of people who appear in the facepile isn't truly random anymore.
  • Marcel
    Marcel over 10 years
    I have a page with 56 likes, when running your script it only returns 21, I need to grab all 56 users, and I don't know why it would not show them all. When opening fan.php page it show everyone, even in incognito mode, but your php script don't. Any thoughts?
  • redice
    redice over 10 years
    No need to use the graph api. Profile id has already existed in the small profile image path.
  • JohnnyM
    JohnnyM over 10 years
    so is this legal according to facebook TOS?
  • verbumSapienti
    verbumSapienti about 10 years
    for some reason the maximum amount of unique fans is limited to 214. this is from a bank of 15470 fans. the percentage does not appear to be a whole number. I have mined the fan page 1000 times, producing 100000 fans (expecting to get somewhere close to 15000 uniques), but of those only 214 are unique. I wonder why this is?
  • verbumSapienti
    verbumSapienti about 10 years
    I just repeated the process for Coca Cola and out of 100000 fans only 367 were unique. whatever is going on at Facebook's end, this is a pretty useless ratio.
  • Oleg
    Oleg almost 10 years
    Could you please provide some details?
  • user1007522
    user1007522 almost 10 years
    I just tried your script. It doesn't work the empty matches is always. True.
  • s3m3n
    s3m3n almost 10 years
    You are right, FB changed default url to user profile, now it starts with https protocol instead of http. Updated my answer to fetch both protocols and it works again.
  • benchpresser
    benchpresser over 9 years
    you will not get likers if their privacy setting is set to friends of mine or private even if you are admin of the page. with this method you will only get public likes. (same problem if you login to fb with admin account you wont get likers if security level is set to friends. you should be friend of the liker to see the info, being admin of the page is not enough. In the past it was possible, admin could see all likes even private)
  • Maxim Mazurok
    Maxim Mazurok almost 9 years
    @verbumSapienti this answer is totally valid as of June '15
  • Maxim Mazurok
    Maxim Mazurok almost 9 years
    @jirungaray Yes. You should replace "3217701..." from original URL in answer to your page ID which you can find out here: findmyfacebookid.com. Also this feature may be missing on mobile version, so try it from a desktop.
  • code-8
    code-8 over 8 years
    Can we embed that in an iFrame ?
  • code-8
    code-8 over 8 years
    Can we embed that in an iFrame ?
  • Oleg
    Oleg over 8 years
    Did you try? Any issues?
  • code-8
    code-8 over 8 years
    Function kept returning file_get_contents(http://graph.facebook.com/cindypizzaplace)‌​: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
  • Mohammad Sadiq
    Mohammad Sadiq about 8 years
    Function kept returning file_get_contents(graph.facebook.com/cindypizzaplace): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
  • s3m3n
    s3m3n about 8 years
    Thanks, updated the answer with new solution. FB now requires access token to get anything from Graph API.
  • Bevor
    Bevor over 7 years
    Page is not found
  • Noah
    Noah over 7 years
    Where do you use graph api in your code? I dont see any.
  • Noah
    Noah over 7 years
    Max is 10,000 now.
  • Noah
    Noah over 7 years
    This doesn't give you all the fans. I think the max is 10K. The reason for that is that your browser cannot handle more than that.
  • Carmela
    Carmela over 7 years
    Still works at 2016. Just make sure you are logged in and you are connected to the related FB page (e.g. Admin, Editor, Advertiser)
  • Noah
    Noah over 7 years
    @Carmela But it won't give you all the fans.
  • Carmela
    Carmela over 7 years
    @Noah yes, it won't. That's too many to give in one list only. Logically, it really has to be chopped.
  • Murat Kucukosman
    Murat Kucukosman over 7 years
    @Bevor 5 years ago it was there. But now Facebook Like Query deprecated
  • Hammad Khan
    Hammad Khan over 6 years
    The links point to "People who like HITEC", not relevent to the answer anymore
  • Portekoi
    Portekoi over 6 years
    I get only 1 to 6 results. No more :(
  • Anoyz
    Anoyz about 6 years
    not working anymore? I guess you have to own the page
  • Omid Ariyan
    Omid Ariyan over 5 years
    I don't know if there's another way to get your page id, but I found this page for that: findmyfbid.com