I'm using wp_remote_post:

    $test = wp_remote_post($environment_url, $args2 );
    $test2 = $test['http_response'];

So what I want to do is get the value of URL but I believe the object is protected so having difficulty getting to it.

The answer may be here


But I need help finding it.

Here is the output of $test2:

WP_HTTP_Requests_Response Object
[response:protected] => Requests_Response Object
        [body] => ...
        [raw] => HTTP/1.1 200 OK
                 Cache-Control: no-cache, no-store

        [headers] => Requests_Response_Headers Object
                [data:protected] => Array
                        [cache-control] => Array
                                [0] => no-cache, no-store


        [status_code] => 200
        [protocol_version] => 1.1
        [success] => 1
        [redirects] => 1
        [url] => https://www.example.com
        [history] => Array

I'm not sure what you're after here, but you can try to get the HTTP response object from the WP_HTTP_Requests_Response::get_response_object() method.

Here's an example to retrieve the url:

       ! is_wp_error( $test )
    && isset( $test['http_response'] ) 
    && $test['http_response'] instanceof \WP_HTTP_Requests_Response 
    && method_exists( $test['http_response'], 'get_response_object' )
    echo $test['http_response']->get_response_object()->url;
  • Hi that is EXACTLY what I needed. Now I realise that my PHP skills aren't as good as yours - so can you explain how I could have figured this out myself? The bit which mainly confuses me at this point is 'instanceof \WP...' - why does it have a \ and what does instanceof refer to? Thank you! – ravravrav Dec 20 '16 at 15:05
  • Glad to hear it worked for you. I just skimmed through the class to find something useful that you could use. That's when I spotted the get_response_object() method. If we use namespace, we need to reference the WordPress classes with \ in front as they live in the global scope. You can skip the \ if you don't use your own namespaces. Since we are going to use the get_response_object() method, I check if we have an object instance from that relevant class. Another idea would be to get the url from filters, e.g. http_request_args @ravravrav – birgire Dec 20 '16 at 15:18
  • Hi it took me 2 days to find this answer so really appreciate it from you. Based on the above I think it's OOP which is my weak point because I didn't fully understand the class / method concept. Do you think that's what I should focus on or anything else as well? – ravravrav Dec 20 '16 at 15:30
  • Yes I think it could help here to look into OOP, at least I'm still trying to learn it, even though there are some that say it's bad for "code health" ;-)re – birgire Dec 20 '16 at 15:40

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.