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'm using file_get_contents_curl to get the facebook likes of the post and store them in my database.

 $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;
            }

Everything worked well until I added more posts (now 700+). The pulling of the likes needed more and more time. Now the script manages to get the right amount of Facebook likes for SOME posts, but for some other posts it just pulls an empty value. So some posts with lets say, 8 FB-Likes, still get the value of 0 FB likes written to the database.

My curl function:

function file_get_contents_curl($url) 
{
    if ( function_exists('curl_init') ) 
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
        curl_setopt($ch, CURLOPT_URL, $url);

        $data = curl_exec($ch);

        if ($data === FALSE) {
            $data =  "cURL Error: " . curl_error($ch);
        }

        curl_close($ch);
    } else {
        $data = $url;
    }
    return $data;
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The usage of curl in PHP is not considered good practice in WordPress. It comes with HTTP API to abstract network requests.

It will use curl when available, but still work via other backends if it's not and allow you to use WP debug plugins to troubleshoot requests, such as Core Control.

share|improve this answer
1  
wp_remote_get() would be more appropriate in fact –  JMau Sep 19 '13 at 13:40
    
how can I rewrite the above code piece to use wp_remote_get() instead of file_get_contents_curl? I just replaced the file_get_contents_curl('http://graph.facebook.com/?id='.get_permalink($post->ID‌​)) with wp_remote_get('http://graph.facebook.com/?id='.get_permalink($post->ID)) but it doesn't seem to work. I'm an absolute beginner in everything PHP, but I guess it doesn't work that easy. –  onz Sep 19 '13 at 20:31
1  
@onz That could be asked as a separate question. –  Pothi Sep 20 '13 at 6:27
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.