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

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

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?