如何检查脸书id是否有效


How to check if a facebook id is valid

我有一个脚本,它将生成随机数,如100008983838,但我需要一个解决方案来检查生成的数字是否与有效的facebook帐户关联。

我正在寻找的解决方案应该是PHP。

谢谢。

一种解决方案是通过Graph API生成后检查ID。

http://graph.facebook.com/[ID]

无论生成什么ID,都将其放在URL的末尾,并检查收到的响应。如ID为1303834107

你可以查看@http://graph.facebook.com/1303834107

如果它是正确的,响应将类似于:

{
  "id": "1303834107",
  "name": "John Flake",
  "first_name": "John",
  "last_name": "Flake",
  "link": "https://www.facebook.com/john.flake",
  "username": "john.flake",
  "gender": "male",
  "locale": "en_US"
}

或者,如果该ID无效,您将得到以下信息:

{
 "error": 
  {
  "message": "Unsupported get request.",
  "type": "GraphMethodException",
  "code": 100
  }
}

根据响应,您可以验证ID。

每次生成ID时,使用php-ccurl调用API URL以检查响应。

这很容易:

只需从php对进行curl调用

https://graph.facebook.com/1457691266/

将其替换为任何ID-100008983838

如果您得到一个json作为响应,那么ID是有效的,否则您将得到不受支持的get请求。:)

希望能有所帮助。

你可以这样做

$url = get_data("https://graph.facebook.com/+".$id."'");
$id = json_decode($url, true);
$id = $id['data'][0]['username'];

$id为您提供已发布id的用户名。