How To: Check if User ‘Liked’ the Application or Page

Posted by Dor Zuberi - April 21, 2011 - Web Development
Facebook Developers Logo

Hello mates,
Now that Facebook cancelled FBML, there is an alternative to filtering contents to users who “likes” the application.

Facebook decided to cancel FBML in order to make place to iFrame applications (again…), this move makes for us, web developers, an easy life, now we can build Facebook applications using our native programming language, instead of learning Facebook API.

Step 1 – Include Facebook API Liberary

In order for our “like” code to work we need to include Facebook’s API liberary, without that our script won’t work.

Go to Facebook’s official API liberary download on the following link -> Facebook API Liberary.

After downloading the liberary, place the file “facebook.php” on your main application directory, and then include it in your code.

include_once( "facebook.php" );

Step 2 – Add “Like” Script

Now that added Facebook API library, it is time to start and add some code.

Initiate Facebook API Library

        $fbAppArray = array(
            'appId'  => 'YOUR_APPLICATION_ID',
            'secert' => 'YOUR_APPLICATION_SECRET',
            'cookie' => true
        );
        $fbAppObj = new Facebook( $fbAppArray );
Be aware, you need to add your application ID and secret from the application creation screen on Facebook.

Handling User Session

        $signedRequest = $fbAppObj->getSignedRequest();
        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;
        }

This function decrypts the current user’s signature and gets it’s data so we can see if he liked the page.

Checking If the User “Liked” the Appliction

        if( $signedRequest == parsePageSignedRequest() )
        {
            if( $signedRequest->page->liked )
            {
                // What to show the user if he liked the application...
            }else{
                // Please like the application so you will be able to see the contents...
            }
        }

The Final Code

        include_once( "facebook.php" );
        $fbAppArray = array(
            'appId'  => 'YOUR_APPLICATION_ID',
            'secert' => 'YOUR_APPLICATION_SECRET',
            'cookie' => true
        );
        $fbAppObj = new Facebook( $fbAppArray );
        $signedRequest = $fbAppObj->getSignedRequest();
        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( $signedRequest == parsePageSignedRequest() )
        {
            if( $signedRequest->page->liked )
            {
                // What to show the user if he liked the application...
            }else{
                // Please like the application so you will be able to see the contents...
            }
        }

Download: zip Facebook "If Likes" Application Code

Enjoy,
Dor.

Tagged: ,

Comments