wp_remote_get - cURL error 28 - only on same domain

On my website I use wp_remote_get to fetch data from mydomain.com/wp-json/wp/v2/pages/75

When I run it on my localhost it works fine, but after sending to production server (mydomain.com) suddenly I’m getting error: ‘cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received’ + VPS starts acting crazy (100% cpu usage and memory).

It can’t retrieve data from the same domain. When I changed url to some random REST mockup it is working again.

How can I get data from WP REST on the same domain??

(VPS info: debian 9, php 7.3, curl 7.52.1 and latest wordpress 5.2.2)

Thanks in advance!

Topic wp-remote-get curl Wordpress

Category Web


By default, WordPress set the timeout value for cURL in wp-includes/class-wp-http-curl.php to 5 seconds and the same value is also set to HTTP requests in wp-includes/class-http.php that is a newer class for making HTTP requests that can use also cURL if it’s present in the server.

In your case, 5 seconds was not enough to perform the WP REST call, but it was enough to perform the dummy call. Generally, in terms of doing an HTTP request 5 seconds should be enough in most cases. But it seems in your WP REST endpoint you are doing too much work that need more than 5 seconds.

You can increase the timeout value to any value (15 seconds for example) by the following filter.

function custom_http_request_timeout( ) {
    return 15;
}
add_filter( 'http_request_timeout', 'custom_http_request_timeout' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.