0

I am trying to create a WP plugin that calls an external API using HTTP POST. I am using wp_remote_post and my arguments look like this:

$body = array(
    'CustomerID' => '10',
    'Items' => ['5', '7'],
);

What I want to end up with is this as my POST data:

CustomerID=10&Items=5&Items=7

But this does not seems to work as expected... Can anyone point me to the right direction?

Also, how can I log the final request parameters, in the same form the external API will get them?

7
  • Method is set to GET? Like wp_remote_post('<API_URI>', array('method' => 'GET', 'timeout' => 15, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array('CustomerID' => urlencode(10), 'Items' => array(5, 7)), 'cookies' => array())); Jul 1, 2019 at 20:53
  • No, it's set on POST. Sorry I as not very clear with that. I have updated my question. Jul 2, 2019 at 6:17
  • What you can do is create the url in that way by using concatenation. Something like '<API_URI>?CustomerID='.$body['CustomerID'].'&Items='.$body['Items'][0].'&Items='..$body['Items'][1] Otherwise you can change the method to get as shown in the example in my previous comment. Jul 2, 2019 at 6:36
  • I was hoping to use POST, but GET is also an option. However a simple GET will not get me the final URL I want. I will probably use a for loop to build the final URL... Jul 2, 2019 at 7:05
  • Not sure why you need that particular url structure but yes, iterating through the variables will provide you that structure. Jul 2, 2019 at 7:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.