我创建了一个Facebook应用程序,我将其用作页面选项卡。我正在使用PHP SDK和Javascript SDK,并经过身份验证和完美设置,以显示对话框,然后在标签iframe中显示我的页面。
我现在想异步加载我网站的其他页面,但是我无法在异步加载的页面上访问用户数据(名称等)。如何将身份验证和变量传递给ajax加载的页面?
(EDIT)解决方案:
PHP SDK会自动创建一个会话,以便在使用ajax(或任何地方)加载的页面中使用。你只需要在每个加载了ajax的页面上调用你的身份验证例程来访问数据(最好使用php include)。
我使用简单的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>';
}
?>
答案 0 :(得分:0)
这取决于您的请求,但您可以将数据保存在$ _SESSION变量中,例如$ _SESSION ['userData'] = $ facebook-&gt; api('/ me');并在每个页面上调用它。