Reputation: 1
In drupal I want to show the amount of likes our page has.
I know you can use the facebook graph utility, but i don't know how to put in correct code, for a html "snippet". Anyone some ideas?
Upvotes: 0
Views: 347
Reputation: 131
easiest way is to enable "php code" text filter, create node and choose "php format", than in node body put updated Arlina's code:
$graph = 'https://graph.facebook.com/< your facebook page id>/?fields=likes&access_token=< your application access_token >';
$response = file_get_contents($graph);
$json = json_decode($response);
try {
if (!isset($json->likes)) {
throw new Exception('Like count not found in facebook graph object');
}
$likes = $json->likes;
echo $likes;
} catch (Exception $e) {
// handle error
}
finally save the node.
Upvotes: 0
Reputation: 1
You can use the following PHP snippet in a block or any other place you are allowed to use php:
$graph = 'https://graph.facebook.com/< your facebook page id>/?fields=likes';
$response = file_get_contents($graph);
$json = json_decode($response);
try {
if (!isset($json->likes)) {
throw new Exception('Like count not found in facebook graph object');
}
$likes = $json->likes;
// do something with $likes
} catch (Exception $e) {
// handle error
}
Upvotes: 0
Reputation: 346
I'm not familiar with Drupal - but this is how you would do it via HTML + JS:
window.fbAsyncInit = function() {
FB._https = true;
// init the FB JS SDK
FB.init({
appId : YOUR APP ID, // App ID from the App Dashboard
channelUrl : 'channel.html', // YOUR CHANNEL HTML
status : true,
cookie : true,
xfbml : true
});
getPage();
};
(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false))
getPage = function() {
var pageID = 'http://google.com'; //put your site here
var pageUrl = 'https://graph.facebook.com/'+pageID;
FB.api(pageUrl, 'get', function(response) {
alert( response.likes )
});
}
Upvotes: 1