How to get Facebook page followers and likes count - php

I need to make an PHP application where anybody can define a page name and get the public information of that page, specifically the followers count.
For example, Google's Facebook page (https://www.facebook.com/Google/) has 28.318.253 likes and 33.390.596 followers. How can I, programatically, get those numbers?
I want my final code to look something like this:
$page = "google";
// Some code logic, API calls or anything else
$likes = $pageData->likes_count;
$followers = $pageData->followers_count;
echo $likes; // Should output "28318253"
echo $followers; // Should output "33390596"

Related

I want to access followers of this page, but it is only showing this much of data

I am trying to access the followers/likers of this page but it is showing only this much of data. How can i access all the info of this page.
category "Utilities"
link "http://localhost/cricshots/"
name "cricketshotsofficial"
id "1996682000635998"
Trying to get the access using this code:
$json_url = 'https://graph.facebook.com/' . $facebook_page_id.'?access_token=' . $access_token ;
Expect the output to be the complete details of this page or only the followers of the page
This is an updated and working code snippet which can be used to retrieve Facebook like count of a specific page. To use this code snippet you’ll need a Facebook App ID and Secret Key.
<?php
function fbLikeCount($id,$appid,$appsecret){ // $id = Fb Page id
$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret.'&fields=fan_count';
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->fan_count){
return $fan_count = $json_output->fan_count;
}else{
return 0;
}
}
echo fbLikeCount('coregenie','___APPID___','___APPSECRET___');
?>
Please edit your page name, app id, app secret.

php order that nested loops run in

I have a piece of code that retrieves data from the Facebook api. Basically what happens is I loop over all the posts in our own database. For each posts it will try to get all shares for that post from the Facebook api. The Facebook api retrieves an object with a next link for the next batch of shares for that posts and it will keep looping till it gets all shares and go on to the next post.
The code looks as follows (Note that this is not my actual code because it has way to many things going on that are not important or needed for my question):
// SELECT all posts from my database
$sql = "SELECT * FROM posts";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
try {
//this is our api call to get all share posts
$response = $fb-get($row['postId'] . "someApiEndPointForSharedPosts");
//start processing the response for the post
processFeed($response);
} catch (Exception $e) {
//There is no response or there is another error
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
}
function processFeed($shares){
//Go through all shares in the response and do some modification and storage etc
foreach($shares as $share){
//Do something with the share
}
//Facebook sdk method to get the next batch of shares for the post (if there are any left)
$nextFeed = fb->next($shares);
//If there are more shares run this function again with the new batch
if(isset($nextFeed)){
processFeed($nextFeed);
}
}
Now my question is. Will the code run in the following order:
Select all post
Do the api call for the first post (which returns an response)
Start process feed with the response for the first post
Check if there is a next feed for the first post
If there is run process feed agian till there is no next feed left for the first post
Start over by doing the api call for the second post and do the above process agian.

facebook is this feature exists?

I have a facebook page
https://www.facebook.com/Best.Brain.Teasers
and my website
http://gpuzzles.com
I have read ob facebook link below
https://developers.facebook.com/docs/plugins/comments/
The Comments box lets people comment on content on your site using their Facebook profile and shows this activity to their friends in news feed. It also contains built-in moderation tools and special social relevance ranking.
can i link my facebook page with my website
i.e all comments posted on my fb page wall automatically come under my post on webiste/blog.
Yes you can do that.
Just paste the below code in your website where you need to post. Just make sure your server is Curl enabled and edit the code to fit in your facebook id.
<ul>
<?php
//function to retrieve posts from facebook’s server
function loadFB($fbID){
$url="http://graph.facebook.com/".$fbID."/feed?limit=3";
//load and setup CURL
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
//get data from facebook and decode JSON
$page = json_decode(curl_exec($c));
//close the connection
curl_close($c);
//return the data as an object
return $page->data;
}
/* Change These Values */
// Your Facebook ID
$fbid = "YOUR_FB_ID";
// How many posts to show?
$fbLimit = 10;
// Your Timezone
date_default_timezone_set("America/Chicago");
/* Dont Change */
// Variable used to count how many we’ve loaded
$fbCount = 0;
// Call the function and get the posts from facebook
$myPosts = loadFB($fbid);
//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
//only show posts that are posted by the page admin
if($dPost->from->id==$fbid){
//get the post date / time and convert to unix time
$dTime = strtotime($dPost->created_time);
//format the date / time into something human readable
//if you want it formatted differently look up the php date function
$myTime=date("M d Y h:ia",$dTime);
?>
<ul>
<li><?php echo($dPost->message) . $myTime; ?></li>
</ul>
<?php
//increment counter
$fbCount++;
//if we’ve outputted the number set above in fblimit we’re done
if($fbCount >= $fbLimit) break;
}
}
?>
</ul>

how to display latest recent posts in my facebook page to my website

i have page on Facebook and I want to display latest 5 posts from my feed/wall on a page to my website. How to do this? I found this solution.. it is easy
https://developers.facebook.com/docs/reference/plugins/like-box/
and someone guide me to use facebook api and do it myself
what is the best way?
I use php mysql to build this site
Here is the PHP code. You need to place this in your template.
<ul>
<?php
//function to retrieve posts from facebook’s server
function loadFB($fbID){
$url = "http://graph.facebook.com/".$fbID."/feed?limit=3";
// Update by MC Vooges 11jun 2014: Access token is now required:
$url.= '&access_token=YOUR_TOKEN|YOUR_ACCESS_SECRET';// *
//load and setup CURL
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
//get data from facebook and decode JSON
$page = json_decode(curl_exec($c));
//close the connection
curl_close($c);
//return the data as an object
return $page->data;
}
/* Change These Values */
// Your Facebook ID
$fbid = "190506416472588";
// How many posts to show?
$fbLimit = 10;
// Your Timezone
date_default_timezone_set("America/Chicago");
/* Dont Change */
// Variable used to count how many we’ve loaded
$fbCount = 0;
// Call the function and get the posts from facebook
$myPosts = loadFB($fbid);
//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
//only show posts that are posted by the page admin
if($dPost->from->id==$fbid){
//get the post date / time and convert to unix time
$dTime = strtotime($dPost->created_time);
//format the date / time into something human readable
//if you want it formatted differently look up the php date function
$myTime=date("M d Y h:ia",$dTime);
?>
<ul>
<li><?php echo($dPost->message) . $myTime; ?></li>
</ul>
<?php
//increment counter
$fbCount++;
//if we’ve outputted the number set above in fblimit we’re done
if($fbCount >= $fbLimit) break;
}
}
?>
</ul>
Two things you must do for working out this script.
Make sure your server is cURL enabled
You will have change the Facebook ID in the script by yours.
* You can get the access token this way:
$token = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials';
$token = file_get_contents($token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
Login to facebook
Go to facebok developers section "Apps"
Register new app, you need only to register new app, all additional data is optional
Copy your App ID/API Key and App Secret from that same "Apps" section.
Copy facebook.php and base_facebook.php files from repo to your server
Use polymorphic query to api, to request wall content from facebook account
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
$fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5');
if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) {
// display contents of $fbApiGetPosts["data"] array
}
Replace YOUR_APP_ID with your app ID, YOUR_APP_SECRET with your app secret and YOUR_FACEBOOK_ACCOUNT_ID with target facebook account, you want to get posts from.
Polymorphic query basically is path/URL. More info inside previously mentioned facebook api reference docs.
If your target facebook account wall is public, you won't need anything else that this, to view them.
I had trouble with Okky's answer here, and I found a possible, albeit not an ideal workaround.
Use an RSS feed of your Facebook wall, then simply parse it with an RSS reader of your choosing.
https://www.facebook.com/feeds/page.php?format=rss20&id=YOUR_UNIQUE_ID
Here is a quick way to get your ID
So to mix up Okky and Deele answer, that both help me out, you must end with something that will look like this. I also add an anchor tag to link to the post url :
<?php
$fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5');
if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) {
//loop through all the posts we got from facebook
foreach($fbApiGetPosts["data"] as $dPost){
//only show posts that are posted by the page admin
if($dPost["from"]["id"]==$fbid){
//get the post date / time and convert to unix time
$dTime = strtotime($dPost["created_time"]);
//format the date / time into something human readable
//if you want it formatted differently look up the php date function
$myTime=date("M d Y h:ia",$dTime);
?>
<li><a href="<?php echo($dPost["link"]); ?>">
<?php echo($dPost["message"]) . "<br>" .
$myTime; ?></a></li>
<?php
}
}
}
?>

Checking if a SoundCloud song is embeddable

I'm trying to use the SoundCloud API to check if a song is embeddable, is this the same as streamable? My code looks like this:
$trackid = $track['id'];
$username = mysql_real_escape_string($track['user']['username']);
$title = mysql_real_escape_string($track['title']);
$downloadable = $track['downloadable'];
$streamable = $track['streamable'];
// Check if streamable
if(!$streamable) {
header( 'Location: error.php' );
} else { ...
I can get the trackid, username, title & downloadable easily, but is streamable the correct property to look for? I'm talking about when you go to embed a song and it says "Oops this track can't be played outside of SoundCloud." I want to make sure this doesn't happen and unallow a song if it's one of those songs.
Referring to the documentation the streamable proprety exists. And yes it's the right one.

Categories