开发者

Detect Facebook page fan on the website

I want to show a Faceboo Fanbox on the website, but only if the current user is NOT a fan of our FB page. Is there a way to detect if th开发者_JAVA技巧e user is a fan of certain page or not and then display the fanbox?


No, I am pretty sure this is impossible. In order to be able to do this you would

  1. Need to be able to fetch somebody's FB user id from their cookie and that would only work if the are currently logged in on FB. They might not even store the user id in the cookie (don't know about this).
  2. Need to be able to access this person's liked pages
  3. All of the above without any consent from the user.

Imagine the outcry of some people if this was possible...


For a PHP based website, it can be done this way:

1- Create a new Facebook app here.

2 - Download Facebook PHP SDK from here. Extract the files and place it in the same folder with the file in which you will paste the code given below!

3 - Get your Facebook Page ID using this tool.

4 - Generate Facebook like box code for your page from here.

After 1, 2, 3 and 4 steps are complete, you can check if user has liked a page or not with the following code:

<?php
    require('facebook.php');
    $config = array(
         'appId' => 'your facebook app id',
         'secret' => 'your facebook app secret code',

        'allowSignedRequest' => false
    );
    $facebook = new Facebook($config);
    $user_id = $facebook->getUser();
    if (isset($user_id)) {
        try {           
            $likes = $facebook->api('/me/likes/your_facebook_page_id_here', 'GET');             

            if (!empty($likes['data'])) // if user has liked the page then $likes['data'] wont be empty otherwise it will be empty
            {
                echo 'Thank you for liking our fan page!';                   
            }
           else {
                echo 'You have not liked our fan page! Like it now:';
                ?>                   
                <iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fchillopedia&amp;width&amp;height=290&amp;colorscheme=light&amp;show_faces=true&amp;header=true&amp;stream=false&amp;show_border=true&amp;appId=1392604484339363" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:290px;" allowTransparency="true"></iframe> //replace this with your own Facebook like box code
                <?php
            }
        } catch (FacebookApiException $e) {
            $login_url = $facebook->getLoginUrl();
            echo '<a href="' . $login_url . '">Please click here to login into your Facebook account.</a>';
            error_log($e->getType());
            error_log($e->getMessage());
        }
    } else {
        $login_url = $facebook->getLoginUrl();
        echo '<a href="' . $login_url . '">Please lick here to login into your Facebook account</a>';
    }
    ?>

The user will click on the "Please click here to login into your Facebook account." text which will redirect it to Facebook app permissions page, once user allows the permission to your app the code will fetch user's data and will display the likebox if user hasn't liked your fan page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜