XML Creator

This is a class that I found laying around, and I decided to share it. It is used to generate a valid XML , and to send it to an API / Gateway. It is made to be very simple to modify, and small. The idea behind it is to experiment with the get_object_vars() in order to minimize the actual code needed to create the XML. We start by creating a new instance, and assigning variables to that instance. Since they don’t need to be declared in the class beforehand, we can declare whatever we decide and to use the those variable names as XML elements. Since the naming rules for the XML elements are almost the same as the naming rules for PHP Class variables, we should be able to skip that check. After that, based on the structure we created,  get_object_vars() will convert the newly created stdClass object variables into the proper XML structure. If you don’t assign a value to an variable, then it will be considered as parent.

Example:

<?php

include('xmlcreator.php');

$xml = new XMLcreator;

$xml->book = "Gone with the wind";
$xml->author = "Margaret Mitchell";
$xml->chapter->one = "Scarlett O'Hara";
$xml->chapter->two = "When the twins left Scarlett";
$xml->process();

 echo $xml->request;
 echo $test->result;
?>

Result:

<Document>
  <book>Gone with the wind</book>
  <author>Margaret Mitchell</author>
  <chapter>
    <one>Scarlett O'Hara</one>
    <two>When the twins left Scarlett</two>
  </chapter>
  <TransactionID>1309489958</TransactionID>
</Document>

And this is the class itself:

<?php

  class XMLcreator
    {
        public function process($url = NULL)
        {
            //create the xml document
            $xmlDoc = new DOMDocument();

            //create the root element
            $root = $xmlDoc->appendChild($xmlDoc->createElement("Document"));

            //get all the class variables
            $class_vars = get_object_vars($this);

            //add them to the xml
            foreach ($class_vars as $name => $value) {
                if(!is_object($value))
                {
                    $current = $xmlDoc->createElement($name, $value);
                }
                else
                {
                    $current = $xmlDoc->createElement($name);
                    foreach ($value as $name => $value) {
                        if(!is_object($value))
                        {
                            $current->appendChild($xmlDoc->appendChild(
                                $xmlDoc->createElement($name, $value)));
                        }
                    }
                }
                $root->appendChild($current);
            }

            //hardcode the elements needed on every request
            $root->appendChild($xmlDoc->appendChild($xmlDoc->createElement("TransactionID", time())));

            //pretty formating
            $xmlDoc->formatOutput = true;

            //save the xml as string
            $this->xml = $xmlDoc->saveXML($root);

            // save the request in variable
            $this->request = $this->xml;

            //send it to the send function
            $this->send($url);
        }

        protected function send($url = NULL)
        {

            //set the default gateway address, but make it so it can be overwritten
            $url = ($url) ? $url : "http://www.example.com/receive.php";

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $this->xml);
            //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
            //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            $response = curl_exec ($curl);
            curl_close ($curl);

            // save the result in variable
            $this->result = $response;
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *