Solution for WordPress CURL IPv6 error “Network is unreachable”

I am using FeedWordPress plugin on some of my sites to pull data from Google News RSS feeds. It was working fine, but after I moved to a new host, I started to get errors like:

Failed to connect to 2a00:1450:8006::63: Network is unreachable

Note the IPv6. Google have been supporting it for a while and news.google.com resolves to IPv6 first (similar error happens in WordPress admin dashboard in “Incoming Links” section). Unfortunately network of my new host didn’t support IPv6, so I had to find solution to force WordPress to use IPv4. Enter class-http.php.

Located in /wp-includes folder of your WordPress install this module is responsible for different aspects of HTTP communication. By default class WP_Http_Curl is used. If you open function request you will notice several CURL options bein set, e.g.:

curl_setopt( $handle, CURLOPT_URL, $url);
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify );
curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );

If you’re running PHP 5.3 and above – there’s an obvious solution, just add one more option to force IPv4 resolve:

curl_setopt( $handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

Unfortunately my host is running PHP 5.2 and there is a known bug about missing cURL option do disable IPv6 in version prior to 5.3. Thanks guys from Google IPv6 team for finding this, when I contacted them about my IPv6 issues with Google News, they responded to emails instantly, went above and beyond call of duty and I am sure they didn’t simple Google the answer.

So, the less obvious solution is to disable CURL altogether, forcing WordPress to use other means of HTTP transfers. The class has function test to see whether CURL can be used:

function test($args = array()) {
   if ( function_exists('curl_init') && function_exists('curl_exec') )
      return apply_filters('use_curl_transport', true, $args);

   return false;
}

Comment the fist 2 lines to make function always return false:

function test($args = array()) {
   //if ( function_exists('curl_init') && function_exists('curl_exec') )
   //   return apply_filters('use_curl_transport', true, $args);

   return false;
}

This will tell WordPress that CURL is not available making it switch to other HTTP means that are not forced to use IPv6.

4 replies on “Solution for WordPress CURL IPv6 error “Network is unreachable””

  1. For those who might be wary of opening and editing php files themselves, or for those who just prefer cleaner solutions, there is an easier way to do this:

    Install the plugin Core Control (http://wordpress.org/extend/plugins/core-control/).

    You’ll find it under Tools > Core Control once it’s installed.

    Enable the HTTP Access Module.

    Go to the “External HTTP Access” page that appears in the plugin.

    On the line for cURL, click “disable transport”.

    The plugin will automatically use the highest enabled transport method on the list.

  2. @dreki Good point. Considering the fact that upgrading WP to a newer version (which is happening quite often these days) will overwrite the custom change.
    I just with you could disable cURL network-wide same way you can network-activate the plugin.

  3. Find yourself another hoster. If IPv6 is unavailable, but it still tries to use it, the system is misconfigured, and that means your hoster got no clue about what they’re doing.

Leave a Reply

Your email address will not be published. Required fields are marked *