4

I have been tearing my hair out for 2 days trying to figure this out and I wonder if any of you could help? I am trying to post an XML string to a server and they state "All XML requests should be sent to our server with the "xml" parameter name using the HTTP POST method". I have tried numerous ways of doing this but no joy (if I use javascript with a form it works fine but that isn't practical with the project). Please could someone point me in the right direction? I will post below firstly the PHP/CURL code that I can't get to work and then the Javascript code that works fine. I guess I just want to emulate the Javascript in the PHP/CURL code

PHP/CURL Code

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Request>
   <Head>
      <Username>username</Username>
      <Password>password</Password>
      <RequestType>GetCities</RequestType>
   </Head>
   <Body>
      <CountryCode>MX</CountryCode>
  </Body>
</Request>';

$url = "http://example.com";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;

curl_close($ch);

Javacript Code (which works)

function submitForm() {
var reqXML = "<Request><Head><Username>username</Username> <Password>password</Password><RequestType>GetCities</RequestType></Head><Body><CountryCode>MX</CountryCode></Body></Request>";
document.getElementById("xml-request").value = reqXML;
var xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("xml-response").value = xmlhttp.responseText;
    }
}
xmlhttp.open("POST", "http://example.com", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("xml=" + reqXML);
return false;

1 Answer 1

3

As you want to send your XML content through the "xml" param, you may have to do this instead:

curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . $xml);

Edit: here's my working code:

<?php

$xml = '<?xml version="1.0" encoding="UTF-8"?><Request><Head><Username>username</Username> <Password>password</Password><RequestType>GetCities</RequestType></Head><Body><CountryCode>MX</CountryCode></Body></Request>';

$url = "https://postman-echo.com/post"; // URL to make some test
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
echo '<pre>';
echo htmlentities($data);
echo '</pre>';

if(curl_errno($ch))
    print curl_error($ch);
else
    curl_close($ch);

?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I just tried that and instead of the usual error it came back with "non-existent IP address"
Sorry, this may be related to the host you try to send your request to or the network.
Hi. In what way do you mean? The javascript method works fine so they obviously expect a POST request. The non existent IP address issue has now gone! Back to "Use POST method to send the 'xml' parameter" as the error
I edited my answer, you may try my code on your side, you'll notice that the POST param 'xml' contains your xml data.
Thank you. I will give that a go first thing in the morning

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.