user4491046
user4491046

Reputation:

store the PERMALINK in a PHP variable

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.

Upvotes: 19

Views: 3058

Answers (3)

Techie
Techie

Reputation: 45124

There are two methods to avoid this error.

1st method

If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

urlencode - This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

Check out the code below

$myurl = get_permalink();
$url = urlencode($url);

2nd method

file_get_contents() utilizes the fopen() wrappers, therefore it is restricted from accessing URLs through the allow_url_fopen option within php.ini.

Should I allow 'allow_url_fopen' in PHP?

3rd method

You can try curl. Try this link.

Upvotes: 0

Bobot
Bobot

Reputation: 1118

Read the last error code you gave in comments : 403 (forbiden access)

So I gone on the facebook API docs and found this : Link to docs

Its possible that one of your requested operations may throw an error. This could be because, for example, you don't have permission to perform the requested operation.

In the example that following this quote, we can see a 403 response code to the request.

Well, I have access to this page from my navigator and from PHP file_get_contents function. So maybe you are blacklisted, or there are some quotas ... If problem persists you should contact Facebook to know why your server does not have access to this information.

{
   "id": "http://alphaomega.bendaggers.com/2014/10/25/legendary-moments-of-barney-stinson/",
   "shares": 4
}

Hope its helped :)

Upvotes: 2

jerdiggity
jerdiggity

Reputation: 3665

This looks like a few simple oversights to me.

The first is that $url is encoding a variable that doesn't exist. Currently you have

$myurl = get_permalink();
$url = urlencode($url);

... when it should probably be

$myurl = get_permalink();
$url = urlencode($myurl);

The second is that there are no functions defined named twitter_tweet_count, facebook_like_share_count, or pinterest_pins. Instead you've assigned them to variables using anonymous functions to determine their value.

Essentially you should be able to change this

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

... to this

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

... with the only difference being the $ before the twitter_tweet_count text.

Once that's fixed you should further be able to combine both fixes, with the final product looking something like this (using the dynamic value of $url):

<?php

$myurl = get_permalink();
$url = urlencode($myurl);

$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( $url );

echo "<br>";

echo $facebook_like_share_count( $url );

echo "<br>";

echo $pinterest_pins( $url );

echo "<br>";

?>

Upvotes: 4

Related Questions