3

On the Internet there are a few topics about this problem, but I have not found any complex solution. Therefore, I would like to ask you for help.

I need to change facebook id to username.

When you type web site like this:

http://facebook.com/profile.php?id=4 (num 4 is FB id), it will give you http://www.facebook.com/zuck, which is Mark Zuckerberg's profile.

On this principle I need to find out who a id is.

I have typed id 4 a got it is zuck.

But I need it for more ids, so it would take a lot of time do it manually. Please help me, how I can do it.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
Andrew
  • 163
  • 2
  • 3
  • 10

3 Answers3

3

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

Kevin
  • 41,694
  • 12
  • 53
  • 70
3

As the username is NO more available from Graph API endpoint /user-id as discussed here, I propose another workaround here (but with Python code)

In brief, we open the page at fb.com/USER_ID and scrape the username from it

#get html of a page via pure python ref. https://stackoverflow.com/a/23565355/248616
import requests
r = requests.get('http://fb.com/%s' % FB_USER_ID) #open profile page of the facebook user
r.raise_for_status()
html = r.content

#search string with regex ref. https://stackoverflow.com/a/4667014/248616
import re
# m = re.search('meta http-equiv="refresh" content="0; URL=/([^?]+)\?', html)
m = re.search('a class="profileLink" href="([^"]+)"', html)
href = m.group(1) #will be https://www.facebook.com/$FB_USER_NAME on 201705.24
username = href.split('/')[-1]
print(href)
print(username)
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
  • scraping is not allowed on facebook, don´t do that. there is no situation where you would need the real id or the username anymore anyway. – andyrandy Mar 22 '18 at 07:57
  • @luschn My app needs to create a profile URL link for the FB user...that seems like a real use case to me. – Learner Dec 16 '18 at 00:39
  • "my app needs it" is not a use case ;) - why exactly would your app need it? why would people need to go to a profile url of facebook in your app? - btw, the api reference says that there is a "link" field for users. did you try this? – andyrandy Dec 16 '18 at 17:50
  • @luschn link field is also deprecated I suppose? – Alwaysblue Oct 11 '19 at 09:22
  • I am not sure, I just tried it using graph API explored and didn't got user profile link – Alwaysblue Oct 11 '19 at 09:58
  • Also, There can be various situation where you would need the real id or username. Like I want to create a Dating App just like facebook where you would get a match if both the users have added themselves. Typically user would add a username of the people who they want to date and if two user have added each other username, then there would be a match – Alwaysblue Oct 11 '19 at 10:05
2

As of October 2019, The only way it is possible is to request for user_links in request and permissions

enter image description here

And then pass it in a scope, something like this

scope: ['user_link'] 
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210