dongmaopiao0901 2014-02-19 01:00
浏览 63
已采纳

Facebook名称是否可以使用Facebook API公开访问

Are the names of profiles on Facebook publicly accessible, as if I would NOT need to log into Facebook to access them?

I am intending to store a large amount of names as a small piece of a larger project. I feel as if scraping Facebook for names would be a relatively simple task using the Facebook Graph API, but I am a little confused.

I found another tutorial online at http://jilltxt.net/?p=2810 which described an easy way of finding any Facebook profile picture using one simple line:

https://graph.facebook.com/USER-ID/picture?type=large

This was very helpful because I am able to use a range of ID numbers and a small amount of PHP to gather large amounts of profile pictures as seen on my test page here: http://www.joshiefishbein.com/fi/photobook.php

But what I am unfamiliar with is how I go from collecting pictures to names in this one simple line. Is it possible? Is there another (better) way?

Here's the code I am working with. The range of ID's are just an example.

function gen_pix($min, $max, $quantity) {
    $numbers = range($min, $max);
    shuffle($numbers);
    $x_arr = array_slice($numbers, 0, $quantity);
    foreach ($x_arr as $key => $value) {
        $username = "https://graph.facebook.com/" . $value . "/";
        $json = json_decode(file_get_contents($username), true);
        echo $json["name"];
    }
}

$x = 337800042;
$y = 337800382;
$z = 1;

gen_pix($x,$y,$z);

I've gotten a little farther with this code, I can echo $username and I get the URL that I am looking for (for example https://graph.facebook.com/337800382/) but I do not get anything after that. json_decode isn't working seemingly.

  • 写回答

1条回答 默认 最新

  • dongyuying1507 2014-02-19 09:15
    关注

    In the same way you are pulling the profile picture, you can get the basic information of a user with their ID.

    This page provides a list of data that is always publicly accessible.

    So you need to make a GET request to pull back the JSON, like so...

    https://graph.facebook.com/{user-id}/

    For example https://graph.facebook.com/586207189/ pulls back my basic information. So your PHP would look like this

    $json = json_decode(file_get_contents("https://graph.facebook.com/$user_id/"), true);
    echo $json["name"];
    

    PHP fiddle here

    Update: Based on the code above, it's worth adding an IF to catch invalid Facebook IDs. Facebook IDs may not be sequential so not every one will return a name or image.

    Updated code:

    <?php
    function gen_pix($min, $max, $quantity) {
        $numbers = range($min, $max);
        shuffle($numbers);
        $x_arr = array_slice($numbers, 0, $quantity);
        foreach ($x_arr as $key => $value) {
            $username = "https://graph.facebook.com/" . $value . "/";
            $json = json_decode(file_get_contents($username), true);
            if (!isset($json['name'])) {
                echo "Invalid ID<br />";
            }
            else {
                echo $json["name"]. '<br />';
            } 
        }
    }
    
    $x = 337800042;
    $y = 337800382;
    $z = 50;
    
    gen_pix($x,$y,$z);  
    ?>
    

    PHP Fiddle here

    It's also worth noting that pulling that much data from the graph is going to take a while. Have a look at doing batch requests to speed things up a bit. More info here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历