2014-10-30 238 views
2

在互联网上有关于这个问题的几个主题,但我还没有找到任何复杂的解决方案。因此,我想请你帮忙。如何从Facebook上获取用户名ID

我需要将Facebook ID更改为用户名。

当你输入网址这样的:

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

在这个原则,我需要找出谁是一个身份证。

我输入了id 4 a得到它是zuck

但我需要它更多的ID,所以它会花费很多时间手动。请帮助我,我该怎么做。

回答

3

如果你已经有一个特定用户的ID,那么就加入它这个网址:

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

简单的例子:

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

这应该基本产量:

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 
) 

然后你可以把它叫做普通数组:

echo $user['username']; 

旁注:为什么不使用PHP SDK代替。

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

+1

这将停止在2015年4月30日工作时API 1.0版被删除 – WizKid 2014-10-30 01:05:00

+0

对于这么简单的任务来说,php sdk太重了,恕我直言。但答案是正确的,所以+1为你:) – luschn 2014-10-30 08:26:25

+0

@luschn是好点也可能是这样一个简单的意图很烦琐 – Ghost 2014-10-30 08:45:04

2

由于用户名是没有更多的可以从图形API端点/user-id所讨论here,我在这里提出了另一种解决方法(但与Python代码)

简而言之,我们在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)