I need to create a PHP script that receives XML input via an HTTP POST request, processes it, and then returns an XML response. I have spent quite a while attempting this on my own, and here is what I have so far.
I first quickly threw together an HTML form that allows a user to submit XML data as a string, an XML URI as a string, or an XML URI to my PHP script. This form is for testing only, as I am only tasked with creating this script.
In my PHP script I do some input handling…
// Checks that some XML input has been given
if (!(isset($_POST['xmlinput']))) {
handleErrors("No XML Input Given");
}
// Creates a new DOM Document
$domdoc = new DomDocument;
// Sets the URL of the XML Schema
$xmlschema = "xmlschema.xsd";
// If XML input is a file, tries to load it
if (file_exists($_POST['xmlinput'])) {
if (!$domdoc->load($_POST['xmlinput'])) {
handleErrors("Error in XML File");
}
}
// If XML input is not a file, tries to load it as a string
else {
if (!$domdoc->loadXML($_POST['xmlinput'])) {
handleErrors("Error in XML Document");
}
}
// Validates the XML against the schema
if (!($domdoc->schemaValidate($xmlschema))) {
handleErrors("XML Does Not Conform To Schema");
}
I then do some stuff based on what the XML data is, and then I want to generate an XML response. I am fairly certain I can create XML in DOM or SimpleXML, but I simply do not understand how to return it to the original page. Also, is this the best method to handle XML input in PHP? I have seen a ton of posts about php://input or $HTTP_RAW_POST_DATA but these don’t seem to be any better than the method I use. Any information you can give me would be a big help. If I can clarify any more, just let me know.
Very simply, you create your xml document using SimpleXml,
then just
echo $xml->asXML();If you need to post xml data to a webservice,
You can use the following code to acheive that.