2012-05-11 34 views
0

我有一個Facebook應用程序,它發佈了一些預加載的圖像。我改變了代碼結構,所以我把所有的變量都移到了config.php中,所以它會更舒適。但之後,該應用程序無法正常工作,我真的不知道爲什麼......在權限對話框後,它一遍又一遍地實現自己...有人能夠發現錯誤嗎?Facebook應用程序在授權後重新加載

這是在index.php:

<?php 

require 'facebook.php'; 
require 'config.php'; 

$facebook = new Facebook(array(
'appId' => $appId, 
'secret' => $appSecret, 
'baseUrl' => $baseUrl, 
'appBaseUrl' => $appBaseUrl, 
'fileUpload' => 'true', 
'cookie' => 'true' 
)); 

if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) { 
    $baseUrl = str_replace('http','https',$baseUrl); 
    $fbPageURL = str_replace('http','https',$fbPageURL); 
    $fbPageAppURL = str_replace('http','https',$fbPageAppURL); 
}else{ 
    // not https 
} 

    $signed_request = $facebook->getSignedRequest(); 
    $page_id = $signed_request["page"]["id"]; 
    $like_status = $signed_request["page"]["liked"]; 

     if(!$like_status){ 
      header("Location: notfan.php"); 
      exit; 
     } 

$user = $facebook->getUser(); 

$params = array(
    scope => 'publish_stream,user_photos', 
    redirect_uri => $fbPageAppURL 
    ); 

$loginUrl = $facebook->getLoginUrl($params); 
$app_data = $signed_request["app_data"]; 

?> 
        <!doctype html> 
        <html xmlns:fb="http://www.facebook.com/2008/fbml"> 
        <head> 
        <title><?php echo $appName; ?></title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
        <style> 
         a:link {color:#f1effc;} 
         a:visited {color:#f1effc;} 
         a:hover {color:#19378d;} 
         a:active {color:#19378d;} 
         a:link {text-decoration: none} 
        </style> 
        </head> 
        <body> 
        <script type="text/javascript"> 
         function NotInFacebookFrame() { 
          return top === self; 
         } 
         function ReferrerIsFacebookApp() { 
          if(document.referrer) { 
           return document.referrer.indexOf("apps.facebook.com") != -1; 
         } 
         return false; 
         } 
         if (NotInFacebookFrame() || ReferrerIsFacebookApp()) { 
          top.location.replace("<?= $fbPageAppURL ?>"); 
         } 
        </script> 

<?php if($user){ 

     try { 
       $user_profile = $facebook->api('/me'); 

       $scope = 'publish_stream,user_photos'; 
       $scope_params = explode(',',$scope); 

       $permissions = $facebook->api("/me/permissions"); 

        if(array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0])) { 

        // the main app logic here 

        } else { 
         $user = null; 
        } 


    } catch (FacebookApiException $e) { 
      error_log($e); 
      $user = null; 
     } 
    } 

    if ($user) { 
    // logged in user 
    } else { 
     // not logged in or not authenticated 
     echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>'; 
    } 

?> 
</body> 
</html> 

和config.php文件:

<?php 
$appName = 'the name'; 
$appId = '123456789'; 
$appSecret = '1324v3434v34v'; 
$baseUrl = 'http://www.domain.com/app-name/'; 
$appBaseUrl = 'http://apps.facebook.com/zzzzzzz/'; 
$fbPageURL = 'http://www.facebook.com/xxxxxxx'; 
$fbPageAppURL = $fbPageURL.'?sk=app_'.$appId; 
?> 

非常感謝您!

回答

1

事實證明,這是一個服務器配置故障。

0

Wrap $ user = $ facebook-> getUser();在try/catch中並回應被捕獲的異常。這會告訴你爲什麼用戶沒有登錄。


更多細節 - 在下面的評論之後。

您在Javascript中檢查文檔引用者 - 但我的測試中的實際引用者是「http://static.ak.facebook.com/platform/page_proxy.php?v=4」,所以這會失敗。這將導致一個循環。

要檢查,如果你在一個畫布的時候,剛開始使用PHP SDK

$facebook->signed_request(); 

的signed_request如果這是空的,那麼你是不是在畫布(或頁選項卡),所以重定向到畫布。用PHP做而不用Javascript。

+0

我試過這個:try {$ user = $ facebook-> getUser(); } catch(FacebookApiException $ e){echo $ e; }但不顯示錯誤(空白頁)並重新加載自己...:S – VORiAND

+0

什麼是URL?我可以嘗試連接並監控Facebook電話以提供更多線索。 – Robbie

相關問題