2

I'm trying to work with an API that expects an XML string in the body of the post. Additionally, the docs for the API specify:

Please post with mime-type text/xml with the XML in the body of the post

I want to use the WordPress HTTP API to make this work, but I'm running into some problems. So far I have code like the following that seems to be failing.

$url = 'https://www.testurl.com';
$xml = <<<TESTED XML HERE>>>;
$response = wp_remote_post( 
    $url, 
    array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'headers' => array(
            'mime-type' => 'text/xml'
        ),
        'body' => array('xml' => $xml),
        'sslverify' => false
    )
);

My question is, am I properly setting the mime-type and am I sending the XML in the right place?

It's surprisingly tough to find examples of the WordPress HTTP API utilizing XML requests.

3
  • Try using a "Content-Type" header instead of "mime-type". Also, your body should probably just be the xml itself, not an array with an XML name pointing to it.
    – Otto
    Commented Jul 19, 2011 at 7:24
  • does your testurl.com expect to get the xml=<xml> if not then try changing 'body' => array('xml' => $xml), to 'body' => array($xml),
    – Bainternet
    Commented Jul 19, 2011 at 7:55
  • Thanks guys for your comments! I finally got it working. The API to which I was submitting is not well documented. Turns out, I had to submit the xml string with the key "postdata" and I had to include another key value pair of "postfield" => "value". Interesting stuff.
    – tollmanz
    Commented Jul 19, 2011 at 15:48

1 Answer 1

4

I figured it out. The WordPress HTTP API was doing it's job; my problem was with the API I was working with. I just modified my code like:

$url = 'https://www.testurl.com';
$xml = <<<TESTED XML HERE>>>;
$response = wp_remote_post( 
    $url, 
    array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'headers' => array(
            'Content-Type' => 'text/xml'
        ),
        'body' => array('postdata' => $xml, 'postfield' => 'value'),
        'sslverify' => false
    )
);

Again...this was just a misunderstand of the API I was working with, not the HTTP API provided my WordPress.

Edit: POST data should be supplied as an array, according to http://codex.wordpress.org/Function_API/wp_remote_post The array will be transformed into a string like this: key1=val1&key2=val2&...

5
  • sorry to drag up an old question, but where did you dig up the 'posdata' and 'postfield' parameters? Were those specific to your third party API? Commented Oct 23, 2012 at 19:50
  • @helgatheviking I have no memory of what this was even for; however, I believe that this was a requirement of the 3rd party API and nothing to do with WordPress.
    – tollmanz
    Commented Nov 3, 2012 at 20:27
  • thanks @tollmanz. i figured out my own problem, and the xml passed tends to be specific to the third party. Commented Nov 4, 2012 at 0:41
  • @helgatheviking Those are custom settings depending on the api. Core then builds the string with using http_build_query() (a native PHP function).
    – kaiser
    Commented Jan 19, 2013 at 10:36
  • Thank you @tollmanz! This really helped! I only needed to change how the body is set. I just needed to provide the xml string I had setup.
    – cbloss793
    Commented Aug 29, 2017 at 15:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.