Topic-icon Check fan Page like

Active Subscriptions:

None
11 years 5 months ago #49790 by fb_100000532508192
Does anybody have code examples in both php and javascript, using jfbconnect lib. That will check if the current logged in user(logged in using fb connect) is a fan of page ??? . I do have code already:

//facebook page id
$page_id = "322357214479732";

//checking if user is logged in and then get fb user id
if ($fbid = JFBCFactory::provider('facebook')->getMappedUserId()){

//check if current user is fan of page, if not then show like box
$fq = "SELECT uid FROM page_fan WHERE page_id = ".$page_id."and uid=".$fbid;
$fbparams = array('method' => 'fql.query', 'query' => $fq);
$result = $jfbcLibrary->api($fbparams);
if(search_array($fbid, $result)){
echo'something
}

But i wonder if there is a JavaScript equivalent which is future proof?

Thanks in advanced for the help
The topic has been locked.
Support Specialist
11 years 5 months ago #49793 by alzander
Replied by alzander on topic Check fan Page like
First off, FQL queries are deprecated and will no longer work sometime over the next year. Looking at some examples on the web, specifically from here , it looks like you can use the following Graph API query:
GET /{user-id}/likes/{page-id}
To implement that, it would look *something* (untested) like:
//facebook page id 
$page_id = "322357214479732"; 

//checking if user is logged in and then get fb user id 
if ($fbid = JFBCFactory::provider('facebook')->getMappedUserId()){ 

    //check if current user is fan of page, if not then show like box 
    $result = $jfbcLibrary->api($fbid . '/likes/. $page_id); 
    if ($result['data']) // May need to look for a specific value in here or if the count of ['data'] is > 0 may be good.
        echo 'Yup, they like it';
}

The nice thing about the Graph API is that once that works in PHP, it's a similar syntax for Javascript. So, something like below *should* work (again, untested):
FB.api('/me/likes/322357214479732', function(response) {
  console.log(response);
  if (response['data'])
     alert("Yup, they like it");
});
For more info on the Javascript API calls, see the FB documentation .

I hope that helps get you started. Again, the above probably won't work immediately, but should put you on the right path. Both are also very future proof (for at least the next 2 years they should work).

Thanks,
Alex
The topic has been locked.
Active Subscriptions:

None
11 years 5 months ago #49796 by fb_100000532508192
Hi,

Thanks for the code. php works javascript give access token error. tried on new browser install of chrome and same error. cache is disabled.

Any ideas?
The topic has been locked.
Support Specialist
11 years 5 months ago #49801 by alzander
Replied by alzander on topic Check fan Page like
From the Access Token documentation page, they note:

The Facebook SDK for Javascript generates and persists access tokens automatically. You can learn more about starting the login process in our guide: Getting Started with Facebook Login for Web. You can retrieve the access token by making a call to FB.getAuthResponse which will include an accessToken property within the response.

Depending on how JFBConnect is configured for login (using popup or not), the token may not be generated yet.

You may want to try adding:
FB.getAuthResponse();
Before you're code, which should fetch and store the token to be used automatically later. More info on the getAuthResponse call here .

I hope that helps, but again, this is untested (and getting a little further away from things I'm really familiar with).

Alex
The topic has been locked.
Active Subscriptions:

None
11 years 5 months ago #49845 by fb_100000532508192
Hi Alex,

I've figured out the problem. the fb.init that jfb uses is a cached version of session token. this is due to fb.init (status) being false(i assume this is due to less calls being made to fb with am impact on page load times). now to get an updated version of the session token, you need to run the FB.getLoginStatus function. But it appears that all code that needs the latest token has to run within the function as apposed to the session table being updated with the most recent token.

So i guest my question is. How do you update jfb with the new token from javascript?

Many thanks in advanced,

Phil
The topic has been locked.
Active Subscriptions:

None
11 years 5 months ago - 11 years 5 months ago #49846 by fb_100000532508192
Also found this article which seems to have a very good way of handling tokens with php and javascript. not sure if you guys implement sessions this way?

www.sammyk.me/access-token-handling-best...-facebook-php-sdk-v4
Last edit: 11 years 5 months ago by fb_100000532508192.
The topic has been locked.
Support Specialist
11 years 5 months ago #49851 by alzander
Replied by alzander on topic Check fan Page like

. this is due to fb.init (status) being false(i assume this is due to less calls being made to fb with am impact on page load times).

Ahh.. yeah, if you have the "Automatically Log In Facebook Users" setting disabled, we set status:false. It has a minor performance impact when enabled and since we don't need to the Javascript token updated on every page load, we take advantage of that. We're very performance conscience :)

So i guest my question is. How do you update jfb with the new token from javascript?

There's a few options:
* You can edit our code to set status:true in the /components/com_jfbconnect/libraries/provider/facebook.php file.
* You can use the getLoginStatus call that you mention, like we do in our jfbconnect.js file after a user tries to authenticate.. for example:
FB.getLoginStatus(function (response)
{
   if (response.status === 'connected')
  {
      // DO SOMETHING
  }
}

Also found this article which seems to have a very good way of handling tokens with php and javascript. not sure if you guys implement sessions this way?

Unfortunately, we can't update to the latest Facebook SDK yet because it requires PHP 5.4. That's higher than the minimum level for even Joomla 3.x, which means we likely won't be updating to that version anytime soon.

With that said, we do have different ways to fetch and use an access token for the user. When you use the JFBCFactory::provider('facebook')->api(..) method, we'll automatically insert the user's token into the call so you don't need to. We also update the user's token automatically during login to make sure the most recent is stored in the database. Etc, etc.

Hopefully the above all helps you, but should you need anything else, just let us know.

Thanks,
Alex
The topic has been locked.