There are 2 things you can do here. 1 - you can set up a "variables class" (because you could have quite a few with your own, here we will only have a select few so we aren't going to do this) and then make your server class an extention of your variables class. You'd set this up like so:
<?php class my_vars { var $var_1; var $var_2; var $var_3; } // end class class xml_server Extends my_vars { function xml_server() { // server set up } // end xml_server() } // end class
?>
2 - you can lump it all together. Here we only have a very small XML request so we won't really have a problem with everything under one class.
So first, we have our class variables.
<?php class xml_server { // variables to check the user built a valid request var $myXML_tag; var $function_tag; var $values_tag; var $orderID_tag; var $reference_tag;
// variables to set values sent in var $function; var $orderID; var $reference;
// error variables var $errno; }
?>
Next... we create our class constructor (set variables)
<?php function xml_server() { var $this->myXML_tag = false; var $this->function_tag = false; var $this->values_tag = false; var $this->orderID_tag = false; var $this->reference_tag = false; var $this->errno = "0"; }
?>
We set each tag to false (so when we verify it was sent we can change to true. Set errno to "0" indicating no errors!
Next we are going to set up the xml_parse() function.(from our server page). This is a basic and primitive XML parser, we are going to check that the desired elements are in the request, that those elements have proper closing and opening tags, that the document is well-formed (almost). A major drawback is "extra info". The client application could possibly send in extra data in the XML request. We don't check for any of this extra data, but you may want to add that into your own parser.
<?php function parse_xml($xml) { $success = true; // variable to verify good xml and good request $xml = trim($xml); // get rid of excess white space that may or may not be present
/* Set _tag variables to the position in the file where it can be found the strpos() function returns false on failure to find the given string Since this is our API, we know what to look for and what is needed */ $this->myXML_tag = strpos($xml, "<myXML>"); // 7 - this is the string length $this->function_tag = strpos($xml, "<function>"); // 10 $this-orderID_tag = strpos($xml, "<orderID>"); // 9 $this->reference_tag = strpos($xml, "<reference>"); // 11
// Verify the correct elements of a request were sent in
if (!$this->myXML_tag) { $success = false; $this->errno = "101"; } if (!$this->function_tag) { $success = false; $this->errno = "101"; } if (!$this->orderID_tag) { $success = false; $this->errno = "101"; } if (!$this->reference_tag) { $success = false; $this->errno = "101"; }
/* Verify we have a well formed XML request (note: this is not a very good validator, it does not check for extra content, only that the needed content is there - but works well for what we are doing here) */ if ($success) { if (!strpos($xml, "</myXML>")) { $success = false; $this->errno = "100"; } if (!strpos($xml, "</function>")) { $success = false; $this->errno = "100"; } if (!strpos($xml, "</orderID>")) { $success = false; $this->errno = "100"; } if (!strpos($xml, "</reference>")) { $success = false; $this->errno = "100"; } }
if ($success) { $j = $this->function_tag + 10; // grab the first char after the open tag $this->function = substr($xml, $j, 1); /* grab the characters until you hit a '<'. Note in your manual that you write with your API that '<' characters will cause a fault. It is your API so, you can do whatever you want! Technically, you're only bound by the rules of the XML language. */ for ($i = $j + 1; substr($xml, $i, 1) != "<"; $i++) { $this->function .= substr($xml, $i, 1); }
// return true on success, false on failure return $success;
}
?>
There's our basic xml parser - not the world's greatest, but it gets the job done. As you may have noticed by now, we are using these codes for error conditions. 100 is for badly formed XML, 101 is for missing required element, 200 is for server creation error. The system is based around 1XX - XML data, 2XX - server stuff (errors not caused by the xml request), 3XX - request info. So a badly formed XML request would yield a 100 error. If the user sends a "ordre" request (misspelled order), the request is invalid and would yield a 300 error. You can build your own error condition/code system with your own API.
Next we will set up our xml generator using the XML DOM. The XML DOM has been replaced by the DOM in PHP5, but I don't have PHP5 so I'm still building with the old stuff. Hey, at least it's not experimental anymore! The following function will generate our XML message to send back to the user:
<?php function generate_xml() { // check to make sure no errors have been found so far (from parsing) // if no errors build an xml response by the function if ($this->errno == "0") { if ($this->function == "order") { $xml = $this->build_xml(); } else { $this->errno = "300"; // 300 - function not recognized $xml = $this->build_error_xml(); } } return $xml; }
This is a simple xml generator to generate xml on the fly. I set up 2 different functions for the good request and bad request xml generation. The final step, as always - test...