Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 Jul 19 '11 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 Jul 19 '11 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 Jul 19 '11 at 15:48

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
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? –  helgatheviking Oct 23 '12 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 Nov 3 '12 at 20:27
    
thanks @tollmanz. i figured out my own problem, and the xml passed tends to be specific to the third party. –  helgatheviking Nov 4 '12 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 Jan 19 '13 at 10:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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