PHP and CURL POST XML

Refresh

April 2019

Views

1.2k time

1

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 answers

0

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);

?>