LSS\XML2Array::createArray PHP Method

createArray() public static method

Convert an XML to Array
public static createArray ( $input_xml, $options ) : DOMDocument
return DOMDocument
    public static function &createArray($input_xml, $options = 0)
    {
        $xml = self::getXMLRoot();
        if (is_string($input_xml)) {
            $parsed = $xml->loadXML($input_xml, $options);
            if (!$parsed) {
                throw new Exception('[XML2Array] Error parsing the XML string.');
            }
        } else {
            if (get_class($input_xml) != 'DOMDocument') {
                throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
            }
            $xml = self::$xml = $input_xml;
        }
        $array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
        self::$xml = null;
        // clear the xml node in the class for 2nd time use.
        return $array;
    }

Usage Example

Beispiel #1
0
 /**
  * Confirmation of the request
  */
 public function execute($config)
 {
     // Post strings
     $xml_post_string_one = '<request><EventType>ConfirmMeter</EventType><event><DeviceId>' . $config['DeviceId'] . '</DeviceId><DeviceSer>' . $config['DeviceSer'] . '</DeviceSer><UserPin>' . $config['UserPin'] . '</UserPin><MeterNum>' . $this->meterNumber . '</MeterNum><Amount>0</Amount><Reference></Reference></event></request>' . PHP_EOL;
     $xml_post_string_two = '<request><SessionId></SessionId><EventType>Reprint</EventType><event><TransRef>' . $this->transref . '</TransRef><MeterNum>' . $this->meterNumber . '</MeterNum><OrigReference>' . $this->reference . '</OrigReference></event></request>' . PHP_EOL;
     // Create a TCP/IP socket
     $socket = new \CodeChap\Aeon\Socket($config);
     // STEP 1. Authenticate //
     // Send confirmation request
     $socket->write($xml_post_string_one);
     // Get result of send
     $result_one = $socket->get();
     // STEP 2. Reprint //
     // Find session id field
     preg_match('/<SessionId>(.*)<\\/SessionId>/', $result_one, $SessionId);
     // Swop in Session ID
     $xml_post_string_two = preg_replace('/<SessionId><\\/SessionId>/', $SessionId[0], $xml_post_string_two);
     // Send confirmation request
     $socket->write($xml_post_string_two);
     // Get result of send
     $result = $socket->get();
     // Done
     $finalResult = \LSS\XML2Array::createArray($result);
     // Return usefull data
     return $finalResult['response'];
 }
All Usage Examples Of LSS\XML2Array::createArray