Determine if a user has liked my page using Facebook PHP SDK?

12,818

Solution 1

It can be done this way:

<?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 {                                       
                 //show something else                   
            }
        } 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 what ever you want if user hasn't liked your fan page.

Solution 2

You can do this using FQL. You'll also need to make sure that you have the user_likes permission set.

I pulled this example from an older app that is now offline, it may need to be changed depending on what Facebook has changed in their last round of updates. Lately I've been using javascript and I subscribe to the edge.create event.... just replace the page_id with your page's id and give it a try

$checkIfUserLikePage =  $facebook->api(array(
    "method"    => "fql.query",
    "query"     => "select uid from page_fan where uid=me() and page_id=1234567"
));
$checkIfUserLikePage = sizeof($checkIfUserLikePage) == 1 ? true : false;

Solution 3

This should work for you! I had to do a lot of these types of pages so I created a really simple way of creating like gated pages. https://github.com/DrewDahlman/FBVersion

Enjoy!

Solution 4

$signed_request = $_REQUEST['signed_request'];

    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) {

            $this->assign('verify', $signed_request->page->liked);
            } 
            else {
                echo "Please click on the Like button to view our Coupons!";
                jexit();
            }
    } 

I hope this will help you :)

Share:
12,818
Brent Deneau
Author by

Brent Deneau

Updated on June 04, 2022

Comments

  • Brent Deneau
    Brent Deneau almost 2 years

    My search has come up empty because when you search for "Facebook" and "like" I get all kinds of other results.

    I have an app that is only on my company's Facebook page. In that app I need to find out if the user has liked the company's page. I'll show one thing if not and another thing if so. How can I do this using the Facebook PHP SDK v.3.1.1?