Getting just number of facebook likes - php

I'm curious to use the facebook graph API as I don't like the simple like button with number of likes next to it. However I'm finding their documentation a bit confusing. I want to do this via PHP so I'm assuming I need the PHP SDK, is that right?
I'm looking at this page Graph API: Page however I don't understand how I would call the likes.
I'm not asking for someone to code this for me, just a little help in the right direction to understand how it works :)

Try this:
$json = #file_get_contents("http://graph.facebook.com/yourpage");
$json1 = json_decode($json,true);
$likes = $json1['likes'];
echo $likes;

This is pretty straightforward. Using the ID or the username of the page, access that
node via the Graph API:
PAGE_ID?fields=likes
In PHP would be something like:
$token_string = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&client_secret='.$app_secret.'&grant_type=client_credentials');
$likes_response = file_get_contents('https://graph.facebook.com/PAGE_ID?fields=likes&'.$token_string);
$likes_obj = json_decode($likes_response);
$likes = $likes_obj->likes;
echo 'Likes count for page: ' . $likes;

Related

how to fetch a page's likes from fb by php

i want to make a social media audit tool and i want to get likes of a person's Fb page. Basically what i want is i want that person to enter his FB pages URL and then i want to fetch the likes of his page and echo it in my PHP page. Is there any way I can do that without using graph API or any API. I just want a simple piece of code.
I have searched many questions regarding my project on web and on StackOverflow as well but coudn't find what i wanted, at last, I am asking this question. Is there anyone who can help me regarding this?
Thanx in advance.
<?php
$file = "https://www.facebook.com/IntellectualIndies/?epa=SEARCH_BOX";
$data = file_get_contents($file);
preg_match_all ('~<div class=\'_4bl9\'>\s*(<div.*?</div>\s*)?(.*?)</div>~is', $data, $matches);
$content = $matches[1];
$total = count($content);
for($i=0; $i<$total; $i++){
echo $content[$i]."<br />";
}
?>
i tried this code.
Is there any way I can do that without using graph API or any API
No, scraping is not allowed on Facebook.
There is only one way to do this:
Apply for Page Public Content Access
Use the Graph API with the following endpoint/field: /page-id?fields=fan_count
API Reference with example code: https://developers.facebook.com/docs/graph-api/reference/page/

Upgraded to PHP 5.4 and now facebook link stats I was pulling no longer show on site

I've been running a photography site for just over two years and displayed stats about how many facebook likes and shares each image page has accumulated underneath each image.
The code I have used to get this info and put it into the variables is this:
<?php // Get Facebook Stats for Shares Likes Comments etc...
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($this->canonicalurl);
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
$shares = number_format($xml->link_stat->share_count);
$likes = number_format($xml->link_stat->like_count);
$comments = number_format($xml->link_stat->comment_count);
$total = number_format($xml->link_stat->total_count);
?>
All was fine until today I switched from PHP5.2 to 5.4 and suddenly all the stats no longer appear. I've read around a little bit and some people were saying that I need to set allow_url_fopen = 1 but that was already set.
Someone else was saying that it may have something to do with using file_get_contents, but I got no concrete result.
Can anyone please shed some light on what might be wrong? Is there another way to write the above without using file_get_contents that would get around this issue if that is to blame?
Thanks!
The error here is that the number_format needs to be a double, but $xml is an object. You can just remove number_format, but I recommend intval for it.
<?php // Get Facebook Stats for Shares Likes Comments etc...
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($this->canonicalurl);
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
$shares = intval($xml->link_stat->share_count);
$likes = intval($xml->link_stat->like_count);
$comments = intval($xml->link_stat->comment_count);
$total = intval($xml->link_stat->total_count);
?>

friend list in facebook

I am trying to fetch friend list. I wrote following code..
$friends = $this->facebook->api('me/friends');
echo count($friends);
Answer was 1. (though it should have been 456). What am I doing wrong? Thanks for help.
The data structure returned by the API is different than you think.
Try echo count($friends['data']); instead.

Facebook Graph API Search without logged in user

I'm trying to issue a Facebook Graphp API search call for groups with a specific search term. The important fact is to search for groups not only beginning with the term but also to search for the term within the group name. So something like that.
FQL => SELECT * FROM groups WHERE groupname LIKE '%term%'
As far as i know this isn't possible in FQL. Therefore I have to use the Graph API search.
http://developers.facebook.com/docs/api#search
But I have to issue th call even if the user isn't logged in. Is this possible
or is there a possibility to log in a default user with some curl calls without user interaction (without displaying a form)?
If there is a simplier solution (for instance with FQL) please tell me.
I work with the graph API and not with the FQL so I'm not 100% sure on the differences etc etc... however a way I'd try is using the cURL script at http://developers.facebook.com/docs/authentication/#authenticating-users-in-a-web-application to get an auth token, then you must add the received auth token to the graph parameters as you can see on the site (a good way to test in the graph api is to click on the example likes or queries given while logged into facebook then copying the api code and breaking up the process like that [testing that you can get results you want from the query, testing that you can get your auth token then combining] so that you know what parts are 'problem' parts).
If that fails, scroll down to the "single sign-on with java-script" and have a look at the code they use to get the auth token from the facebook cookie, I notced you may be able to access that cookie from FQL.
I hope this was of some help! Please tell me if those ideas didn't work.
Jon
Using PHP-SDK 3.1.1, no auth or access_token needed. This sample assumes you have PHP-SDK installed on the page.
Use a form post the question to the page, arguments for get and post
are included. This will return the array from search https://graph.facebook.com/search?q=facebook&type=group
<?php
$q = urlencode($_GET['qs']);
if(!$_GET['qs']){
$q = urlencode($_POST['qs']);
if(!$_POST['qs']){
$q = "facebook";
}
}
$MEsearch = $facebook->api('/search?q='.$q.'&type=group');
foreach ($MEsearch as $key=>$value) {
$i=1;
foreach ($value as $fkey=>$fvalue) {
$i++;
if($fvalue[id]=="h"){
}else{
$groupname = $fvalue[name];
$groupid = $fvalue[id];
$groupversion = $fvalue[version];
echo $groupname. '<br />';
echo $groupid. '<br />';
echo $groupversion. '<br /><hr />';
}
};
};
?>
Sample Usage http://shawnsspace.com/plugins/photofeed.php Click Get Plugin at the bottom and use the search box to see this sample in action.

can I use a number of "facebook likes" in php?

I am using facebook like in my site, and my php script need to know how many people like every page. For example I want to use in php echo $likes; where $likes is a number of likes. Is it possible to do?
Using the new Graph API this is fairly straight forward.
Just do: https://graph.facebook.com/uniqueid/members
Using the new SDK library (http://github.com/facebook/php-sdk/) do the following:
$facebook = new Facebook($appId,$secret);
$facebook->api('uniqueid/members');
This should return a JSON array which you can iterate.

Categories