问题
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
});
};
回答1:
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, '-_', '+/'));
}
回答2:
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
}
来源:https://stackoverflow.com/questions/9014466/facebook-edge-create