Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook App: Getting a user's name from ID

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?

like image 710
ryan Avatar asked Jun 06 '12 15:06

ryan


2 Answers

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'
like image 117
phwd Avatar answered Sep 30 '22 11:09

phwd


try using the php graph api like this:

$facebook = new Facebook(array(
   'appId'  => "your appID",
   'secret' => "your app secret",
   'cookie' => true,
));

$answer = $facebook->api("/{$row[user_id]}");
$username = $answer['name'];
print $username;
like image 22
Luca S. Avatar answered Sep 30 '22 11:09

Luca S.