我可以通过个人资料网址或用户名获取任何用户的Facebook ID吗?

时间:2018-04-04 05:59:04

标签: html facebook facebook-graph-api

如果任何Facebook用户使用其个人资料网址或用户名,我该如何获得用户。

我想实现与this website中显示的功能相同的功能。

我尝试使用https://graph.facebook.com/username,但它给了我以下错误

{
   "error": {
      "message": "(#803) Cannot query users by their username (username)",
      "type": "OAuthException",
      "code": 803,
      "fbtrace_id": "G8AjtLUd3AC"
   }
}

我还看过this post并且它说facebook不允许从用户名中获取ID。如果它是真的那么this website如何获取id

1 个答案:

答案 0 :(得分:0)

你尝试旧方法......对于新的你需要facebook访问令牌

尝试这种类型 -

$access_token = "EAAAAAYsX7TsBAH1efzbZAhqJREQfwzZCNIZBgGVbpxoLaoZBOVx4KminpzqkiczMC19pVdjqX6fVj3jIxxdGaROSyr04ZCYmhSe59UMZAwZAKH5YJ7dnawHwgYSLvbJa4TV8kqAkO6l5mcgVIVfeE6trgm7hh01RJ7SB0hDc7scQ8YineE0exabZCmuYOvnldngEopPEYBZAZBVCZC4knDHGNZCKwk0fj36526MZD";


function getFacebookIdFromUrlandUsername($url,$access_token) {
/**
 * Taken from https://www.allautoliker.com/find_id, the valid formats are:
 * dr.p.pareek
 * https://www.facebook.com/leloweb
 * https://m.facebook.com/dr.p.pareek
 * https://www.facebook.com/profile.php?id=100001148755604
 */
$correctURLPattern = '/^https?:\/\/(?:www|m)\.facebook.com\/(?:profile\.php\?id=)?([a-zA-Z0-9\.]+)$/';
if (!preg_match($correctURLPattern, $url, $matches)) {
    $getinf = 'https://graph.facebook.com/' .$url.'/?access_token='.$access_token;
    $info = json_decode(file_get_contents($getinf), true);
    return $info;
}else{
    $getinf = 'https://graph.facebook.com/' .$matches[1].'/?access_token='.$access_token;
    $info = json_decode(file_get_contents($getinf), true);
    return $info;
}
}
$find_id = "dr.p.pareek";
$user = getFacebookIdFromUrlandUsername($find_id,$access_token);
 //echo '<pre>';
//print_r($user);

echo $user['id'];