3

I'm trying to make a website using PHP with some Facebook features. I'd like to know if there's a way of getting the Facebook user ID from a user and return the name and surname of the user?

3 Answers 3

7

PHPRocker and Jimmy are both right, but if all you need is the data you mentioned, you can use:

http://graph.facebook.com/{ID}

Where you replace {ID} at the end with anything Facebook object ID you desire... For instance: http://graph.facebook.com/1157251270

1
  • Thank you all. Nuby, that was exactly what I was looking for. It's not suppose to log in, the user just has to post the id and it returns the name. Thank you all!
    – John Doe
    Apr 25, 2011 at 22:36
1

You can use the graph API to get the data of a user. The extent of the data that you can get depends on the the level of authorization you ask for when

1

You need to authenticate the user in order to get a user ID, and you get the name for free with the same level of permissions. Alternatively, if you already know the user ID (by downloading installed users' friends' lists or something), you can also get their name by downloading and parsing https://graph.facebook.com/<uid>.

Using the PHP SDK, some basic code that authenticates a user would look like:

<?php
$fb = new Facebook(array(
    'appId' => APP_ID,
    'secret' => APP_SECRET
));

$session = $fb->getSession();

if (!$session)
{
    // if not authed, redirect to auth page
    header("Location: ".$fb->getLoginUrl());
}

$uid = $session['uid'];

echo "Your user ID is {$uid}";

You may also choose to do this using the Javascript SDK, depending on the UX you're going for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.