1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need Help With Pinterest Api Function

Discussion in 'PHP' started by chotoan, Feb 4, 2013.

  1. #1
    function getPinterestCount() {
        $url = get_permalink();
        $json_string = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='.$url);
        $json = json_decode($json_string, true);
        return intval( $json['count'] );
    }
    Code (markup):
    Hi guys, please help me check and fix the following code, I can’t understand why it couldn’t work.

    Thanks!
     
    chotoan, Feb 4, 2013 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    "Couldn't work" doesn't tell us anything. What's it doing or not doing and what are you expecting it to do?
     
    Rukbat, Feb 4, 2013 IP
  3. chotoan

    chotoan Active Member

    Messages:
    423
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I'm using this code for wordpress. And when display it as <?php echo getPinterestCount(); ?> it always show " 0 ". I'm using the same code for Twitter, Linkedin. They're showing well but doesn't work for Pinterest.
     
    chotoan, Feb 4, 2013 IP
  4. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #4
    This is an example of when you need to check the output of each line of your function and see why the following line isn't working properly. It's a simple enough process to show that the data returned by Pinterest isn't a valid json encoded string Below works:

    <?php
     
    var_dump(getPinterestCount('http://google.com'));
     
    function getPinterestCount($url = '') {
        $url = empty($url) ? get_permalink() : $url;
        $json_string = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='.$url);
        $raw_json = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $json_string);
        $json = json_decode($raw_json, true);
        
        return intval( $json['count'] );
    }
    PHP:
    Feel free to check the output for yourself.
     
    Alex Roxon, Feb 4, 2013 IP
  5. chotoan

    chotoan Active Member

    Messages:
    423
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    But my code worked well for Linkedin and Twitter ( I'm using Wordpress ) and doesn't work with Pinterest :|
     
    chotoan, Feb 5, 2013 IP
  6. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #6
    As I said, the returned output of the Pinterest API by itself isn't a valid json encoded string. You have to do a bit of parsing first.
     
    Alex Roxon, Feb 5, 2013 IP