Facebook App: Getting a user's name from ID

ryan picture ryan · Jun 6, 2012 · Viewed 35.6k times · Source

I just had a FB app project turned over to me, and I'm trying to clean up a few items before it goes live.

The original programmer has the user ID number stored in the database, but doesn't display it or the name. I'd like to display the user's name under the picture they upload.

My code is <?php echo $row['user_id']; ?>, is there a simple way to convert that into the user's name?

Answer

phwd picture phwd · Jun 6, 2012

Call the id from the API with the name field.

 https://graph.facebook.com/user_id?fields=name

It should return

{
   "name": "First Lastname",
   "id": "user_id"
}

Save that to a variable and then extract

 $user['name'];

For more information http://developers.facebook.com/docs/reference/api/user/

An example without the SDK/access token,

$response = file_get_contents("https://graph.facebook.com/4?fields=name");  
$user = json_decode($reponse,true);  
echo $user['name'];  

Should print,

'Mark Zuckerberg'