Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Change Facebook snippet from REST API to Graph API help WordPress

It seems good old facebook have retired the REST API which means my facebook likes count no longer works.

A developer friend sent me the code to make to make it work again using the graph API, but I have no idea how to implement it.

My current code is:

<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>

<?php


if(empty($fbLinkStats) || $fbLinkStats->link_stat->total_count == 0) {
echo 'Share it!';
} elseif($fbLinkStats->link_stat->total_count == 1) {
echo 'One share';
} else {
echo $fbLinkStats->link_stat->total_count.' shares';

}


?> </a>




The code he sent is:


$rfd = fopen("http://graph.facebook.com/?ids=$url", 'r');
$fb_c = fread($rfd, 8192);
$fb_c = json_decode($fb_c,true);
fclose($rfd);

$return = 0;
if (!empty($fb_c[$url]['shares'])) {
$return = $fb_c[$url]['shares'];
}
return $return;

where $url is seen on the first line, this needs to be the url of your
post, using the permalink or similar is fine.




Just need to know how to merge the two. Thanks in advance.

Answers (3)

2011-08-01

ej_emman answers:

Hello,

Are you using wordpress in your website? Can you give me the link?


kateM82 comments:

Here you go! This is as far as I got, but it's not working.

http://pastebin.com/2uuNDxMp

2011-08-01

Svilen Popov answers:

Put this in <strong>functions.php</strong>

function fb_shares($url) {
$fb_graph = 'http://graph.facebook.com/?ids='.$url.'';
$json = file_get_contents($fb_graph);
$json = json_decode($json, true);
if (!empty($json[$url]['shares'])) return $json[$url]['shares'];
else return 0;
}


Then change you can use this:

<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>
<?php
$fb_shares = fb_shares($shareUrl);
if($fb_shares == 0) {
echo 'Share it!';
}
elseif($fb_shares == 1) {
echo 'One share';
}
else {
echo $fb_shares.' shares';
}
?>
</a>


kateM82 comments:

Didn't seem to do anything?

Does the $url need to be updated to something that grabs the post permalink?
<blockquote>
"where $url is seen on the first line, this needs to be the url of your

post, using the permalink or similar is fine." </blockquote>


??


Svilen Popov comments:

Put this somewhere before your code:

<?php $url = get_the_permalink(); ?>


kateM82 comments:

adding that line just crashed it, help!


Svilen Popov comments:

Use <strong>get_the_permalink</strong> instead of <strong>get_permalink</strong>

<blockquote><?php
$shareUrl = get_the_permalink();
$shareTitle = urlencode($post->post_title);
?></blockquote>


Svilen Popov comments:

Post the entire file code in [[LINK href="http://pastebin.com/"]]pastebin.com[[/LINK]].


kateM82 comments:

Woops put this on the wrong one, here you go.

http://pastebin.com/2uuNDxMp


Svilen Popov comments:

[[LINK href="http://pastebin.com/as7LnRvv"]]http://pastebin.com/as7LnRvv[[/LINK]]

2011-08-01

Romel Apuya answers:

ofcourse its in wordpress..it should have not been posted in here if its not..
as this websites title says. WPQUESTIONS..

anyways.

<?php $url = get_the_permalink(); ?>

should be called before
<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>


kateM82 comments:

Can't get it working, this is what I have? ( I use thesis so it;s all in the same spot)


function fb_shares($url) {



$fb_graph = 'http://graph.facebook.com/?ids='.$url.'';

$json = file_get_contents($fb_graph);

$json = json_decode($json, true);

if (!empty($json[$url]['shares'])) return $json[$url]['shares'];

else return 0;

}



function prev_next_single_arrows () {
if (is_single()) {

?>


<div class="hpsharetop" style="width: 120px; height: 30px; float: left; display: block; padding-top: 3px; font-size:12px;">

<?php $url = get_the_permalink(); ?>

<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>

<?php

$fb_shares = fb_shares($shareUrl);

if($fb_shares == 0) {

echo 'Share it!';

}

elseif($fb_shares == 1) {

echo 'One share';

}

else {

echo $fb_shares.' shares';

}

?>

</a>

</div>


kateM82 comments:

Forgot this bit



<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);

?>



So actual current code is:




function fb_shares($url) {







$fb_graph = 'http://graph.facebook.com/?ids='.$url.'';



$json = file_get_contents($fb_graph);



$json = json_decode($json, true);



if (!empty($json[$url]['shares'])) return $json[$url]['shares'];



else return 0;



}







function prev_next_single_arrows () {

if (is_single()) {



?>





<div class="hpsharetop" style="width: 120px; height: 30px; float: left; display: block; padding-top: 3px; font-size:12px;">



<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);

?>



<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>



<?php



$fb_shares = fb_shares($shareUrl);



if($fb_shares == 0) {



echo 'Share it!';



}



elseif($fb_shares == 1) {



echo 'One share';



}



else {



echo $fb_shares.' shares';



}



?>



</a>



</div>


Romel Apuya comments:

i think you have missing tags.



function fb_shares($url) {
$fb_graph = 'http://graph.facebook.com/?ids='.$url.'';
$json = file_get_contents($fb_graph);
$json = json_decode($json, true);
if (!empty($json[$url]['shares'])) return $json[$url]['shares'];
else return 0;
}

function prev_next_single_arrows () {
if (is_single()) {?>
<div class="hpsharetop" style="width: 120px; height: 30px; float: left; display: block; padding-top: 3px; font-size:12px;">
<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
?>
<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>

<?php
$fb_shares = fb_shares($shareUrl);
if($fb_shares == 0) {
echo 'Share it!';
}elseif($fb_shares == 1) {
echo 'One share';
}else {
echo $fb_shares.' shares';
}
?>
</a>
</div>
}
}