Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a script that pulls the facebook likes via facebook graph and writes them in my DB. However it uses file_get_contents_curl for the request and this is causing problems, so I'd like to use the wp_remote_get() command. I've tried to change it but somehow I can't do it (I don't really have PHP knowledge).

Here's the part of the script:

foreach($posts as $post)
    {

        $fb = json_decode(file_get_contents_curl('http://graph.facebook.com/?id='.get_permalink($post->ID)));
        if( !isset( $fb->likes) && isset($fb->shares) )
        {
            $fb->likes = $fb->shares;
        }
        //$fb->likes = isset($fb->likes) ? $fb->likes : 0;
        $myfblikes = sprintf("%04s", (int)$fb->likes);
        update_post_meta($post->ID, 'fb_likes', $myfblikes);
    }
share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

You can try this:

foreach( $posts as $post )
{
    $url      = sprintf( 'http://graph.facebook.com/?id=%s', get_permalink( $post->ID ) );
    $response = wp_remote_get( $url,  array( 'timeout' => 15 ) );


    if( ! is_wp_error( $response ) 
        && isset( $response['response']['code'] )        
        && 200 === $response['response']['code'] )
    {
        $body = wp_remote_retrieve_body( $response );
        $fb   = json_decode( $body );

        if( ! isset( $fb->likes ) && isset( $fb->shares ) )
        {
            $fb->likes = $fb->shares;
        }

        if( isset( $fb->likes ) )
        {
            $myfblikes = sprintf( '%04s', (int) $fb->likes );
            update_post_meta( $post->ID, 'fb_likes', $myfblikes );
       }
    }
}

where we can use wp_remote_retrieve_body() to get the response body of wp_remote_get().

share|improve this answer
1  
I just tested this and I get a share/like result of the form 0026 for some given permalink. –  birgire Sep 20 '13 at 10:47
    
There was a wrong comment here. I've misread your code. +1 (I can't immagine why somone -1 this one). –  G. M. Sep 20 '13 at 10:49
    
if you check the previous edtis of the answer, I did put this inside ;-) So I'm still editing, I did put an extra isset in the last edit ;-) Thanks for pointing this out. –  birgire Sep 20 '13 at 10:50
    
ok thanks @G.M. for the +1 - yes I'm not sure what I did to deserve -1 on this one ;-) –  birgire Sep 20 '13 at 10:56
1  
Seems OP thinks with us ;) –  G. M. Sep 20 '13 at 11:23
add comment

wp_remote_get returns an array, not a string. So,

foreach($posts as $post) {

    // get remote document as JSON
    $json = wp_remote_get( 'http://graph.facebook.com/?id=' . get_permalink($post->ID) );

    // check for WP_Error
    if(is_wp_error($json))
        return false;

    // Decode JSON and get document body as associative array
    $fbData = json_decode($json['body'], true);

    // return (numeric) array value
    return intval($fbData['likes']);
}

I'm not sure about get_permalink($post->ID) part. That's OK only if you have a FB page for each post.

share|improve this answer
add comment

Just change one function with the other. This require also that you change the way you are using json_decode() because wp_remote_get() returns an array and json_decode() requires a string.

foreach($posts as $post) {

    $fb = wp_remote_get('http://graph.facebook.com/?id='.get_permalink($post->ID));
    if($fb && !is_wp_error($fb){
        $body = json_decode(wp_remote_retrieve_body($fb));
        if( !isset( $body->likes) && isset($body->shares) ) {
            $body->likes = $body->shares;
        }
        //$body->likes = isset($body->likes) ? $body->likes : 0;
        $myfblikes = sprintf("%04s", (int)$body->likes);
        update_post_meta($post->ID, 'fb_likes', $myfblikes);
    }
}
share|improve this answer
    
that was the frist thing I tried. It gives me this error Warning: json_decode() expects parameter 1 to be string, array given in /usr/www/users/toaste/zitate/wp-content/plugins/order_posts_by_social/order_post‌​s_by_social.php on line 151 - line 151 is the line I replaced the command btw –  onz Sep 20 '13 at 10:09
1  
That is because you are passing an array to json_decode(), not a string. This error is not related with the wp_remote_get() itself, it is related with its returned value and the way you are using json_decode(). See edited answer. –  cybmeta Sep 20 '13 at 10:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.