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);

    } else {
        $data = $url;
    return $data;

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.

| 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. – ndru Sep 19 '13 at 20:31
  • 1
    @onz That could be asked as a separate question. – Pothi Kalimuthu Sep 20 '13 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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