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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端