我正在开发一个包含2个文件的应用程序:
的index.php
<?php
include 'config.php';
include 'lib/functions.php';
require_once 'lib/sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => SECRET
));
$userID = $facebook->getUser();
if (!$userID)
{
$scope = 'read_stream, publish_stream, user_likes, user_photos, user_status, friends_hometown, friends_location, friends_likes, photo_upload';
$params = array(
'scope' => $scope,
'redirect_uri' => "https://apps.facebook.com/".APP_NAME."/"
);
$loginUrl = $facebook->getLoginUrl($params);
}
else
{
$access_token = $facebook->getAccessToken();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Facebook App</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link href="/css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body style="margin:0 !important; padding: 0 !important;">
{ SOME HTML }
<script type="text/javascript">
function loadData()
{
$.ajax({
url: "process.php",
type: 'POST',
cache: false,
data: {
id_sessione: function(){return $("#id_sessione").val();}
},
dataType: "json",
success: function(data) {
if (data.status == "FAILURE")
alert('failure');
else
console.log(data);
}
});
}
/********* INIT ********/
$(document).ready(function(){
if ($("#userID").val() != "0")
loadData();
else
top.location = '<?php echo $loginUrl; ?>';
});
</script>
<input id="id_sessione" type="hidden" value="<?php echo session_id(); ?>" />
<input id="userID" type="hidden" value="<?php echo $userID; ?>" />
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo APP_ID; ?>',
frictionlessRequests: true
});
FB.Canvas.setAutoResize(7);
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/it_IT/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
和process.php
<?php
session_id($_POST['id_sessione']);
session_start();
include 'config.php';
include 'lib/functions.php';
require_once 'lib/sdk/facebook.php';
set_time_limit(120);
$memory_limit = ini_set('memory_limit', '512M');
try
{
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => SECRET
));
$userID = $facebook->getUser();
$user_profile = $facebook->api('/me');
{ SOME FQL QUERIES AND DATA PROCESSING }
echo json_encode(array(
'status' => 'SUCCESS',
.... other info
));
} catch (Exception $e) {
$handle = fopen(ROOT_PATH.'/log/'.$userID.'_'.uniqid().'.txt', 'a+') or die('Cannot open file');
fwrite($handle, $e->getMessage()."\n");
fwrite($handle, $e->getTraceAsString()."\n");
fclose($handle);
echo json_encode(array(
'status' => 'FAILURE'
));
}
在95%的情况下,应用程序工作正常,但有时候是$ facebook-&gt; api('/ me');转发异常“必须使用活动访问令牌来查询有关当前用户的信息。”并且$ userID等于0.
为了在所有文件中拥有相同的会话ID,我必须在POST中使用index.php传递的session_id手动启动会话。这是因为某些浏览器(IE,safari)不会在iframe中传递第三方网页的Cookie。 (http://stackoverflow.com/questions/9930671/safari-3rd-party-cookie-iframe-trick-no-longer-working)
有什么建议吗? 谢谢
答案 0 :(得分:0)
检查会话值是否正常传递?