1

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

)
  • Did you try to increase the timeout? – Ahmed Fouad Sep 22 '16 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 '16 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. – Ahmed Fouad Sep 22 '16 at 23:50
  • Be my guest medi.eqr-system.com/api/test I'm going to ip whitelist it later anyway :) – Angerion Sep 23 '16 at 1:13
  • Loaded in browser in less than sec. lets try the WP remote get – Ahmed Fouad Sep 23 '16 at 1:15
0

Check with these args

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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