使用facebook php sdk登录后获取用户的Facebook信息 [英] Get user's facebook info after login using facebook php sdk

查看:367
本文介绍了使用facebook php sdk登录后获取用户的Facebook信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Facebook javascript sdk和php sdk登录用户到我的网络应用程序。使用javascript我有登录/外出工作,我可以获得登录用户的公共Facebook详细信息。



然而,当使用PHP时,我没有得到用户的Facebook详细信息。为了向您提供有关应用程序的一些信息,当我导航到包含表单的某个页面时,代码会检查他们是否已登录。如果没有,则会给他们一个LoginUrl链接。登录后,我有$ user_id,所以现在用户可以使用该表单,但是在提交表单时我没有将他们的facebook详细信息保存在MySQL数据库中。有没有人对我该做什么有建议?我肯定有$ user_id,因为只有我有$ user_id才显示表单。



我的代码基于https://developers.facebook.com/ docs / reference / php / facebook-api /



 <?php  
require ' facebook.php';
// 创建应用程序实例
$ config =数组(
' appId' => ' **************'
' secret' => ' ****************************'
' cookie' => true);
$ facebook = new Facebook($ config);
$ user_id = $ facebook-> getUser();
?>





 //包含表单的页面
< div data-role = page id = postAJourney data-add-back-btn = true data-back-btn-text = 返回 data-rel = BAC k data-direction = reverse 数据转换 = flip data-theme = b >
< div data-role = header data-position = 已修复 >
< h1 > 发​​布旅程< / h1 >
< / div >

< div data-role < span class =code-keyword> = content >
<? php if ($ user_id):? >
< 表格 action = add_journey.php 方法 = post id = postAJourneyForm >
<? php
尝试
{
< span class =code-comment> // 继续了解您已经过身份验证的登录用户。
$ user_profile = $ facebook-> api(' / me' );
echo 名称:。 $ user_profile [' name'];
// 商店会话数据
$ _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 [' < span class =code-string> id'
]。 / picture;
$ _SESSION [' fbLink'] = https://www.facebook.com/。$ user_profile [' < span class =code-string> name'];
}
catch(FacebookApiException $ e)
{
// 如果用户已注销,则可以使用
// 用户ID,即使访问令牌无效。
// 在这种情况下,我们会得到一个例外,所以我们
// 只需要求用户再次登录。
$ login_url = $ facebook-> getLoginUrl();
error_log($ e-> getType());
error_log($ e-> getMessage());
}
?>

// jQuery Mobile Form将在这里

<? php else:?>
<? php // 无用户,打印用户登录的链接
$ login_url = $ facebook-> getLoginUrl(array(
' < span class =code-string> scope' => ' email'
' redirect_uri' => http:// localhost / share /#postAJourney
' display' => ' popup'
));
?>
< div >
使用由PHP SDK处理的OAuth 2.0登录:
< a href = <?php echo $ login_url;?> > 使用Facebook登录< / a >
< / div >
<? php endif ?>





我根本没有收到用户详细信息。登录后,我会看到表单,网址包含更多数据,但不知道如何处理。我尝试了我提供的链接中的示例,我遇到了同样的问题。 -

解决方案

user_id,所以现在用户可以使用该表单,但是在提交表单时我没有将他们的facebook详细信息保存在MySQL数据库中。有没有人对我该做什么有建议?我肯定有


user_id,因为只有我有


user_id才显示表格。



我的代码基于https://developers.facebook.com/docs/reference/php/facebook-api/



 <?php  
需要' facebook.php';
// 创建应用程序实例

I am using Facebook javascript sdk and php sdk to login user to my web app. With javascript I have the login/out working and I can get a logged in user''s public facebook details.

However, when using php, I am not getting the user''s facebook details. 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. 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.

I based my code on 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
       {
       //Proceed knowing you have a logged in user who's authenticated.
       $user_profile = $facebook->api('/me');
       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",
     'display' => 'popup',
 ));
 ?>
 <div>
     Login using OAuth 2.0 handled by the PHP SDK:
     <a href="<?php echo $login_url; ?>">Login with Facebook</a>
 </div>
 <?php endif ?>



I''m not getting user details at all. After I login and I am shown the form, the url contains more data but not sure what to do with that. I tried the example from the link I provided and I have the same issue. –

解决方案

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. 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.

I based my code on https://developers.facebook.com/docs/reference/php/facebook-api/

<?php
require 'facebook.php';
//create application instance


这篇关于使用facebook php sdk登录后获取用户的Facebook信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆