Moved to: WoodyHayday.com
Woody Hayday

Fresh Ideas

Hi there! This is my old blog, I don't hang around here much.
You can now find me trying to mix things up here:
Visit WoodyHayday.com Subscribe To My Email List

Facebook Graph Api – Is user a fan of page id – Using FQL and opengraph to mimic Pages.IsFan

Working with integration of Facebook Connect for authentication is mostly easy at the moment, once you have got your head around FBML etc. However as they are in between switching from the old Restful API and the new graph API there remains the odd little thing that has only a splatter of useful documentation online. The first one of these I have come across is trying to find out if a user (who is logged in to  your site via facebook connect) is a fan of page X.

There is infinite uses for this – with facebook more and more integrating “likes” with their search and other stuff you will need this functionality to drive traffic from facebook. Anyway this used to be the easy case of using https://api.facebook.com/method/pages.isFan?access_token=… to get a 1 or 0 depending on whether or not the user had “fanned” the page/website/whatever.

But then came along the big bad Graph API, which to get this IsFan functionality is a bitch. Getting the “likes” directly works for the page owner (which confuses everything) but not for third parties, showing just the popular/main category “likes” against them (and you need extended permissions.)

Long and short of it there’s about 5 ways to skin this cat, all of which suck except this one, in my brief, frustrated look into it. In any case this uses FQL and the Graph API call to produce a Boolean indicating whether or not the user has fanned something with id $pageID. It works, in all scenario’s for me so far.

$likeID = $facebook->api(
array( ‘method’ => ‘fql.query’, ‘query’ =>
‘SELECT target_id FROM connection WHERE source_id = $userID AND target_id = $pageID’ )
);

if ( empty($likeID) ) {
// user has NOT Liked the page/whatever
} else {
// user HAS Liked the page/whatever
}

If only facebook would update their documentation to show how easy it is to fql query from the API!

This entry was posted in Facebook Graph API, Facebook Pages, PHP, Social Media, Web Development and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

23 Comments

  1. Posted November 23, 2010 at 9:14 am

    Thank you very much for the tip! BTW, is there a way to check whether certain user has write something (leave a comment) on certain page? Thank you!

  2. Posted November 23, 2010 at 11:33 am

    Sure it is – its all FQL man. Will let you know when I get back to FB integration.

  3. Posted December 5, 2010 at 6:33 am

    if this works I’m going to be very happy, I’ve been trying to figure this out all weekend, you’re right there’s not a lot written about it, on the facebook .isfan page they mention that this will be worked into the Graph API but in the meantime use this old method… it’s amazing to me that there’s actually a way to do it that you’ve found and they can’t just put this on the page! I guess when you’re rolling in money and have a movie by David Fincher made about your company you don’t need good API documentation anymore…?

    brent
    @
    mimoymima.com

  4. Posted December 5, 2010 at 6:49 am

    arg… just realized that this is php… I’m using the javascript API, this is for a wordpress site, so I can use PHP, I’m just not so familiar with facebook’s API’s and the javascript one had a good tutorial that got me up and running pretty quickly… Can I use both? At the moment I’m loading the javascript Graph API asynchronously and getting the current user’s ID, I was going to use your bit here to find out if they were a fan of a page…

  5. xainee khan
    Posted December 11, 2010 at 2:03 am

    thanx man……
    it worked….

  6. xainee khan
    Posted December 11, 2010 at 2:05 am

    $likeID = $facebook->api( array( ‘method’ => ‘fql.query’, ‘query’ => “SELECT source_id FROM connection WHERE source_id =’$uid’ AND target_id = ‘1xxxxxxxx9′” ) );
    but the correct is this bro

  7. Posted December 13, 2010 at 3:30 pm

    Yeah its in php Brent, but its worth learning a bit of that I would say – otherwise how are you saving things serverside? .net? or not?

    Xainee both work man 😉

  8. Posted December 20, 2010 at 5:12 pm

    Hi there Woody, this works a treat so many thanks for that 🙂
    However am wandering if you can help me take it a little further. Basically running an iframe canvas app – to the header have an xfbml like box – further down the page have your code, if user is fan already they get to see X, if not they get to see Y (that part works great), but how can I get it so that once a user clicks like the FQL call is made again, will then realise the user is now a liker, and then display the new content? At the moment if I do a refresh after liking then your code works a treat

    Many thanks Tony

  9. Posted December 20, 2010 at 5:15 pm

    Hi Tony,

    If it works then it works? lol
    Sure there is a way, if you want to do things without refreshing you’ll have to use javascript/ajax instead of just strait php – sure its doable if its worth the dev time.

  10. SWengr
    Posted December 22, 2010 at 11:23 am

    Thanks Bro!
    This works very well. But I want to ask you whether there is a way to find all the “likes” of a particular user? (means what pages etc that user has liked, ALL those). Please reply.
    Regards.

  11. Posted December 30, 2010 at 7:35 pm

    Sure there is definitely ways – I had that working once they login on your site – but I found it more temperamental than this option – perhaps will relook at this when I need too in 2011!

  12. Paul Anthony
    Posted January 3, 2011 at 6:43 pm

    Hi Woody. This is useful, however how does one get access to the userId variable? Particularly if you are using FBML tabs, I can’t see any way to query the user looking at the page.

  13. Posted January 12, 2011 at 3:05 pm

    Not sure with FBML tabs dude…

  14. Chris Kemm
    Posted January 24, 2011 at 6:38 pm

    Hi

    I can’t get this script running? I’ve added it to my canvas .php app but I always get user is not a fan response?

    Any ideas?

    My code below:

    ‘1*************0’,
    ‘secret’ => ‘a******************************f’,
    ‘cookie’ => true,
    ));

    array( ‘method’ => ‘fql.query’, ‘query’ =>

    ‘SELECT target_id FROM connection WHERE source_id = $userID AND target_id = $pageID’ );

    ?>

    Sorry you need to be a fan to enter

    You’re a fan

  15. Posted January 28, 2011 at 12:59 pm

    Hi Chris,

    Post the full script and I will have a guess, my original code still seems to work for me..

  16. Nick
    Posted January 31, 2011 at 9:06 pm

    Good day to u,

    I am using this code but i am continuously getting errors I am using the above code as it is. Do i have to replace anything please help. I am pretty new to Facebook and does not have much knowledge.

    Regards,

  17. Posted February 2, 2011 at 2:44 pm

    Good day Nick!

    I got your email also, script looks ok on first glance – what are the errors you are seeing? using PHP5? did your facebook initiating work ok?

  18. Sean
    Posted February 15, 2011 at 4:20 pm

    Thanks for this, it has saved me a lot of time and hair pulling. Of course the next problem I have is how to get the user_id of another user in order to run the query. “$uid = $facebook->getUser();” works when it is me that is viewing my page – because I authorized the app when i added it to my page, but this wont work for other users. Any ideas?

  19. Posted February 15, 2011 at 5:39 pm

    I guess it may only work in that situ, I only used it in an approved app situation so you may have to investigate another way. If I do any more work on this I will post here 😉

  20. Shiv
    Posted August 9, 2011 at 11:01 pm

    Hey, I am trying to tell if someone has liked a page before. Not a fan page but a normal page with a like button embedded. Is there a way to tell if the person liked that page in the past?

    Thanks!

  21. Posted August 10, 2011 at 1:54 pm

    Google it dude, sure its possible, seen it used.

  22. Posted January 26, 2012 at 5:50 am

    If you are dealing with facebook app tab page, following code help you to find isfan status

    $signed = parse_signed_request($_REQUEST[‘signed_request’], ‘YOUR-APP-SECRET’);
    if ($signed[‘page’][‘liked’] == 1) {
    $fan = true;
    } else {
    $fan = false;
    }

  23. Posted January 29, 2012 at 3:51 pm

    Yeah this has probably changed since it was written. Cheers Joby.

Woody Hayday

Comments Archive

Hi there. This is my old blog and it's archived, so you can no longer post new comments on this post (Facebook Graph Api – Is user a fan of page id – Using FQL and opengraph to mimic Pages.IsFan).

Read my new blog about writing software and stories at WoodyHayday.com

The New Blog
WoodyHayday.com
A Quote..
"Be fearful when others are greedy, and greedy when others are fearful."
Warren Buffett
Old Random Projects
    © Woody Hayday 2008-2024