PHP-XML
How to display XML output in Browser?
To display XML file on browser use this header.
<?php header("Content-type: text/xml"); ?> |
How to post XML using socket ?
I had discussed posting XML over HTTP using CURL in last post. Remember that was first method.
As I had promised on earlier post I would like to share second method with you. That is socket!!
Use this code and send your XML file.
<?php function postXMLToURL ($server, $path, $xmlDocument) { $contentLength = strlen($xmlDocument); $fp = fsockopen($server, 80, $errno, $errstr, 30); fputs($fp, "POST $path HTTP/1.0rn"); fputs($fp, "Host: $serverrn"); fputs($fp, "Content-Type: text/xmlrn"); fputs($fp, "Content-Length: $contentLengthrn"); fputs($fp, "Connection: closern"); fputs($fp, "rn"); // all headers sent fputs($fp, $xmlDocument); $result = ''; while (!feof($fp)) { $result .= fgets($fp, 128); } return $result; } function getBody ($httpResponse) { $lines = preg_split('/(rn|r|n)/', $httpResponse); $responseBody = ''; $lineCount = count($lines); for ($i = 0; $i < $lineCount; $i++) { if ($lines[$i] == '') { break; } } for ($j = $i + 1; $j < $lineCount; $j++) { $responseBody .= $lines[$j] . "n"; } return $responseBody; } $xmlpacket ='<AATHtlDispReq1> <Agency> <Iata>1234567890</Iata> <Agent>lgsoftwares</Agent> <Password>myapassword</Password> <Brand>phpmind.com</Brand> </Agency> <Passengers> <Adult AGE="" ID="1"></Adult> <Adult AGE="" ID="2"></Adult> </Passengers> <DestCode>OGG</DestCode> <CheckInDate>101009</CheckInDate> </AATHtlDispReq1>'; $result = postXMLtoURL("www.yourdomain.com", "/path/",$xmlpacket); $responseBody = getBody($result); echo $responseBody; ?> |
How to post XML using CURL?
Recently I was working in a hotel booking engine and found a couple of methods to post XML to server; I thought this might be good to share with my friends who want to post xml via HTTP POST method.
There are several ways to Send XML requests via HTTP POST.
I am going to show you two ways. Both are very simple and easy.
As first approach I have used a small xml file with CURL.
<?php $xml_data ='<AATAvailReq1>'. '<Agency>'. '<Iata>1234567890</Iata>'. '<Agent>lgsoftwares</Agent>'. '<Password>mypassword</Password>'. '<Brand>phpmind.com</Brand>'. '</Agency>'. '<Passengers>'. '<Adult AGE="" ID="1"></Adult>'. '<Adult AGE="" ID="2"></Adult>'. '</Passengers>'. '<HotelAvailReq1>'. '<DestCode>JHM</DestCode>'. '<HotelCode>OGGSHE</HotelCode>'. '<CheckInDate>101009</CheckInDate>'. '<CheckOutDate>101509</CheckOutDate>'. '<UseField>1</UseField>'. '</HotelAvailReq1>'. '</AATAvailReq1>'; $URL = "https://www.yourwebserver.com/path/"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_MUTE, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); ?> |

