Please Stop Using cURL in WordPress Plugins

WordPress HTTP class usage

Unfortunately, I keep finding WordPress plugins that try to call cURL functions directly. Unfortunately, not only do these plugins fail to work if cURL isn’t installed, it throws a fatal PHP error in the process.

The problem with using cURL in WordPress plugins is that WordPress solved that problem more than 2 years ago by implementing the WP_Http class. WP_Http is a class included in the WordPress core that has multiple options. One of those options is cURL, but it gracefully reverts to other PHP functions if cURL isn’t available.

Basically, anything you can do with cURL can be done with the WP_Http class, and it will allow your plugin to be much more versatile and compatible with more server setups.

Unfortunately, though, it seems that a lot of plugin developers are completely unaware of the class. Each time I find a plugin that requires cURL (one of my servers does not have cURL compiled into PHP), I point them to a great article on the WP_Http class that Ozh wrote back in August 2009, and each one tells me they’ve never heard of WP_Http before.

So, if you are even tangentially connected to a WordPress plugin (or theme) developer, please help shout this from the rooftops. If you are performing any sort of file request in your plugin, please use WP_Http instead of relying entirely on cURL.

3 Responses

  • JHihaat

    hi tnx for this post
    but how can I replace the Curl_init with wordpress class ?
    for example in this code:

    if( function_exists('curl_init'))
      { // if cURL is available, use it...
      $ch = curl_init($notifier_file_url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      $cache = curl_exec($ch);
      curl_close($ch);
    }
    
  • Hi,
    Example implementation in WordPress class would look like this:

    $url = $notifier_file_url;
    $request = new WP_Http;
    $result = $request->request( $url );
    

    After this code executes $result variable will have the same value as the $cache variable in your code example.
    Check out this great post to find more examples.

  • Tom Dworzanski

    As a WordPress user who sets up my own server, is it better for me to install curl? Does WordPress perform better with curl installed?