How to Get Username from Facebook Id

How to get username from Facebook ID

If you already have an ID of that particular user, then just add it on this url:

https://graph.facebook.com/<USER_ID>

Simple example:

function get_basic_info($id) {
$url = 'https://graph.facebook.com/' . $id;
$info = json_decode(file_get_contents($url), true);
return $info;
}

$id = 4;
$user = get_basic_info($id);
echo '<pre>';
print_r($user);

This should basically yield:

Array
(
[id] => 4
[first_name] => Mark
[gender] => male
[last_name] => Zuckerberg
[link] => https://www.facebook.com/zuck
[locale] => en_US
[name] => Mark Zuckerberg
[username] => zuck
)

Then you could just call it like a normal array:

echo $user['username'];

Sidenote: Why not use the PHP SDK instead.

https://developers.facebook.com/docs/reference/php/4.0.0

How to get user's facebook username or id?

Looks like you can retrieve the user_link and other permissions through the facebook api, it still requires App Review.

Best Way To Find a Username From a Facebook ID

You could do something similar to this when setting up the friend picker controller:

// Initialize the friend picker
FBFriendPickerViewController *friendPickerController =
[[FBFriendPickerViewController alloc] init];
...
// Ask for friend device data
friendPickerController.fieldsForRequest =
[NSSet setWithObjects:@"username", nil];

Then when the friend picker data is returned, the username info should be returned as well.

You can implement the friend picker delegate method for a roundabout way of getting the username info. Implement: friendPickerViewController:shouldIncludeUser:

In this method, you can examine the returned data that should include the username:

- (BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker
shouldIncludeUser:(id<FBGraphUserExtraFields>)user
{
NSString *username = user.username;
// Process the username however you wish to.
return YES;
}

For a similar ask, in this case looking for "device" info see the doc: https://developers.facebook.com/docs/howtos/filter-devices-friend-selector-using-ios-sdk/

Facebook App: Getting a user's name from ID

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'


Related Topics



Leave a reply



Submit