php 如何从我的应用程序中获取 Facebook 用户 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5780416/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:28:17  来源:igfitidea点击:

How get facebook user ID from my application?

phpfacebook

提问by riad

I created a facebook application and send request to a user through below code.Now when a user allow my application then i need his id and email address into my canvas url.But on that below code user don't go to my canvas URL.So,Please help me to solve ,how a user go to my canvas url when he allow my application and i get the user id of that user?My code is below:

我创建了一个 facebook 应用程序并通过以下代码向用户发送请求。现在当用户允许我的应用程序时,我需要他的 id 和电子邮件地址到我的画布网址中。但是在下面的代码中,用户不要转到我的画布网址。所以,请帮我解决,当用户允许我的应用程序并获得该用户的用户 ID 时,他如何转到我的画布 url?我的代码如下:

require_once 'facebook.php';
$facebook = new Facebook(array(
'appId' => '#######',
'secret' => '###################',
'cookie' => true, ));
$req_perms = "publish_stream,offline_access,user_status,email,read_stream";

$session = $facebook->getSession();

$loginUrl = $facebook->getLoginUrl(array('canvas'=>1,'fbconnect' => 0,'req_perms'=>$req_perms));
$me = null;

if (!$session) { echo "top.location.href= '$loginUrl';";exit;}

else{

 try {
        $uid    =   $facebook->getUser();
        $me     =   $facebook->api('/me');
     } 
 catch (FacebookApiException $e) 
   { echo "<script type='text/javascript'>top.location.href= '$loginUrl';</script>"; exit;}
}

require_once 'facebook.php';
$facebook = new Facebook(array(
'appId' => '#######',
'secret' => '##################',
'cookie' => true, ));
$req_perms = "publish_stream,offline_access,user_status,email,read_stream";

$session = $facebook->getSession();

$loginUrl = $facebook->getLoginUrl(array('canvas'=>1,'fbconnect' => 0,'req_perms'=>$req_perms));
$me = 空;

if (!$session) { echo "top.location.href='$loginUrl';";exit;}

别的{

 try {
        $uid    =   $facebook->getUser();
        $me     =   $facebook->api('/me');
     } 
 catch (FacebookApiException $e) 
   { echo "<script type='text/javascript'>top.location.href= '$loginUrl';</script>"; exit;}
}

So,please help me to get the user id.

所以,请帮我获取用户ID。

Thanks in advance

riad

提前致谢

里亚德

回答by Alex Latchford

The Facebook ID & email for the current user can be accessed through:

可以通过以下方式访问当前用户的 Facebook ID 和电子邮件:

$me['id'];
$me['email'];

Is that what you're after?

那是你追求的吗?

回答by ifaour

First of all, your code is "badly" written. The catchblock in your code "when executed" does notmean that the user is not logged in. It means that something went wrong in your tryblock (a.k.a bad Facebook Call).

首先,你的代码写得“糟糕”。catch“执行时”代码中的块并不意味着用户未登录。这意味着您的try块中出现了问题(也就是 Facebook Call 错误)。

Now for your question, you just need to place this in your tryblock:

现在对于您的问题,您只需要将其放在您的try块中:

$data     =   $facebook->api('/me?fields=id,email');

IMPORTANT NOTE:
You may notget the user realemail, the user may choose to not share it with you and this way Facebook will create a "mirror" email to share with your App.

重要提示:
您可能不会收到用户的真实电子邮件,用户可能会选择不与您共享,这样 Facebook 将创建一个“镜像”电子邮件与您的应用程序共享。

回答by riad

At last i solve my problem and now i can easily get the facebook user id and email address and other related information.Using this code i can hit to facebook for particular user's authentication and when user accept my application then i can get his/her facebook user id, email and other information into $user_info array.Hope it will help you.

最后我解决了我的问题,现在我可以轻松获得 facebook 用户 ID 和电子邮件地址以及其他相关信息。使用此代码我可以点击 facebook 进行特定用户的身份验证,当用户接受我的申请时,我可以获得他/她的 facebook将用户ID、电子邮件和其他信息放入$user_info 数组中,希望对您有所帮助。

Just see my code below:

看看我下面的代码:

<?php
require_once 'facebook.php';
$facebook = new Facebook(array(
  'appId'  => '########',
  'secret' => '##########################',
  'cookie' => true,
));

$req_perms = "publish_stream,offline_access,user_status,email,read_stream"; 
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(array('canvas'=> 1,'fbconnect' => 0,'req_perms' => $req_perms));
$user_info = null;
if (!$session) 
    { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";exit;}
else{
     try {  $user_info = $facebook->api('/me');  } 
     catch (FacebookApiException $e) 
         { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; exit;}
    }
?>
<!doctype html>
<html>
<head>
</head>
<body>
<?php 
$user_name=$user_info['name'];
echo "Hi $user_name ! Your Facebook ID is: ".$user_info['id']."<br/>"; 
echo "Hi $user_name ! Your E-mail Address is: ".$user_info['email']."<br/>"; 
echo "Thanks for accepting our application.";
//print_r($user_info);
?>
</body>
</html>

回答by Balakrishnan

I am not familiar with php to comment on the code above. However in general it would be better to use javascript sdk for login alone (faced the same issue in asp.net). That way post user login, the control comes back to the same page where it was initiated. For example:

我不熟悉php来评论上面的代码。然而,一般来说,单独使用 javascript sdk 进行登录会更好(在 asp.net 中面临同样的问题)。这样,用户登录后,控件就会返回到启动它的同一页面。例如:

    <div id="fb-root"></div>
    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript">
        FB.init({
            appId: <app_id>, cookie: true, status: true, xfbml: true
        });

        function fblogin() {
            FB.login(function (response) {
                //Control comes back here
                if (response.session) {
                    alert(response.session.access_token);
                    var url = '/me?fields=name,email';
                    FB.api(url, function (response) {
                        //You are now in the same page with the data you wanted. If necessary redirect.
                        alert(response.name);
                        alert(response.email);
                    });
                }
                else {
                    alert("User did not login successfully");
                }
            }, { perms: 'email' });
        }
    </script>
    <a href="#" onclick="fblogin(); return false;"><img src="../Images/social_facebook.png" /></a>