Avinash
Avinash

Reputation: 2005

Get birthdays of all facebook friends in php

My Code

 require 'src/facebook.php';
 $app_id = 'My App Id';
 $app_secret = 'My App Secret Id';
 $facebook = new Facebook(array(
'appId'  => $app_id,
'secret' => $app_secret,
'cookie' => true,
 ));
 $user = $facebook->getUser();
 if ($user) {    
    $user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
 }
 if ($user) {
 $params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
 $logoutUrl = $facebook->getLogoutUrl($params);
 } else {
 $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'user_photos'
));
 }

What I require

The above code only provides friends id and name but not birthdays. I have done some researching and was unsuccessful in finding a solution.

Upvotes: 1

Views: 1314

Answers (1)

Martin Metselaar
Martin Metselaar

Reputation: 356

You probably need to request permission of the user to access the birthday information of their friends. The Facebook permission you are looking for is 'friends_birthday'.

Request the user their access token with the above permission and set the access token.

$facebook = new Facebook(array(
   'appId'  => $app_id,
   'secret' => $app_secret,
   'cookie' => true,
   )
);
$facebook->setAccessToken($userAccessToken);
$user = $facebook->getUser();

if ($user) {    
   $user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}

if ($user) {
   $params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
   $logoutUrl = $facebook->getLogoutUrl($params);
} else {
   $loginUrl = $facebook->getLoginUrl(array(
      'scope' => 'user_photos, friends_birthday'
   ));
}

Actually I think you just have to add friends_birthday to the scope.

Upvotes: 2

Related Questions