Fetching friends' birthdays from facebook prof

2019-03-31 05:07发布

问题:

I want to fetch 'birthdays' of users, and their friends on my website, from their facebook profiles (with their facebook credentials supplied).

Is their a feature in Facebook API/Connect that I can use to fetch these details from facebook as possible on Native Facebook Apps using Facebook API.

I want to store this data in my DB, and users will be asked for their facebook credentials and consent before this is done.

回答1:

Read the api documentation things like this are easily done. You can do it like this:

$facebook = new Facebook( $apikey, $secret );
$uid = $facebook->require_login();

$friends = $facebook->api_client->friends_get(); // $friends is an array holding the user ids of your friends

foreach( $friends as $f ) {
    $data = $facebook->api_client->fql_query( "SELECT birthday_date FROM user WHERE uid=$f" );
    // $data[0] is an array with 'birthday_date' => "02/29/1904"
    // see api documentation for other fields and do a print_r
}


回答2:

So recently I wanted to check my friends to see if any of them had their birthday for the current day. Using FQL this is super easy and I encourage you to explore FQL more because it will yield a more efficient solution than, say, what Pierre kindly offered. Here is a small snippet of the code:

$friends = $facebook->api_client->friends_get();
$uids = "";

foreach($friends as $f) {
  $uids .= "uid=$f OR ";
}

$query_uids = substr($uids,0,strlen($query_uids)-4);

date_default_timezone_set('UTC');
$current_date = date("m/d");
echo "<br />Searching for birthdays for the given month/day: $current_date<br />";

$data = $facebook->api_client->fql_query( "SELECT name, uid FROM user WHERE ( ($query_uids) AND strpos(birthday_date,'$current_date') >= 0 )" );

if(count($data) > 0) {
  foreach($data as $d) {
    print_r($d);
  }
} else {
 echo "<br />No Birthdays Today<br />";
 }


回答3:


require_once('facebook-platform/client/facebook.php');

$facebook = new Facebook(API_KEY, SECRET);
$facebook->require_login();

function getInfo($user_list, $fields) 
{
    try
    {
        $u = $facebook->api_client->users_getInfo($user_list, $fields);
        return $u;
    }
    catch (FacebookRestClientException $e)
    {
        echo $e->getCode() . ' ' . $e->getMessage();
    }
}

function getFriendsBirthdays($user_id) 
{
    $f = $_REQUEST['fb_sig_friends'];
    $f = explode(',', $f);
    $birthdays = array();
    foreach($f as $friend_id) 
    {
       $birthdays[] = getInfo($friend_id, 'birthday');
    }
    return $birthdays;
}

Do something like that or use the Batch API to do multiple calls at once. Check the Facebook API.



回答4:

You could fetch it via the API, but Facebook's terms strictly forbid you from storing anything other than their user ID in your database - see the developer wiki for details. You will need to query the API each time.