我创建了一个 facebook 应用程序,我将其用作页面选项卡。我正在使用 PHP SDK 和 Javascript SDK,并且已经过身份验证并将其设置为完美显示对话框,然后在选项卡 iframe 中显示我的页面。
我现在想异步加载我网站的其他页面,但是我无法访问异步加载页面上的用户数据(姓名等)。如何将身份验证和变量传递给 ajax 加载的页面?
(编辑)解决方案:
PHP SDK 会自动创建一个会话,以便在使用 ajax(或任何地方)加载的页面之间使用。您只需在每个加载有 ajax 的页面上调用您的身份验证例程即可访问数据(最好使用 php 包含)。
我使用简单的 jquery 加载将页面加载到 div 中:
$('div#page').load('your-page.php');
这是我在每个 php 页面上运行的例程:
<?php
//facebook application configuration
$fbconfig['appid' ] = "YOUR APP ID";
$fbconfig['secret'] = "YOUR APP SECRET";
$fbconfig['baseUrl'] = "SOURCE FILES BASE URL";
$fbconfig['appBaseUrl'] = "APP BASE URL";
/*
* If user first time authenticated the application facebook
* redirects user to baseUrl, so I checked if any code passed
* then redirect him to the application url
*/
if (isset($_GET['code'])){
header("Location: " . $fbconfig['appBaseUrl']);
exit;
}
//
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown'
)
);
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
//get user basic description
$userInfo = $facebook->api("/$user");
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>