Search code examples
facebookfacebook-fqlfacebook-page

How to check if user is a fan of facebook page by using FQL?


I want to check if user is a fan of a specific facebook page, I have tried many times with FQL but the response is always null:

FB.api({
        method:     'fql.query', 
        query:  'SELECT uid FROM page_fan WHERE uid=user_id AND page_id=page_id'
    }, function(resp) {
        if (resp.length) {
            alert('A fan!')
        } else {
            alert('Not a fan!');
        }
    }
);

I've tried with user table is running good:

FB.api(
          {
            method: 'fql.query',
            query: 'SELECT name FROM user WHERE uid=me()'
          },
          function(response) {
            alert('Your name is ' + response[0].name);
          }
        );

I've just checked in https://developers.facebook.com/docs/reference/fql/page_fan/. It seems that facebook does not support FQL, so what can I do ?

I really appreciate your help.

UPDATE I've just tried Graph API solution but the result is array null:

FB.api('/100005548217775/likes/329849167104448',function(response) {
                    if( response.data ) {
                        if( !isEmpty(response.data) )
                            alert('You are a fan!');
                        else
                            alert('Not a fan!');
                    } else {
                        alert('ERROR!');
                    }
                }); 

Solution

  • If you don't have an v2.0 app, you can't use FQL at all, because it's deprecated. Furthermore, you're using the wrong method parameter, which is also deprecated.

    The official endpoint for FQL is

    /fql?q={url_encoded_query}
    

    where {url_encoded_query} is the url encoded FQL query.

    I'd recommend you skip FQL, and go with the standard Graph API funcitonality (which is future proof):

    /{user-id}/likes/{page-id} 
    

    Note that you'll need the user_likes permission in the access token to be able to get any result.

    See