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

2019-01-01 14:31发布

问题:

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.

回答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));


回答2:

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



回答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 :(



回答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.



回答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



回答6:

This page https://developers.facebook.com/docs/reference/fql/like/ wrote, you can\'t get fan list.

\"The Post, Video, Note, Link, Photo and Album Graph API objects contain an equivalent connection called likes.\"

NOTE: fql like query is deprecated



回答7:

Technically this FQL query should work, but for some reason Facebook disallows it because of a missing index. Not sure if that is because of policy or they just forgot.

SELECT uid FROM page_fans WHERE page_id=\"YOUR_PAGE_ID\"


回答8:

Facebook\'s FQL documentation here tells you how to do it. Run the example SELECT name, fan_count FROM page WHERE page_id = 19292868552 and replace the page_id number with your page\'s id number and it will return the page name and the fan count.