php 如何从 Facebook ID 获取用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26643037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:59:07  来源:igfitidea点击:

How to get username from Facebook ID

phpfacebookfacebook-graph-api

提问by Andrew

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.

我需要将 facebook id 更改为用户名。

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.

http://facebook.com/profile.php?id=4(num 4 是FB id),它会给你http://www.facebook.com/zuck,这是马克扎克伯格的个人资料。

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

根据这个原则,我需要找出一个 id 是谁。

I have typed id 4a got it is zuck.

我输入了 id4得到它是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.

但是我需要它来获得更多的 id,所以手动完成需要很多时间。请帮助我,我该怎么做。

采纳答案by Kevin

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

如果您已经拥有该特定用户的 ID,则只需将其添加到此 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.

旁注:为什么不改用 PHP SDK。

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

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

回答by Nam G VU

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

由于用户名不再可从 Graph API 端点获得/user-id,如此处讨论,我在这里提出另一种解决方法(但使用 Python 代码)

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

简而言之,我们在fb.com/USER_ID打开页面并从中抓取用户名

#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)

回答by iRohitBhatia

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

截至 2019 年 10 月,唯一可能的方法是在请求和权限中请求 user_links

enter image description here

在此处输入图片说明

And then pass it in a scope, something like this

然后在一个范围内传递它,像这样

scope: ['user_link']