This post simple display How to read Facebook user profile information (Name, Email, Profile Picture, Locale, Timezone, Total Friends, etc) via PHP Graph API and FQL Query.
First, you need to create a new Facebook application (or use an exiting one) then replace appId and secret with your Facebook app id and app secret in the Initializing section (line 6,7). In this example, I place them in config.php file so you will need to update them in there.
Also in the config.php file, you will need to update your current Facebook app URL to ensure everything works properly.
PHP Load Facebook User Profile Information Via Graph API and FQL
<?php require 'src/facebook.php'; require 'config.php'; $facebook = new Facebook(array( 'appId' => $appId, 'secret' => $secret, )); $user_id = $facebook->getUser(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <div style="font-size:13px; color:#333333; line-height:24px;"> <?php if($user_id) { try { $user_profile = $facebook->api('/me','GET'); echo "<h1>1. User profile returned from Graph API</h1>"; echo "Profile ID: " . $user_profile['id'] . "<br>"; echo "Name: " . $user_profile['name'] . "<br>"; echo "Email: " . $user_profile['email'] . "<br>"; echo "Locale: " . $user_profile['locale'] . "<br>"; echo "Timezone: " . $user_profile['timezone'] . "<br>"; echo "<h3>All information in the user_profile array</h3>"; echo "<pre>"; var_dump($user_profile); echo "</pre>"; echo "<h1>2. User profile returned from FQL query</h1>"; $fql = 'SELECT name, username, pic_square, locale, email, friend_count from user where uid = ' . $user_id; $ret_obj = $facebook->api(array( 'method' => 'fql.query', 'query' => $fql, )); echo 'Name: ' . $ret_obj[0]['name'] . '<br>'; echo 'Username: ' . $ret_obj[0]['username'] . '<br>'; echo 'Profile picture: ' . $ret_obj[0]['pic_square'] . '<br>'; echo 'Email: ' . $ret_obj[0]['email'] . '<br>'; echo 'Locale: ' . $ret_obj[0]['locale'] . '<br>'; echo 'Total friends: ' . $ret_obj[0]['friend_count'] . '<br>'; } catch(FacebookApiException $e) { $login_url = $facebook->getLoginUrl(array( 'redirect_uri' => $facebook_app_url, 'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos, user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos")); echo "<script type='text/javascript'>top.location.href = '$login_url';</script>"; } } else { $login_url = $facebook->getLoginUrl(array( 'redirect_uri' => $facebook_app_url, 'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos, user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos")); echo "<script type='text/javascript'>top.location.href = '$login_url';</script>"; } ?> </div> </body> </html> |
Check out Facebook FQL user to see which information you may access.
+ View demonstration on my Facebook page
+ Download the PHP source code above as well as the current latest Facebook PHP SDK