1

I have added the edge.create event and the event can be fired by the browser as well. But how can I check if the user has liked the page when they come back to the site?

<div id="fb-root"></div>
<script type="text/javascript">
<!--
window.fbAsyncInit = function() {
 FB.init({appId: 'YOUR_FACEBOOK_APP_ID', status: true, cookie: true, xfbml: true});
 FB.Event.subscribe('edge.create', function(href, widget) {
 // OK
 });
};
Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
Denny
  • 11
  • 2

2 Answers2

2

You are going to want to take a look at the signed_request documentation...

This signed request is facebooks method of validating that the user that made the request is indeed "who he/she says they are". It is encrypted and and uses your application secret to decode the values.
Once you parse this signed request you will have the data you need.

function parse_signed_request($signed_request, $secret) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    error_log('Unknown algorithm. Expected HMAC-SHA256');
    return null;
  }

  // check sig
  $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  if ($sig !== $expected_sig) {
    error_log('Bad Signed JSON signature!');
    return null;
  }

  return $data;
}

function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}
Lix
  • 47,311
  • 12
  • 103
  • 131
  • Signed requests are only available for page tabs running in facebook, not stand-alone sites (afaik) – PanMan Mar 28 '14 at 13:18
1

If your question is about like for URL that you can get this information only for user who authorized your application and granted user_likes permission. To do so issue next FQL query:

SELECT user_id, url FROM url_like WHERE user_id = me() and url="URL_OF_PAGE"

If your page have an ID in OpenGraph (or you speak about Facebook Page), by querying Graph API for user's likes connection:

GET https://graph.facebook.com/me/likes/OPEN_GRAPH_OBJECT_ID

But if you speaking about Facebook Page and Application running in Page Tab, you don't need anything special to get this information since it will be passed within signed_request (sample php code using PHP-SDK):

$signedRequest = $facebook->getSignedRequest();
$data = $signedRequest['data'];
if ($data['page']['liked']){
  // User is liked this Facebook Page
} else {
  // User is not yet liked Facebook Page
}
Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93