3

Building an app with the Facebook JavaScript API that will embedded into a page using the new iframe method.

I want to detect if they have liked the current page. Usually I would use print_r($_REQUEST) in PHP but that doesn't seem to work when using an iframe.

There is also this option: http://developers.facebook.com/docs/reference/fbml/visible-to-connection/ but it says its deprecated and I have never liked this method as its fairly hacky.

What is the way t do it now? Prefer to use XFBML + JavaScript API but can use PHP if required.

wesbos
  • 25,839
  • 30
  • 106
  • 143

1 Answers1

3

We've done this several times, and it seems to work pretty well. It uses XFBML to generate a Like Button widget and the JS SDK to render XFBML and subscribe to Facebook events. Code sample below:

edit: Since you're looking to detect if the user is a fan when the page loads, and FB deprecated the feature to let you get it directly from them when the canvas is loaded by passing fb_page_id to the address query string, you'll need an application install for the user to test their fan-dom of your page. It certainly adds a lot of friction to your application, but it is what it is for now - I guess.

    <?php
require 'facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET',
    'cookie' => false,
));

try
{
    $session = $facebook->getSession();

    if (empty($session['uid']))
    {
        throw new Exception("User not connected to application.");
    }

    $is_fan = $facebook->api(array(
        'method'    => 'fql.query',
        'query'     => "SELECT uid, page_id FROM page_fan WHERE uid = {$session['uid']}"
    ));

    if (false == $is_fan || count($is_fan) == 0) // 0 results will be returned if the user is not a fan
    {
        $is_fan = false;
    }
    else
    {
        $is_fan = true;
    }   
}
catch (FacebookApiException $e)
{
    /**
     * you don't have an active user session or required permissions 
     * for this user, so rdr to facebook to login.
    **/

    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'user_likes'
    ));

    header('Location: ' . $loginUrl);
    exit;
}
?>
<html>
<head>
</head>
<body>

<? if (empty($is_fan)): //user is not a fan. ?>
    <fb:like href="http://www.facebook.com/your-facebook-page"
        show_faces="true"
        width="400">
    </fb:like>
<? else: ?>
    Yay! You're a fan!
<? endif; >?

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript">
    </script>
<script type="text/javascript">
FB.init({
    appId: '<?= FB_APP_ID; ?>',
    cookie: true,
    status: true,
    xfbml: true
});

// Subscribe to the edge creation event, from Facebook
FB.Event.subscribe('edge.create', function(response)
{
    alert("Congratulations! We are so excited that you are a fan now! woot!")
});

</script>

</body>
</html>

okay, finally got got everything formatted with straight markdown. that wasn't painful at all.. (sike) :|

Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54
  • This works for the event, but if they come to the page they already like, the event isn't triggered. – wesbos Apr 29 '11 at 12:55
  • True story. For that, we append "?fb_page_id=" to the address bar query string (so..apps.facebook.com/your-app/path/to/page.php?fb_page_id=123123123). That adds information to the POSTed data. However, unfortunately, FB kind of deprecated this feature and if you disable some of their migration options, it won't work. – Jim Rubenstein Apr 29 '11 at 12:59
  • I just re-verified my comment, and just as I thought; Facebook removed/deprecated/disabled the "fb_sig_is_fan" value from the data POSTed to your application. So, I'll edit my code above to check for fan-age on page load. – Jim Rubenstein Apr 29 '11 at 13:05
  • That just adds a param to my array called 'fb_page_id' - I dont see what that does? Am I supposed to see a bunch of POST data? – wesbos Apr 29 '11 at 13:07
  • This looks great, will give it a shot now. Thanks a ton :) – wesbos Apr 29 '11 at 13:18
  • @Wes, unfortunately, they recently deprecated the feature. In the past, it appended a param called "fb_sig_is_fan" with a value of 1 or 0. Unfortunately, that feature is gone. I posted a new solution above, it uses PHP and requires an install..if you want to make it a php-free solution, you still have the same problem, but you can use the Facebook JS SDK in order to get a login and query FQL... – Jim Rubenstein Apr 29 '11 at 13:19
  • Hey JimR - I get an error saying they aren't connected to the application. Which is true, because I only want the user to connect to the page, they dont know about the application. Any ideas? – wesbos May 03 '11 at 13:31
  • @Wes, yeah, the problem is that in order to check if a user is a Fan of a Page you have to have access to the pages they like via the api. This requires you to get an application install from them. I understand that this is incredibly less than ideal, and it didn't used to be the case - Facebook recently deprecated and removed the feature that let you check to see if a user was a fan without an application install ): – Jim Rubenstein May 03 '11 at 14:57