I have this code to count all the social sites shares in my posts. The problem with the code is the permalink is static.
If you noticed, i stored the permalink in $myurl
variable using get_permalink();
, the problem with the code is once I change the link "http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/", it gives me tons of errors such as:
file_get_contents(http://graph.facebook.com/?id=$myurl): failed to open stream: HTTP request failed!
Here's my code:
$myurl = get_permalink();
$url = urlencode($url);
$facebook_like_share_count = function ( $url ) {
$api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );
$count = json_decode( $api );
return $count->shares;
};
$twitter_tweet_count = function ( $url ) {
$api = file_get_contents( 'https://cdn.api.twitter.com/1/urls/count.json?url=' . $url );
$count = json_decode( $api );
return $count->count;
};
$pinterest_pins = function ( $url ) {
$api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $url );
$body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );
$count = json_decode( $body );
return $count->count;
};
echo twitter_tweet_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
echo "<br>";
echo facebook_like_share_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
echo "<br>";
echo pinterest_pins( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );
echo "<br>";
?>
To add, the get_permalink
function returns the current URL of the active page. I'm doing this to integrate it to my wordpress website and show the exact number of shares (facebook, twitter, linkedin and pinterest). The code above doesnt return the URL of the current page. It's static.