8

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?

balupton
  • 47,113
  • 32
  • 131
  • 182
ryan
  • 83
  • 1
  • 1
  • 5

2 Answers2

17

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'
phwd
  • 19,975
  • 5
  • 50
  • 78
  • Do I need to define that in a config file or facebook.php? Appologies for the noob questions, I'm new to FB development. – ryan Jun 06 '12 at 16:15
  • @ryan No not necessarily as defined in the docs mentioned above no access token is required for id,name. So you can roll your own logic to receive the JSON response from that call. – phwd Jun 06 '12 at 16:31
  • Great dude. This did the trick for me https://graph.facebook.com/4?fields=username – Nirav Gadhiya Dec 04 '13 at 10:59
  • 2
    { "error": { "message": "Unsupported get request.", "type": "GraphMethodException", "code": 100 } } – R.Alvin Jan 31 '14 at 07:03
  • 7
    Looks like this is deprecated. – bumpkin Jun 09 '15 at 03:08
1

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;
Luca S.
  • 94
  • 1
  • 9