How-to Fix WP HTTP Error: name lookup timed out

This blog and the Snowferno site both run WordPress on separate but identically-equipped Lunarpages servers. When I went to apply today’s WordPress 2.8.4 upgrade, one install was giving me “WP HTTP Error: name lookup timed out” messages and no upgrade button, while the other worked just fine. There are some workarounds detailed on the WordPress support site, but I wasn’t happy having to disable cURL on one host and not the other. It had to be something else…

The problem for me was caused by lines 1276-1277 of the wp_Http_Curl request() method in wp-includes/http.php:

curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] );
curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );

I commented those lines out and threw in a line of debug code on line 1325 (after the curl_exec call)

print_r(curl_getinfo($handle));

and tested the cURL transport using Core Control. I found that my requests were simply taking longer than the timeout provided. I don’t know quite why, except to guess that one server must be just more swamped than the other.

I fixed it by building myself a little plugin that adds an action for the 'http_api_curl' action called on line 1315.

After enabling this fix, WP showed the 2.8.4 upgrade button on the Dashboard page, but the WordPress Development Blog and Plugins RSS feed boxes still showed timeout errors. So, I looked further and found a filter that catches and can modify $r['timeout'] in the wp_Http request() method on line 237.

Here is my barebones plugin code, which overrides all timeouts to a massive 15 seconds:

//adjustments to wp-includes/http.php timeout values to workaround slow server responses
add_filter('http_request_args', 'bal_http_request_args', 100, 1);
function bal_http_request_args($r) //called on line 237
{
	$r['timeout'] = 15;
	return $r;
}

add_action('http_api_curl', 'bal_http_api_curl', 100, 1);
function bal_http_api_curl($handle) //called on line 1315
{
	curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 15 );
	curl_setopt( $handle, CURLOPT_TIMEOUT, 15 );
}

I recommend you make sure this does not cause any other issues for your WP install. If you know how to make this code into a plugin, then you are capable enuf to be responsible for any adverse fallout. 🙂 I only know this solved my immediate problem while still maintaining cURL functionality.

6 comments on “How-to Fix WP HTTP Error: name lookup timed out

  1. Hello,

    I tried this solution and it doesn’t work. The plugin activates OK, but Core Control still shows me an error:
    An Error has occured: name lookup timed out

    I have 5 wordpress sites stalled with the wp-o-matics not updating the RSS because the cURL doesn’t work anymore.

    (The sites are hosted at 1Dollar-webhosting and the tech support does not even ANSWER email, except intermittently every week or so, and then they answer with a non sequitur.)

    Any help solving this hosting-related problem would be SUPER appreciated !!!

  2. Hmm… a few thoughts:

    Do you still get the timeout error with wp-includes/http.php lines 1276-1277 commented out?

    If removing those lines allows the connections to happen, then you prolly just need to figure out what your actual timeout number should be. It might just need to be higher than 15 seconds. That being said, if your server is taking longer than 15 seconds per cURL request, there’s prolly something nastier going on somewhere else and all this is doing is masking the symptoms.

    But, to find out your adequate timeout number, you could try enabling Core Control’s “HTTP Access Logger Module”. It’s in Tools > Core Control (Main Page). Once you enable the module, don’t forget to also go into the External HTTP Access Logger “tab” and click the “Enable HTTP Logging” button. Click thru a few pages in the WP dashboard that generate the timeout error, then check back and see what the logger has to report. It might tell you something new.

    A more hardcore way would be to catch your cron results in an error_log file. Instead of the line 1325 debug code you see above, add this instead to wp-includes/http.php:

    error_log(print_r(curl_getinfo($handle),TRUE));

    Naturally, wrapping curl_getinfo() in error_log() is nicer to your users than just dumping the output to screen. FYI, my error_log file ended up in the wp-admin folder.

    Look in the file for values of [total_time], [namelookup_time], and so on. If they are longer than 15, then change the plugin code to a higher number.

  3. Hi,

    I pretty much have the same problem and after following your advise I am now getting “An Unexpected HTTP Error occurred during the API request.” – will let you know if I find a work around

Comments are closed.