我正在使用 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';
//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 ?>