How to check if a user likes my Facebook Page or URL using Facebook's API

后端 未结 5 1037
囚心锁ツ
囚心锁ツ 2020-11-22 02:37

I think I\'m going crazy. I can\'t get it to work.
I simply want to check if a user has liked my page with javascript in an iFrame app.

FB.a         


        
5条回答
  •  梦毁少年i
    2020-11-22 03:25

    I tore my hair out over this one too. Your code only works if the user has granted an extended permission for that which is not ideal.

    Here's another approach.

    In a nutshell, if you turn on the OAuth 2.0 for Canvas advanced option, Facebook will send a $_REQUEST['signed_request'] along with every page requested within your tab app. If you parse that signed_request you can get some info about the user including if they've liked the page or not.

    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) {
          echo "This content is for Fans only!";
        } else {
          echo "Please click on the Like button to view this tab!";
        }
      }
    

提交回复
热议问题