0

我正在开发一个包含 2 个文件的应用程序:

  1. index.php(应用程序容器)
  2. process.php(在 ajax 中调用)

索引.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->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)

有什么建议么?谢谢

4

1 回答 1

0

检查会话值是否正确传递?

于 2012-07-18T22:46:51.060 回答