且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Facebook API:获取喜欢页面的粉丝/人

更新时间:2023-12-02 16:48:52

有一种方法"可以在没有令牌的情况下使用某些粉丝页面的个人资料 ID 获取粉丝列表的某些部分.

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

  1. 使用公共图形数据获取粉丝页面的 id:http://graph.facebook.com/cocacola - Coca-Cola 有 40796308305. UPDATE 2016.04.30:Facebook 现在需要访问令牌才能通过图形获取 page_id,因此您可以解析粉丝页面 HTML 语法来获取此 ID,而无需来自 https://www.facebook.com/{PAGENAME} 的任何授权,如下例所示,基于粉丝页面上的 og 标签.
  2. 使用一些修改后的参数直接获取可口可乐的like plugin"iframe 显示:http://www.facebook.com/plugins/fan.php?connections=100&id=40796308305
  3. 现在检查页面来源,有很多粉丝带有指向他们个人资料的链接,您可以在其中找到他们的个人资料 ID 或昵称,例如:http://www.facebook.com/michal.semeniuk.
  4. 如果您只对个人资料 ID 感兴趣,请再次使用图形 API - 它会直接为您提供个人资料 ID:http://graph.facebook.com/michal.semeniuk 2016.04.30 更新:Facebook 现在需要访问令牌才能获取此类信息.您可以解析配置文件 HTML 语法,就像在第一步中元标记是您***的朋友一样:<meta property="al:android:url" content="fb://profile/{PROFILE_ID}"/>
  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}" />

现在是***的部分:尝试刷新 (F5) 第 2 点中的链接.. 有一个新的全套可口可乐粉丝.只选择独特的人,您将能够获得一些不错的、几乎完整的粉丝列表.

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.

你为什么不使用我准备好的 PHP 脚本来获取一些粉丝?:)

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

UPDATE 2016.04.30:在 Facebook 开始要求访问令牌以从图形 API 获取公共数据后,更新了示例脚本以使用新方法.

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
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-encoding: gzip, deflate, sdch
Accept-language: en-US,en;q=0.8,pl;q=0.6
')));
    // 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));