How to get the generated query string of wp_remote_get?

I have some very basic data that I am posting to a proprietary lead capturing system. Whenever I submit my form data to their system the body of the request is an error 500 page.

I am trying to debug the problem with their developer, it clearly doesn’t like something in my query string, and he would like to be able to test it on his end.

However, I’ve scoured through Google results and the WordPress codex and I cannot find a way to pull the query string that is generated from something like:

$result = wp_remote_get( ‘thirdparty.com’, array( ‘body’ => array(
‘foo’ => ‘bar’ ) ) );

I would expect the query string to look something like:

thirdparty.com?foo=bar

Anyone have any tip on how to get this generated URL/query string?

3 Answers
3

While the other answers are pretty good in providing an answer to your question (how to get the generated url), i will answer what i guess you really want to know (how do i get it to call thirdparty.com?foo=bar with wp_remote_get)

The Answer is: the $args array you use to transmit the url parameters won’t work like this. If you have to transmit a POST request with wp_remote_post, you would use the body argument like your example. However, to wp_remote_get thirdparty.com?foo=bar, you simply do

wp_remote_get('http://thirdparty.com?foo=bar');

if you want to get fancy and use an array, you can also do this:

$url = "http://thirdparty.com";
$data = array('foo' => 'bar');
$query_url = $url.'?'.http_build_query($data);
$response = wp_remote_get($query_url);

Happy Coding!

Leave a Comment