Reputation: 79
my following script doesn't show the number of fb-page-likes anymore...
$ch = curl_init("http://graph.facebook.com/FACEBOOKPAGE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
$data = json_decode($raw);
$scm_fb = $data->likes;
echo $scm_fb;
I tried also the following code, but this also displays nothing...
$page_id = "FACEBOOKPAGE";
$likes = 0; //Initialize the count
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
$likes = $json_output->likes;
}
(I also tried the Facebook-Page-ID instead of the pagename, result is the same)
any suggestions?
Upvotes: 0
Views: 15053
Reputation: 11440
You can get any graph object or url likes by parsing Facebook Like Widget source code using this php snippet:
<?php
$id = "youtubetabapp";
// customize: "https://facebook.com/[customize]"
$url = "https://facebook.com/$id"; // by name
//$url = "https://facebook.com/282374908483303"; // or by id
// customize: "locale=[customize]&" to your needs
$ch = curl_init("https://www.facebook.com/v2.5/plugins/like.php?locale=$locale&href=".$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Safari');
$html = curl_exec($ch);
//die( var_dump( $html ) ); // <- debug fetched data
// have a look for the data you need and customize your regex pattern accordingly
// "preg_match([customize], $html, $match);"
preg_match("/([0-9,.]+[a-zA-Z,\S]) people like this./", $html, $match);
$likes = $match[1];
//$likes = str_replace(array(".", "K"), array("", "000"), $likes); // <- customize output | remove ".", replace "K" with "000"
//$likes = (float)$likes; // <- customize output | change type to float, strips prepending non-digits (eg: "K") as well
die( $likes );
?>
Compare to API methods you don't need to provide any tokens and it takes one single request. Based on my tests — you only need to keep locale
parameter to make sure that you have constant source code for parsing.
Upvotes: 6
Reputation: 2642
You can use graph api to get the details of the page with corresponding access tokens. If you already have a valid access token, please use the below code to get the page details.
$fb_page = '';
$access_token = '';
$url = "https://graph.facebook.com/v2.2/".$fb_page.'?access_token='.$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$details = json_decode($result,true);
echo "Likes:".$details['likes'];
Upvotes: 4
Reputation: 1022
You can't get any page like without providing your access_token to the Facebook Graph API.
Your URL should be like this
https://graph.facebook.com/v2.2/{YOUR_PAGE_ID}?fields=like&access_token={YOUR_ACCESS_TOKEN}
If you want to learn about how to use Graph API and how to generate your access_token try following links.
FACEBOOK GRAPH API - Pages
https://developers.facebook.com/docs/graph-api/reference/v2.2/page
Access Token - Pagetoken
https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens
GRAPH API EXPLORER
Upvotes: 3