粉丝页面上的Facebook应用程序选项卡

时间:2012-02-25 23:55:25

标签: php javascript facebook facebook-graph-api facebook-fql

我想在粉丝页面上制作标签应用程序。一年前,使用facebook.php v.2.0,我能够创建和管理一个应用程序,但现在情况发生了变化,我无法弄清楚如何连接到API以验证粉丝页面是否是喜欢与否。我使用facebook.php v.3.1.1,所以一年前我会使用:

<?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"];
?>

我该怎么做?

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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();
?>