2012-02-25 162 views
-1

我想在粉絲頁面上創建一個選項卡應用程序。一年前,使用facebook.php v.2.0,我能夠創建和管理一個應用程序,但現在情況發生了變化,我無法弄清楚如何連接到API來驗證粉絲頁面是否被喜歡。我用facebook.php v 3.1.1,所以一年前,我會用:粉絲頁面上的Facebook應用程序選項卡

<?php 
    require 'facebook.php'; 
    $app_id = "YOUR APP ID HERE"; 
    $app_secret = "YOUR SECRET KEY HERE"; 

    $facebook = new Facebook(array(
     'appId' => $app_id, 
     'secret' => $app_secret, 
     'cookie' => true 
    )); 

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

我應該怎麼辦呢?

+1

其實,你可以使用'$ _ POST [ 'signed_request'] ' - 閱讀此http://developers.facebook.com/docs/authentication/signed_request/ – scibuff 2012-02-25 23:59:42

+0

不工作..我想我想念的東西..因爲和$用戶= $ facebook-> getUser();是0(零)和echo var_export($ _ POST ['signed_request'],true);是NULL ... – Sebastian 2012-02-26 00:06:34

+0

只有當用戶已經給予應用程序(至少基本)權限時,應用程序纔有權訪問用戶標識和用戶詳細信息,只需執行'print_r(parse_signed_request($ _REQEST ​​['signed_request'],$ secret)) ;'看看你得到的是什麼數據。頁面 - >喜歡是肯定存在,即使用戶沒有給予應用權限 – scibuff 2012-02-26 00:10:42

回答

1

你並不需要使用getSignedRequest,這樣的事情應該工作:

$signed_request = $_REQUEST["signed_request"]; 
list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); 

if (empty($data["page"]["liked"])) { 
    //User is not a fan 
} else { 
    //User is a fan 
} 
+0

它每次都不粉絲..我想當我創建該應用程序有什麼錯誤..看:http://www.facebook.com/PentruRomani?sk=app_215673801864574當你訪問它時,不需要權限 – Sebastian 2012-02-26 00:16:45

+0

'print_r($ signed_request)'顯示什麼? – 2012-02-26 02:16:02

+1

另外,請確保你的應用程序配置中你的標籤網址的末尾有一個'/',它似乎有問題有時 – 2012-02-26 02:18:21

1

只是這樣做:

<?php 
    $secret = ''; // Input the application secret 

    function parse_signed_request($signed_request, $secret) { 
     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     // Decode the data 
     $sig = base64_url_decode($encoded_sig); 
     $data = json_decode(base64_url_decode($payload), true); 

     if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { 
      echo 'Unknown algorithm. Expected HMAC-SHA256'; 
      return null; 
     } 

     // Check signature 
     $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); 
     if ($sig !== $expected_sig) { 
      echo 'Bad Signed JSON signature!'; 
      return null; 
     } 
     return $data; 
    } 

    function base64_url_decode($input) { 
     return base64_decode(strtr($input, '-_', '+/')); 
    } 

    $data = parse_signed_request($_REQUEST['signed_request'], $secret); 
    print_r($data); 
    exit(); 
?> 
+0

未知算法。預計HMAC-SHA256 ... – Sebastian 2012-02-26 00:45:05

+0

最有可能您的$祕密不正確 – scibuff 2012-02-26 00:48:35

+0

我認爲我沒有正確創建應用程序.. – Sebastian 2012-02-26 00:49:00

相關問題