4

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
        (
        )

)
17
  • 1
    Did you try to increase the timeout? Sep 22, 2016 at 23:33
  • Yes I did (lowered it back to 5s because I was running out of patience), but the response body has only few lines of code so it should be loaded pretty much instantly anyway.
    – Angerion
    Sep 22, 2016 at 23:40
  • If it's a public API, I would be happy to test it for you from here to see if it gives the same timeout issue. Sep 22, 2016 at 23:50
  • Be my guest medi.eqr-system.com/api/test I'm going to ip whitelist it later anyway :)
    – Angerion
    Sep 23, 2016 at 1:13
  • Loaded in browser in less than sec. lets try the WP remote get Sep 23, 2016 at 1:15

2 Answers 2

9

Check with these args

$args = array(
    'timeout'     => 10,
    'sslverify' => false
); 
2
  • The default timeout is 5 seconds. Nov 4, 2019 at 15:22
  • sslverify is what fixed it for me - i suddenly got a timeout even though it was set to 15 seconds, and the api was on the same server. also, the ssl certificate is valid.
    – andyrandy
    Mar 7 at 11:34
2

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.