How do I get URL from WP_HTTP object?

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

WP_HTTP_Requests_Response

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

1 Answer
1

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:

if( 
       ! 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;

Leave a Comment