Fix cURL request being really slow locally with Valet

I’m using Valet for running all my local WordPress development installations, which has worked perfectly until updating Valet to PHP 7.4 after the summer vacations.

Suddenly all my outgoing cURL requests (through wp_remote_post) was running really slow, always above 5 seconds which was really frustrating to work with.

I found out that cURL has problems locally to resolve DNS lookups on IPv6. cURL is easily configured to ignore the IPv6 resolver by setting the resolve option:

curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

However – this is not as easy if wp_remote_post is doing the request. There are no arguments to set for cURL. I scratched my head for awhile when searching in WordPress’ source code, but then I found an action to fix the problem:

add_action( 'http_api_curl', 'force_ipv4_resolver' );
function force_ipv4_resolver( &$ch ) {
	curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
}

The cURL handler is passed by reference, so remember to use the & to reference it correctly.