2014-10-30 236 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)