wp_remote_get keeps timing out

I'm making a plugin that compares data from external API with meta items in WordPress backoffice.

I tried using wp_remote_get method to query my API but it doesn't return anything, nobody, nothing. When accessed directly with the same URL in browser the API generates JSON array without problems.

What am I doing wrong?

This is (partially omitted code in the plugin)

    ..........
    $chopped = explode("@", $meta['Email'][0]);

    $url = 'http://example.com/api/users/'.$chopped[0].'/'.$chopped[1];

    global $wp_version;
    $args = array(
        'timeout'     = 5,
        'redirection' = 5,
        'httpversion' = '1.0',
        'user-agent'  = 'WordPress/' . $wp_version . '; ' . home_url(),
        'blocking'    = true,
        'headers'     = array(),
        'cookies'     = array(),
    ); 
    $response = wp_remote_get( $url, $args );
    $body = wp_remote_retrieve_body( $response );
    $http_code = wp_remote_retrieve_response_code( $response );

    echo 'pre Test dump: '.print_r($http_code,1).'/pre';

edit 1: For those who might think it has to do with csrf protection or similiar, I can query the api from https://www.hurl.it/ without any problems too. Could the error be because I'm calling it inside a hook?

edit 2: The response code I'm getting

WP_Error Object
(
    [errors] = Array
        (
             [http_request_failed] = Array
                (
                    [0] = Connection timed out after 5003 milliseconds
                )

        )

    [error_data] = Array
        (
        )

)

Topic wp-remote-get plugin-development Wordpress

Category Web


just set a timeout argument for that.

wp_remote_get('Your Rest API URL', ['timeout' => 20]);

or you can also set it in the functions.php file:

function custom_timeout_extend( $time )
{
    return 20;
}

add_filter( 'http_request_timeout', 'custom_timeout_extend' );

Check with these args

$args = array(
    'timeout'     => 10,
    'sslverify' => false
); 

About

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