Posting an XML request using HTTP API

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.

1 Answer
1

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&…

Leave a Comment