I am trying to test cURL option in PHP. But I am unable to print the XML output on the browser. I get parse error. For some reason the beginning of XML is truncated and hence the syntax error. My client code is as follows:
<?php
header('Content-type: text/xml');
/**
* Define POST URL and also payload
*/
$xml_data = "<?xml version='1.0'?><member><name>XYZ</name><address>1111 yonge street Toronto Canada</address><businessunit>Hotel </businessunit></member>";
define('XML_PAYLOAD',$xml_data);
define('XML_POST_URL', 'http://localhost:8888/PlatformX/build_xml.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
/**
* Execute the request and also time the transaction
*/
$start = array_sum(explode(' ', microtime()));
$retValue = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
/**
* Check for errors
*/
if ( curl_errno($ch) ) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 404:
$result = 'ERROR -> 404 Not Found';
break;
default:
$result = 'Success';
break;
}
}
/**
* Close the handle
*/
curl_close($ch);
/**
* Output the results and time
*/
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;
print_r($retValue);
/**
* Exit the script
*/
exit(0);
?>
And my serer side code is:
<?php
header('Content-type: text/xml');
foreach( $_POST as $xmlstr ) {
echo $xmlstr;
}
?>
But I am unable to print the XML on the browser.
Request your help. I get the following in response
'1.0'?>XYZ
1111 yonge street Toronto Canada
Hotel
I am not getting the tag names and if I use header('Content-type: text/xml'); I get parse error.