Location: PHPKode > scripts > API > api/server_class.php
<?php

/**
 * Created on 2008/12/03
 * Created by KS Tuhin
 * 
 * This class use to parse and generate xml
 
 class my_vars {
 var $var_1;
 var $var_2;
 var $var_3;
} // end class

*/

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;
 
 
 function xml_server() {
 	
 	 $this->myXML_tag = false;
 	 $this->function_tag = false;
 	 $this->values_tag = false;
 	 $this->orderID_tag = false;
 	 $this->reference_tag = false;
 	 $this->errno = "0";
}

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

  $j = $this->orderID_tag + 9;
  $this->orderID = substr($xml, $j, 1);
  
  for ($i = $j + 1; substr($xml, $i, 1) != "<"; $i++) {
   $this->orderID .= substr($xml, $i, 1);
  }
  
  $j = $this->reference_tag + 11;
  $this->reference = substr($xml, $j, 1);
  for ($i = $j + 1; substr($xml, $i, 1) != "<"; $i++) {
   $this->reference .= substr($xml, $i, 1);
  }
 }

 // return true on success, false on failure
 return $success;
 
}

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
  
  //$this->doc();
  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;
}

function build_xml() {
	
	$xml = new DOMDocument('1.0');
	//$xml = domxml_new_doc("1.0");

  // create the elements
  $root = $xml->createElement("myXML");
  $values = $xml->createElement("values");
  $id = $xml->createElement("orderID");
  $ref = $xml->createElement("reference");
  $response = $xml->createElement("response");
  $order = $xml->createElement("order");

  // create the text nodes
  $id_txt = $xml->createTextNode($this->orderID);
  $ref_txt = $xml->createTextNode($this->reference);
  $ord_txt = $xml->createTextNode("confirmed");

  // append the elements &amp; nodes
  $root = $xml->appendChild($root);
  $values = $root->appendChild($values);
  $id = $values->appendChild($id);
  $id_txt = $id->appendChild($id_txt);
  $ref = $values->appendChild($ref);
  $ref_txt = $ref->appendChild($ref_txt);
  $response = $root->appendChild($response);
  $order = $response->appendChild($order);
  $ord_id = $order->appendChild($ord_txt);

  $xml = $xml->saveXML();

  return $xml;
}

/*function doc(){
	$doc = domxml_new_doc("1.0");
$root = $doc->create_element("HTML");
$root = $doc->append_child($root);
$head = $doc->create_element("HEAD");
$head = $root->append_child($head);
$title = $doc->create_element("TITLE");
$title = $head->append_child($title);
$text = $doc->create_text_node("This is the title");
$text = $title->append_child($text);
echo "<PRE>";
echo htmlentities($doc->dump_mem(true));
echo "</PRE>";
	
}*/

function build_error_xml() {

  $xml = domxml_new_doc("1.0");

  // create the elements
  $root = $xml->create_element("myXML");
  $fault = $xml->create_element("fault");
  $code = $xml->create_element("code");

  $code_txt = $xml->create_text_node($this->errno);

  $root = $xml->append_child($root);
  $fault = $root->append_child($fault);
  $code = $fault->append_child($code);
  $code_txt = $code->append_child($code_txt);

  $xml = $xml->dump_var();

  return $xml;  
}


 
}
  
?>
Return current item: API