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
variable using
, the problem with the code is once I change the link "<a href="http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/" rel="nofollow">http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/</a>", it gives me tons of errors such as:
<blockquote>
file_get_contents(<a href="http://graph.facebook.com/?id=" rel="nofollow">http://graph.facebook.com/?id=</a>$myurl): failed to
open stream: HTTP request failed!
</blockquote>
Here's my code:
To add, the
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.
If you noticed, i stored the permalink in
Code:
$myurl
Code:
get_permalink();
<blockquote>
file_get_contents(<a href="http://graph.facebook.com/?id=" rel="nofollow">http://graph.facebook.com/?id=</a>$myurl): failed to
open stream: HTTP request failed!
</blockquote>
Here's my code:
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
Code:
get_permalink