Display Number of Facebook Likes as Text - php

I'm trying to create a widget like the Facebook widget found on this site. I'm not sure how they've done it but somehow they're pulling in the number of Facebook likes from their page via the API. I've found a few examples online but none seemed to work. Is it possible someone could point me in the direction of how I can display the number of Facebook likes (for a page) as text?
Please note that the site I need it for is Wordpress so either an existing plugin or just PHP code will work!

This is how you can do it in php:
$fb_handle = "GideonShalwickUpdates";
$graph_url = "https://graph.facebook.com/".$fb_handle."?fields=likes";
$get_data = file_get_contents($graph_url);
$get_json = json_decode($get_data);
$likes = $get_json->likes;
echo $likes;

Related

How do i get description and title of facebook pages, users in php?

I am using PHP function get_meta_tags to get several info from the different websites like description. When it comes to Facebook it doesn't show any type of description. It gives description for Facebook fan page but not for any users profile page.
$tags = get_meta_tags('https://www.facebook.com/zuck?fref=ts');
echo $tags['og:description'];
I am also facing problems for getting title of Facebook pages as well as users profile page using following script:
$str = file_get_contents('https://www.facebook.com/zuck?fref=ts');
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
Correct title as well as description:
Facebook profile page title as well as description:
You need an Access Token in order to get details of a User profile.
Information about Access Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
To get Faxcebook page details use below method
http://graph.facebook.com/pageid
or
http://graph.facebook.com/pageusername
Example http://graph.facebook.com/38282497425
The output will be in json format.then use any json decxoding methods .

Joomla 3.2.0 - Facebook comments counting dynamic url

Yesterday I had some trouble with Facebook comment for Joomla 3.2.0. And for today i have to count the article's counts separately. So have the code from facebook but can't get the data-href right.
<span>Comments:<fb:comments-count data-href="<?=JURI::current()?>"></fb:comments-count></span>
So this data-href="<?=JURI::current()?>" is have to be changed i think. All articles have their own facebook comments so now i need is to have their own comment counting.
Question2: If there is no comment it's get just blank like Comments: ... so is there anyway to display non commented articles to have like Comments: 0.
Try this,
https://api.facebook.com/restserver.php?method=links.getStats&urls=[URL]
You can use restserver.php for getting count of share,like,comment etc
<?php
$url = 'https://www.facebook.com/Walkswithme';
$xml = simplexml_load_file("http://api.facebook.com/restserver.php?method=links.getStats&urls=".$url);
$counts = $xml->link_stat->comment_count;
?>
for more details
where you need the comment count just use like <?php echo $counts;?>
Hope its helps..

Wordpress share twitter via twitpic

my question a little complicated.
I want to share a featured image which is in a post on my wordpress site to twitter.
But there a important point. i want to share a picture with twitpic because of people can see this image in twitter without open my web site.
i get api from twitpic, and i use this code (link)
but twitpic or this code cannot accept a url to upload twitpic and get a link.
$resp = $twitpic->upload(array('media'=>'$a2', 'message'=>'get_the_title()'));
if i wrote $a2 = "ex.jpg" its working,
if i wrote $a2 = "http://www.ex.com/ex.jpg" not working
then i try that;
<?php
$a1 = $_SERVER['DOCUMENT_ROOT'];
// ex image remove 18 chr from begining http://*******.com/wp-content/uploads/2013/04/2012-10-17-23.48.34.jpg
$kisalt1 = $imageshare[0]; // wordpress featured image fxn ex: http://*******.com/wp-content/uploads/2013/04/2012-10-17-23.48.34.jpg
$kisalmis = substr($kisalt1,18,9999);
$a2 = $a1.$kisalmis;
// echo $a2; => /home/*******/public_html/wp-content/uploads/2013/04/bu-terste-bir-islik-var-panpa.jpg
?>
but it stil give same error.
Unable to find or read file
how can i get ride of this?
summary => i want to this => wordpres featured image to twitpic for share twitter like image which can open without click a link.
Edit: I just tried deleting this because I saw that you said that http://www.ex.com/ex.jpg doesn't work for you. But then I thought this might still be helpful (to someone), and for some reason the comments option didn't show up for me, so here it is again. Sorry if it doesn't help though.
What you need is just the post_thumbnail url, not the full image tag.
http://wordpress.org/support/topic/getting-post-thumbnail-url
The relevant post is the one with this code block:
$a2 = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
Does it work if you add that?
-Andrew
Another edit...I just came across this post on the Wordpress StackExchange site: https://wordpress.stackexchange.com/questions/83137/post-thumbnail-relative-link-and-html-modify
Does it work if you provide TwitPic with a relative link location?

reading posts from a specific fanpage

I'm running in circle with this problem so I submit it here.
I have created a facebook fanpage and a website for my mother to promote her music. Since she is quite used to facebook I decided that instead of creating a whole backoffice, the website would simply display the fanpage's posts, photos and events. I do this with simple calls to
$data= $fb->api('/'.PAGE_ID.'/posts?fields=id');
for example (or 'notes', 'events'...). The problem is that I have to use a specific access token that I get from my own 'me/accounts/' permissions. From time to time (say every month) I have to go to the Graph API Explorer, get that token, and save it in the website config.
I'm wondering if I couldn't generate that access token server-side on my website, but I can't seem to make it happen. the facebook PHP SDK can't acces my api('/accounts') graph results unless I'm the authentified user (obviously), and a call to api(PAGE_ID.'?fields=access_token') only returns the page's ID.
What Am I doing wrong? can the facebook php sdk make api calls on my behalf (independently of users)?
More info : I created the fanpage, and a simple app called feed reader, with *manage_pages* permission. I'm the only user that authorized that app, and I don't want the user on the website to have to login (why should they? it's my fanpage, why would I need their *manage_page* permission?).
To be honest I kinda feel that public posts sould be public in the graph api too, but I'm sure there is a reason.
To be honest I kinda feel that public posts sould be public in the graph api too, but I'm sure there is a reason.
You can get to any users public posts like so:
Note: this assumes you are using the PHP SDK here http://developers.facebook.com/docs/reference/php/
$facebook = new Facebook(
array(
'appId' => 'fb_app_id',
'secret' => 'fb_secret',
)
);
$url = '/' . 'fb_username' . '/posts?fields=id,name,created_time,message,story,type&limit=10'; // you might need to screw with this some
$page = $facebook->api($url);
while(count($page['data']) > 0)
{
foreach($page['data'] as $fb_post)
{
// USE THEM DATAS
}
// Keep retrieving next pages until spent
$url_parts = parse_url($page['paging']['next']);
$url = '/' . 'fb_username' . '/posts?' . $url_parts['query'];
$page = $facebook->api($url);
}
this is an example from: https://github.com/fyaconiello/WP_Social_Network_Posts/ which pulls FB posts in and saves em as WP posts. It was written to answer a different question.

Manage Facebook Page tab apps via Open Graph API?

I have been investigating the Facebook Open Graph API. There is a "manage pages" extended permission which allows publishing to a user's Facebook Page news feed/wall via "impersonation". This is nice functionality, but is there anything more you can do through the API?
Specifically, I would like to be able to Add or Remove a "Tab" application from a user's Page via the API. Right now it's complicated to guide a user through the process of adding a tab app to their business (or Place) Facebook Page, and it would be AWESOME if they could just grant my app permission and I could add the app for them with the API.
Basically I am wondering if the API allows for true "page management", or just posting to the Page.
Anyone have any experience with this?
Is the best I can do pointing them to the "add.php" page with the "page" GET variable (e.g. http://www.facebook.com/add.php?api_key=xxxx&pages=1&page=xxxxxx)? This works OK for ADDING a tab application, but there is nothing at all to help them REMOVE an application from their Page so far as I know.
Thanks!
It is worth noting now, for anyone viewing this question (which is celebrating it's 1st birthday!), that Facebook significantly upgraded their API functionality back in July 2011. You can now Add, Remove, Rename, Reorder and set tabs as Default via the Open Graph API. The blog post is here:
https://developers.facebook.com/blog/post/524/
And the official documentation is here:
https://developers.facebook.com/docs/reference/api/page/#tabs
What Facebook does NOT have yet is a new API method for creating new tab apps, or for changing the tab icon. This is a big step though!
UPDATE: There is bug on the new bug tracker to follow about creating apps in the API if you want to follow it and see what they do: http://developers.facebook.com/bugs/295627350461318
There's a way to do it via the API. We finally got it to work, after many hours of sweat and tears :)
You can add a tab to a page without leaving your app. Here's a snippet of code that we used for that. We get a list of pages that a user manages, show them in a drop down and ask them to select what page they want to add our "my agent profile" tab to.
And the final product looks something like this - http://www.facebook.com/pages/Jennifer-Anderson-Real-Estate-Agent/185071901564573?sk=app_253956901293839
protected void btnAddTab_Click(object sender, EventArgs e)
{
if (ddlPage2.SelectedIndex >= 0)
{
FaceBookPages page = FaceBookPages.LookupByPageID(long.Parse(ddlPage2.SelectedValue));
if (page == null)
throw new NPlaySysException("FaceBookPages is null.");
AnalyticLog log = new AnalyticLog();
log.EventID = FBCommon.Events.AddAgentAppTabID;
log.UserID = UserID;
log.EventTime = DateTime.Now;
log.Update();
string result = FacebookSvc.AddTab(Web.AgentAppID, "me", page.AccessToken);
if (result.Equals("true"))
{
FaceBookPages.UpdateAgentProfileAdded(page.PageID, true);
List<FaceBookPages> notTabbedPages = FaceBookPages.LookupAgentProfileNotAddedByUserID(UserID);
imgStep3.ImageUrl = StepDoneImagePath;
divStep3.Attributes["class"] = StepDoneCssClass;
phStep3.Visible = false;
Step3Done = true;
btnCloseStep3.Visible = false;
if (notTabbedPages.Count > 0)
btnEditStep3.Visible = true;
else
btnEditStep3.Visible = false;
}
else
{
lblErrorAddTab.Text = "Failed to add your profile to page.";
Web.EmailError(string.Format("FacebookSvc.AddTab Failed. result={0}<br />UserID={1}<br />PageID={2}", result, UserID, page.PageID));
}
}
}
Yes, the best you can do is direct them to the add.php url. You can't do anything to help them remove the app.
The good news is that the user used to have to go through the add process AND physically decide to "enable" the tab by finding it in the tab dropdown on the page. Facebook has recently changed that and it seems that the tab is now immediately enabled after a user adds the app to their page.
As for "has_added_app" that works... sorta. It will tell you if the app is added to the page, but it won't tell you if the tab is enabled. For example, a user can disable the tab but still technically have the app installed on the page. Therefore "has_added_app" will return true even though the tab isn't actually visible.

Categories