Raphael
Raphael

Reputation: 562

Facebook: Check if User liked Facebook Fanpage Tab

I know, that my question was asked a lot of times here, but for me no answer worked. I've created a Facebook Fanpage Tab. All the files are stored at my private Webspace. Now I want to determine if a user already liked the page or haven't liked it yet!

So I used this code:

<?php
    function parsePageSignedRequest() {
        if (isset($_REQUEST['signed_request'])) {
            $encoded_sig = null;
            $payload = null;
            list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
            $sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
            $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
            return $data;
        }
        return false;
    }

    if($signed_request == parsePageSignedRequest()) {
        if($signed_request->page->liked) {
            $isteinfan = "false";
        } 
        else {
            $isteinfan = "true";

        }
    }   
    //PHP Variable an JavaScript übergeben
    echo "<script>";
    echo "isteinfan = '$isteinfan';";
    echo "console.log('ist ein fan: ');";
    echo "console.log(isteinfan);";
    echo "</script>";
?>

But it doesn't work. Can u give me help, please!!! Yours, Raphael

Upvotes: 2

Views: 4649

Answers (1)

Plankje
Plankje

Reputation: 298

I would recommend you include the facebook php library, which you can download from https://github.com/facebook/php-sdk/tree/master/src. You have to place all the three files in the same directory. Then you can get the liked status very easily:

define('APP_ID','xxxxxxxx');
define('APP_SECRET','xxxxxxxx');

require ("facebook.php");

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

$signed_request = $facebook->getSignedRequest();

$liked = $signed_request["page"]["liked"];

Now $liked is a boolean which can be true or false

Upvotes: 7

Related Questions