开发者

Facebook Fan Gate - Displaying the 'Reveal" Content

I'm trying to add a fan gate to my page, and I can get the "pre-like" content to show, but after I like the page - the post-like content isn't showing up. It shows the same pre-like content, regardless of whether I like the page or not.

This is the code I'm using in my index.php file...

 require 'facebook.php';
 $app_id = "myappid";
 $app_secret = "myappsecret";
 $facebook = new Facebook(array开发者_运维知识库(
 'appId' => $app_id,
 'secret' => $app_secret,
 'cookie' => true
 ));

 $signed_request = $facebook->getSignedRequest();

 $page_id = $signed_request["page"]["id"];
 $page_admin = $signed_request["page"]["admin"];
 $like_status = $signed_request["page"]["liked"];
 $country = $signed_request["user"]["country"];
 $locale = $signed_request["user"]["locale"];

 // If a fan is on your page
 if ($like_status) {
 $a = file_get_contents("dolike.html");
 echo ($a);
 } else {
 // If a non-fan is on your page
 $a = file_get_contents("dontlike.html");
 echo ($a);
 }

 ?>

I've looked at half-dozen examples, and they're all essentially the same (with a few variants, some use images over html, some use the html in the same page), but none of them show the post-like content.

I removed the app id and secret from the code, though I do have them and have been using them.

Any help'd be awesome.


<?php
 require 'facebook.php';
 $app_id = "myappid";
 $app_secret = "myappsecret";
 $facebook = new Facebook(array(
 'appId' => $app_id,
 'secret' => $app_secret,
 'cookie' => true
 ));

 $signed_request = $facebook->getSignedRequest();

 $page_id = $signed_request["page"]["id"];
 $page_admin = $signed_request["page"]["admin"];
 $like_status = $signed_request["page"]["liked"];
 $country = $signed_request["user"]["country"];
 $locale = $signed_request["user"]["locale"];

 // If a fan is on your page
 if ($like_status == 1) {
 $a = file_get_contents("dolike.html");
 echo ($a);
 } else {
 // If a non-fan is on your page
 $a = file_get_contents("dontlike.html");
 echo ($a);
 }

 ?>

Try this :)

EDIT:

Or try this version without the FB PHP-SDK, this is the solution I use for fangating so i don't need the user the whole PHP-SDK

<?php
  $app_secret="xxxxxxxxxxxxx";
  $data = parse_signed_request($_REQUEST['signed_request'], $app_secret);
  $page_data=$data['page'];

  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') {
      error_log('Unknown algorithm. Expected HMAC-SHA256');
      return null;
    }
    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
      error_log('Bad Signed JSON signature!');
      return null;
    }

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

  if($page_data['liked'] == "1"){

    // Fan Content

  }else{

    // No-Fan Content

  }
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜