最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp remote get - How to get the generated query string of wp_remote_get?

programmeradmin3浏览0评论

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', array( 'body' => array( 'foo' => 'bar' ) ) );

I would expect the query string to look something like:

thirdparty?foo=bar

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

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', array( 'body' => array( 'foo' => 'bar' ) ) );

I would expect the query string to look something like:

thirdparty?foo=bar

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

Share Improve this question asked Mar 7, 2017 at 14:54 GazillionGazillion 3171 gold badge5 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

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?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?foo=bar, you simply do

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

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

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

Happy Coding!

You can have a look into the classes inside wp-includes/class-http that handle the actual request. There are various hooks like http_api_curl you could hook into to see what is actually requested.

This is a task for Xdebug. I'd recommend to use it together with PHPStorm.

Then you'll need to set a breakpoint exactly at the moment you build the query or when you post the request. Then you can inspect what exactly is going on.

Following is a sample chain I used to post something successfully, which results in a query that simply looks like the following which then get appended to the $url below. I found it important to format the query with PHP_QUERY_RFC3986 using PHP's http_build_query() so it gets encoded properly.

The query:

foo=foovalue&bar=barvalue

The complete sample:

// Build the URL query to be append to the POST request URL.
$data = [
  'foo' => $foo,
  'bar' => $bar,
];

$query = http_build_query($data, NULL, '&', PHP_QUERY_RFC3986);

// The POST request URL.
$url = 'https://example/index.php/fooBar/save2?' . $query;

$args = [
  'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  'method'  => 'POST',
];

// Post the request.
$response = wp_remote_request($url, $args);

//Check for success
if (!is_wp_error($response) && ($response['response']['code'] === 200 || $response['response']['code'] === 201)) {
  return $response['body'];
}
发布评论

评论列表(0)

  1. 暂无评论