Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Ravi Kaul
Ravi Kaul
1,372 Points

How can I get the url from a wp_remote_post request?

So as the title says I'm using wp_remote_post in Wordpress on a plugin. This posts data to a url, which in turns redirects it to another page. This is the page which is being returned in the [body] array.

However, I don't want the page itself, just the [url] for it.

The [url] is displayed in the code within:

[headers] => Requests_Response_Headers Object
                        (
[url] => https://example.com...

But how do I get to it?

3 Answers

Ravi Kaul
Ravi Kaul
1,372 Points

Hi Jacob

I'm using wp_remote_post so I can't use wp_remote_get.

The actual page content is there - I can see it in $response['body']

But I don't want to get the page content, I actually just want to get the url of the returned response.

What values are you using in the $args array?

Ravi Kaul
Ravi Kaul
1,372 Points

$args = array( 'body' => $body, 'timeout' => '5', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'cookies' => array() );

where $body is like this

    $body = array(
        'var1[api_key]' => '123abc',
        'var2[id]' => '999',
                    etc...
    );

Not 100% sure on your set up, but it sounds like you might want to look at wp_remote_get() This can "get" a URL if that is what you want. I hope this helps, if not please post the method you are using.