简体   繁体   English

使用facebook php sdk登录后获取用户的facebook信息

[英]Get user's facebook info after login using facebook php sdk

I am using Facebook javascript sdk and php sdk to login user to my web app. 我正在使用Facebook javascript sdk和php sdk将用户登录到我的Web应用程序。 With javascript I have the login/out working and I can get a logged in user's public facebook details. 使用javascript,我可以执行登录/注销操作,并且可以获得登录用户的公共Facebook详细信息。

However, when using php, I am not getting the user's facebook details. 但是,使用php时,我没有获得用户的facebook详细信息。 To give you some info on the app, when I navigate to a certain page that contains a form, the code checks to see if their logged in. If not, they are given a LoginUrl link. 为了向您提供有关该应用程序的一些信息,当我导航到包含表单的某个页面时,代码将检查以查看其是否已登录。 Once logged in, I have the $user_id, so now the user can use the form, but I am not getting their facebook details to save in MySQL database when form is submitted. 登录后,我有了$ user_id,因此用户现在可以使用该表单,但是提交表单时,我没有得到他们的Facebook详细信息保存在MySQL数据库中。 Does anyone have advice to what I should do? 有人对我应该做什么建议吗? I definitely have the $user_id as the form is only shown if I have the $user_id. 我绝对有$ user_id,因为只有在我有$ user_id时才会显示该表单。

I based my code on https://developers.facebook.com/docs/reference/php/facebook-api/ 我的代码基于https://developers.facebook.com/docs/reference/php/facebook-api/

<?php
require 'facebook.php';
//create application instance
$config = array(
'appId' => '**************',
'secret' => '****************************',
'cookie' => true);
$facebook = new Facebook($config);
$user_id = $facebook->getUser(); 
?>

//Page containing form
<div data-role="page" id="postAJourney" data-add-back-btn="true" data-back-btn-text="back" data-rel="back" data-direction="reverse" data-transition="flip" data-theme="b" >
    <div data-role="header"  data-position="fixed">
       <h1>Post A Journey</h1>
    </div>

    <div data-role="content">
       <?php if ($user_id): ?>
       <form action="add_journey.php" method="post" id="postAJourneyForm">
       <?php
       try
       {
           $access_token = $facebook->getAccessToken();
       $param_token = array('access_token' => $access_token); 
       //Proceed knowing you have a logged in user who's authenticated.
       $user_profile = $facebook->api('/me','GET',$param_token);
       echo "Name: " . $user_profile['name'];
       // store session data
       $_SESSION['fb_id'] = $user_profile['id'];
       $_SESSION['fb_name'] = $user_profile['name'];
       $_SESSION['fb_first_name'] = $user_profile['first_name'];
       $_SESSION['fb_last_name'] = $user_profile['last_name'];
       $_SESSION['fbImg'] = "https://graph.facebook.com/".$user_profile['id']."/picture";
    $_SESSION['fbLink'] = "https://www.facebook.com/".$user_profile['name'];
       }
       catch(FacebookApiException $e)
       {
       // If the user is logged out, you can have a 
       // user ID even though the access token is invalid.
       // In this case, we'll get an exception, so we'll
       // just ask the user to login again here.
       $login_url = $facebook->getLoginUrl(); 
       error_log($e->getType());
       error_log($e->getMessage());
       }
       ?>

       //jQuery Mobile Form would be here

 <?php else: ?>
 <?php // No user, print a link for the user to login
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => "http://localhost/share/#postAJourney"
));
 ?>
 <div>
     Login using OAuth 2.0 handled by the PHP SDK:
     <a href="<?php echo $login_url; ?>">Login with Facebook</a>
 </div>
 <?php endif ?>

/*In that you need user access token then pass that token in Facebook /me API as parameter then you can get the facebook user logged in information. / *因为您需要用户访问令牌,然后将该令牌传递到Facebook / me API中作为参数,然后才能获取Facebook用户登录信息。 you need to pass the scope of permission which you want from the user in scope .For retrieving user access token you can use like that */ 您需要通过范围中的用户传递所需的权限范围。要检索用户访问令牌,可以这样使用* /

$loginUrl = $facebook->getLoginUrl(array('scope' => 'manage_pages','redirect_uri'=>'you url')); $ loginUrl = $ facebook-> getLoginUrl(array('scope'=>'manage_pages','redirect_uri'=>'you url'));;

/*made oauth url and pass the scope in that to allow user to Facebook oath page for permission Get the access token from facebook redirect it will give you in the request */ / *制作oauth url并传递范围,以允许用户进入Facebook宣誓页面以获取许可。从facebook重定向获取访问令牌,它将在请求中给您* /

$access_token = $facebook->getAccessToken(); $ access_token = $ facebook-> getAccessToken();

$param_token = array('access_token' => $access_token); $ param_token = array('access_token'=> $ access_token);

$user_profile = $facebook->api('/me'); $ user_profile = $ facebook-> api('/ me'); //Replace with that line //用该行替换

$user_profile = $facebook->api('/me','GET',$param_token); $ user_profile = $ facebook-> api('/ me','GET',$ param_token);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM