get facebook share counts wordpress - facebook

i still have a problem on http://www.whaelse.com/en/grau-gruen-schwarz/ with the share counter.
Twitter and Google+ works fine for me.
With this code i've tried to load the facebook shares.
function get_likes($url) {
$json_string = file_get_contents('https://graph.facebook.com/?id='.$url);
$json = json_decode($json_string, true);
return intval( $json[$url]['shares'] );
}
and then
function getSocialCount($url){
$urlCurrentPage = get_permalink($post->ID);
$strPageTitle = get_the_title($post->ID);
echo '<li>facebook<span class="facebooksticky">'.get_likes($url).'</span></li>';
}
echo getSocialCount( get_permalink($post->ID));
but i still get 0 shares counter.

Could be a caching problem, just try it again tomorrow. Or try this endpoint, used by sharedcount.com:
https://api.facebook.com/method/links.getStats?urls=%%URL%%&format=json

Related

facebook manage pages details

I am trying to get access of facebook pages through php SDK
Using php-sdk-4.0.13
Facebook app version v2.2
And redirecting url like this
https://www.facebook.com/v2.2/dialog/oauth?redirect_uri=http//www.example.com/john114614/settings&state=5255ad86fea19ab0e8d61776890d0224&scope=manage_pages&client_id=558252720931906&ret=login&sdk=php-sdk-4.0.13&ext=1431067051&hash=AeYVfkUZEGLy0wD_
Its stay at 302 status and redirecting to http//www.example.com/john114614/settings?code=blahbla.......
App id is perfect and more over this problem faced from last 5-6 days before that its working fine.
Please help me how i can resolve this issue?
Thanks
You didn't specify a response_type, see
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.3#login
In your case, you get a code which your'd need to exchange to an Access Token afterwards. If you specify your login url as follows
https://www.facebook.com/v2.2/dialog/oauth?redirect_uri=http://www.example.com/john114614/settings&scope=manage_pages&client_id=558252720931906&response_type=token
it should work IMHO.
Solution for above problem
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('appid84', '0secret0a626c6');
$helper = new FacebookRedirectLoginHelper('http://tmd.local/fblogin.php');
try {
$session = $helper->getSessionFromRedirect();
}
catch(FacebookRequestException $ex) {
}
catch(\Exception $ex) {
}
if ($session) {
var_dump($session);
}
else
{
$loginUrl = $helper->getLoginUrl();
header("location:".$loginUrl);
exit;
}

get likers of Facebook page and get count of those who like more than 200 pages

I'm trying to get a "list" of random likers who follow a Facebook Page. I'm using this code to get some fans (not random fans, but this is something else).
<?php
function fetch_fb_fans($fanpage_name, $no_of_retries = 10, $pause = 500000){
$ret = array();
/* get page info from graph */
$fanpage_data = json_decode(file_get_contents('http://graph.facebook.com/' . $fanpage_name), true);
if(empty($fanpage_data['id'])){
/* invalid fanpage name */
return $ret;
}
$matches = array();
$url = 'http://www.facebook.com/plugins/fan.php?connections=100&id=' . $fanpage_data['id'];
$context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0')));
for($a = 0; $a < $no_of_retries; $a++){
$like_html = file_get_contents($url, false, $context);
preg_match_all('{href="https?://www\.facebook\.com/([a-zA-Z0-9._-]+)" data-jsid="anchor" target="_blank"}', $like_html, $matches);
if(empty($matches[1])){
/* failed to fetch any fans - convert returning array, cause it might be not empty */
return array_keys($ret);
}else{
// merge profiles as array keys so they will stay unique
$ret = array_merge($ret, array_flip($matches[1]));
}
// don't get banned as flooder
usleep($pause);
}
return array_keys($ret);
}
/*
print_r(fetch_fb_fans('cocacola', 2, 400000));
prints 73 unique fan names as array
*/
$contador = 0;
foreach (fetch_fb_fans('cocacola', 2, 400000) as $fan) {
$pageContent = file_get_contents('http://graph.facebook.com/'.$fan.'');
$parsedJson = json_decode($pageContent);
echo $parsedJson->username ."<br/>";
}
?>
Code from: Facebook API: Get fans of / people who like a page
This code give me some usernames. Now, my question, after searching Google... Is, can I get the number of pages that follow every user?
I know that Graph API let me know my likes but when I try to see other user likes it throws me an OAuthException error. I supose that I'm not doing right.
So I will apreciate some explanation about how to do this. I searched Google but I don't understand how it works.
Thanks.
The Facebook documentation is unfortunately not very clear: https://developers.facebook.com/docs/graph-api/reference/v2.2/user
However, getting the likes from a user requires:
User access token for the user
"User likes permission" granted on the access token, which is a special permission that Facebook approves on your app
Without an access token for the user you cannot see what pages they like.
While not supported, you could perhaps use a page scraper to find this information if they have it public.
Based on your question, it's not clear whether users log in to your app or if you're just trying to get information from one of your own pages, or another page. If you don't have users logging into your app, I'm afraid there's no way at all to get this information apart from a page scraper.

Facebook developer roadmap: Deprecating 'comments' field & Removing 'count' from 'comments'

I have noticed the [new changes in FB Developer]: https://developers.facebook.com/roadmap/
I'd like to know what you think I need to change in my code.
I have wordpress and I have a function that counts the total number of comments, and of course it still need to works also after July 10.
function full_comment_count() {
global $post;
$url = get_permalink($post->ID);
$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
$wpCount = get_comments_number();
$realCount = $count + $wpCount;
if ($realCount == 0 || !isset($realCount)) {
$realCount = 0;
}
return $realCount;
}
Is it as simple as changing:
$count
to
$total_count
or something else needs to be changed as well in the code?
Thank you
Facebook Roadmap:
We are removing the undocumented 'count' field on the 'comments'
connection in the Graph API. Please request
'{id}/comments?summary=true' explicitly if you would like the summary
field which contains the count (now called 'total_count')
...file_get_contents is VERY bad, CURL would be better, but more complicated. the best way to use the graph api in this case is the php sdk: https://github.com/facebook/facebook-php-sdk
anyway, i guess those changes are needed:
$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
...this is still correct, with a var_dump right after this line (or after the json decode) you see that there is an "id". with that id, you have to make a second call to the graph api:
$comments= file_get_contents('https://graph.facebook.com/' . $id . '/comments?summary=true);
the rest is easy-peasy basic php stuff, just do a var_dump of $comments after using json_decode again.

Getting user info after login for facebook application

I have written a small piece to get the user to login :
<?php
include_once ('facebook.php');
$api_key = 'xxxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxxx';
global $facebook;
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();
#Ask user to login if not logged in
$is_tab = isset($_POST['fb_sig_in_profile_tab']);
if( !$is_tab ){
$uid = $facebook->require_login($required_permissions = 'email,status_update,offline_access');
}
else{
$uid = $facebook->get_profile_user();
}
I need to retrieve the users info once I have the uid. Unfortunately I cannot find the api calls for it and there is a no IDE for facebook :(. Can anyone point me to the api's or give me here. I want to get users username, city, zipcode.
I coudnt get this to work : facebook.Schema.user user = api.users.getInfo();
I think it is for the new API lib.
Thanks.
P.S: I am not using graph APIs, because I found them to be poorly documented and I got stuck with them for a while so decided to abandon them atleast for now.
You can get it like this:
$user_info = $facebook->api_client->users_getInfo($uid, "name, city, zipcode");
print_r($user_info);
More Info:
http://developers.facebook.com/docs/reference/rest/users.getInfo

Facebook infinite redirect loop during authentication

I am getting an infinite loop in the URL redirect after a user either logs in or is already logged in. The page redirects to the login page if the user is not logged in as expected , but goes in the loop as soon as he enters the credentials.
Following is the code:
<?php
include_once ('facebook.php');
$api_key = 'xxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxx';
global $facebook;
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();
$uid = $facebook->require_login($required_permissions = 'email,status_update,offline_access');
$facebook->api_client->users_hasAppPermission("offline_access",$uid);
#echo $uid;
# $facebook->api_client->users_setStatus("hello",$uid);
# echo "<p>Your status has been updated</p>";
?>
Interestingly this code was working before, but suddenly started giving me an infinite loop problem. There have been couple of discussion on facebook forum about this but no indication that is a bug or what is the workaround .
Any help would be highly appreciateed.
Thanks
I added the code to direct to login only if the user is not logged in else do not do that. This worked for me! .. Hope it helps.
$is_tab = isset($_POST['fb_sig_in_profile_tab']);
if( !$is_tab ){
$uid = $facebook->require_login($required_permissions = 'email,status_update ,offline_access');
}
else{
$uid = $facebook->get_profile_user();
}