store the PERMALINK in a PHP variable

admin

Administrator
Staff member
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
Code:
$myurl
variable using
Code:
get_permalink();
, 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:

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-&gt;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-&gt;count;
};


$pinterest_pins = function ( $url ) {
    $api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&amp;url=' . $url );
    $body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );
    $count = json_decode( $body );
    return $count-&gt;count;
};




echo twitter_tweet_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );

echo "&lt;br&gt;";

echo facebook_like_share_count( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );

echo "&lt;br&gt;";

echo pinterest_pins( 'http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-an-interview/' );

echo "&lt;br&gt;";


?&gt;

To add, the
Code:
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.