I'm testing facebook php sdk. I have permissions for 'user_birthday,email'and default user profile. Here is the code.
<?php
if(isset($session)) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me/'
))->execute()->getGraphObject(GraphUser::className());
var_dump ($user_profile);
echo $user_profile->getEmail();
$_SESSION['user']=$user_profile->getName();
$_SESSION['user_id']=$user_profile->getId();
$_SESSION['profile_pic']="http://graph.facebook.com/".$_SESSION['user_id']."/picture";
$_SESSION['gender']=$user_profile->getGender();
$vars = get_object_vars ( $user_profile->getBirthday() );
print_r ( $vars );
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
?>
And Output is
object(Facebook\GraphUser)[6]
protected 'backingData' =>
array (size=12)
'id' => string 'xxxxxxxxxxx' (length=15)
'birthday' => string '01/12/1990' (length=10)
'email' => string 'xxxxx@gmail.com' (length=19)
'first_name' => string 'xxxx' (length=10)
'gender' => string 'male' (length=4)
'last_name' => string 'xxxx' (length=4)
'link' => string 'https://www.facebook.com/app_scoped_user_id/xxxxxx/' (length=60)
'locale' => string 'en_US' (length=5)
'name' => string 'xxxxx xxxxx' (length=15)
'timezone' => int -8
'updated_time' => string '2015-02-22T07:48:23+0000' (length=24)
'verified' => boolean true
xxxxx@gmail.comArray ( [date] => 1993-04-22 00:00:00 [timezone_type] => 3 [timezone] => UTC )
Problem is,I can't use the birthday object. when I checked the birthday with echo,
echo $user_profile->getBirthday();
I got this error.
Catchable fatal error: Object of class DateTime could not be converted to string
when I dumped the $user_profile->getBirthday(); output is.
object(DateTime)[9]
public 'date' => string '1993-04-22 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
How can I get the Birthday as string? I'm new to php. Any help would be greatly appreciated.
Use the DateTime object's format method:
$user_profile->getBirthday()->format('m/d/Y');
See this page for help on constructing the preferred format string.