使用 PHP 从 Facebook 获取用户 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6948668/
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
Get User Id from Facebook in PHP
提问by PUG
I am trying to make a Facebook app and need user id from Facebook when the user opens the applicaiton. I have setup my application and its show mock form on the Facebook canvas, I need help in connecting Facebook API to my page, would i need to download a API for that? And how do I get the user id from the JSON object which facebook says it sends to the application?
我正在尝试制作 Facebook 应用程序,并且在用户打开应用程序时需要来自 Facebook 的用户 ID。我已经在 Facebook 画布上设置了我的应用程序及其显示模拟表单,我需要帮助将 Facebook API 连接到我的页面,我是否需要为此下载 API?以及如何从 Facebook 发送给应用程序的 JSON 对象中获取用户 ID?
My test applcation is:
我的测试应用程序是:
<?php
echo "this is working";
?>
This is working on Facebook.
这是在 Facebook 上工作的。
回答by jBit
Download the PHP SDK
下载PHP SDK
A very simple code example to get the user id - if the user is logged in and has authorised the application then $facebook->getUser()
will give you the users id:
获取用户 ID 的一个非常简单的代码示例 - 如果用户已登录并已授权应用程序,$facebook->getUser()
则将为您提供用户 ID:
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
$user_profile = $facebook->api('/me');
echo "<pre>";
print_r($user_profile);
echo "</pre>";
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
Take a look a the examples within the SDK and on the Facebook Developers Site.
查看 SDK 中和Facebook 开发人员网站上的示例。
回答by Muhammad Junaid
Here is a sort of hack code written by me which allows to fetch any facebook user id even if he is not logged in or authorized by app https://github.com/invisiblevision/get-facebook-id/
这是我编写的一种黑客代码,即使他没有登录或未获得应用程序授权,也可以获取任何 Facebook 用户 ID https://github.com/invisiblevision/get-facebook-id/
<?php
$profile_url = 'https://facebook.com/profileUrl';
function get_web_page( $url )
{
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
/*Getting user id */
$url = 'http://findmyfbid.com';
$data = array('url' => $profile_url );
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
function getData($data)
{
$dom = new DOMDocument;
$dom -> loadHTML( $data );
$divs = $dom -> getElementsByTagName('code');
foreach ( $divs as $div )
{
return $div -> nodeValue;
}
}
$uid = getData($result); // User ID