0

I have a working facebook sharecounter. But I am having difficulty getting it to work the same way for pinterest.

Here is my function:

    //sharecount fo pinterest
function get_pins() {
    $pin_results = file_get_contents('http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='. get_permalink());
    $pin_array = json_decode($pin_results, true);
    $pin_count =  $pin_array['count'];
    return ($pin_count ) ? $pin_count : "0";
}

Here is the code on wordpress theme file for the share button:

<span class="social_count"><?php echo get_pins(); ?></span>

Always returning 0.

if I test out the api with url in browser this is the json return:

receiveCount({"url":"http://example.com/2015/05/get-moving-for-mindstrong-lets-stop-the-stigma/","count":1})

here are the results of echoing $pin_results:

receiveCount({"url":"http://example.com/2015/05/get-moving-for-mindstrong-lets-stop-the-stigma/","count":2})

Sackling
  • 1,780
  • 5
  • 37
  • 71
  • Pinterest may block web hosts' IP ranges from accessing that API, as it's not supposed to be used server-side like this. See if `$pin_results` contains an error message of some sort. – ceejayoz May 27 '15 at 17:06
  • here are the results of echoing $pin_results: receiveCount({"url":"http://example.com/2015/05/get-moving-for-mindstrong-lets-stop-the-stigma/","count":2}) – Sackling May 27 '15 at 17:50
  • Is it because of the "receiveCount( ) that encompasses the Json? – Sackling May 27 '15 at 17:55
  • Yep, that's not JSON. That's JSONP. Please see the dupe question for code to parse it. – ceejayoz May 27 '15 at 18:27
  • awesome thanks. Works with that function – Sackling May 27 '15 at 18:48

1 Answers1

1

You send get_permalink() to the API and not $url. Outside of the loop, without any parameter, get_permalink act like this:

Note that when used outside The Loop on a posts page (index, archive, etc.) without the ID parameter, it will return the URL of the last post in The Loop, not the permalink for the current page

Which is why it returns 0 - you probably send a random url to the API.

vard
  • 4,057
  • 2
  • 26
  • 46
  • Yep you are right. However I update the code to reflect the same way the facebook code is working properly and I am still getting 0. I edited my question to show the way I update it. – Sackling May 27 '15 at 17:00