I've been reworking part of one of my applications from AJAX to cURL and part of optimizing this new application flow was adding the CURLOPT_IPRESOLVE with protocol CURL_IPRESOLVE_V4. I ran some tests with and without this protocol and I noticed that the results got back to me approx. 5 times faster when using this protocol, making it by far the best optimization I added.
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
However, I'm puzzled at how this works. Can someone explain why setting the CURLOPT_IPRESOLVE option speeds up requests so drastically? From what I could find it has something to do with cURL's DNS resolvement.
The CURLOPT_IPRESOLVE option switches between IPv4, IPv6, or both.
My guess is that your system is technically capable of talking IPv6, but in practice has no active connection. So in the default configuration, curl is trying first to connect over IPv6, reaching a time limit, and falling back to IPv4. By forcing it to connect only over IPv4, you skip that initial attempt, speeding up the request.
An alternative explanation is that your system can connect over IPv6, but does so via a different network route, which for some reason is slower than the default IPv4 network.