XML

Sends XML response

@param array $arr
@return string
555097
public static function xml(array $arr) {
        parent::setContentType('application/xml');

        $simpleXML = new \SimpleXMLElement('<?xml version="1.0"?><data></data>');
        self::arrayToXML($arr, $simpleXML);

        return $simpleXML->asXML();
    }
333966
public static function xmlToArray($xml) {
	  // check passed data
    if(!is_string($xml)) {
	    // bad data possed
      throw new LinkedInException('LinkedIn->xmlToArray(): bad data passed, $xml must be a non-zero length string.');
	  }
	  
	  $parser = xml_parser_create();
	  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    if(xml_parse_into_struct($parser, $xml, $tags)) {
	    $elements = array();
      $stack    = array();
      foreach($tags as $tag) {
        $index = count($elements);
        if($tag['type'] == 'complete' || $tag['type'] == 'open') {
          $elements[$tag['tag']]               = array();
          $elements[$tag['tag']]['attributes'] = (array_key_exists('attributes', $tag)) ? $tag['attributes'] : NULL;
          $elements[$tag['tag']]['content']    = (array_key_exists('value', $tag)) ? $tag['value'] : NULL;
          if($tag['type'] == 'open') {
            $elements[$tag['tag']]['children'] = array();
            $stack[count($stack)] = &$elements;
            $elements = &$elements[$tag['tag']]['children'];
          }
        }
        if($tag['type'] == 'close') {
          $elements = &$stack[count($stack) - 1];
          unset($stack[count($stack) - 1]);
        }
      }
      $return_data = $elements;
	  } else {
	    // not valid xml data
	    $return_data = FALSE;
	  }
	  xml_parser_free($parser);
    return $return_data;
  }
1213656
private static function generateXmlFromArray($array, $node_name) 
    {
        $xml = '';

        if (is_array($array) || is_object($array)) {
            foreach ($array as $key=>$value) {
                if (is_numeric($key)) {
                    $key = $node_name;
                }

                $xml .= '<' . $key . '>' . self::generateXmlFromArray($value, $node_name) . '</' . $key . '>';
            }
        } else {
            $xml = htmlspecialchars($array, ENT_QUOTES);
        }

        return $xml;
    }
419889
public static function xmlToArray($xml, $format = self::XML_GROUP) {
		if (is_string($xml)) {
			$xml = @simplexml_load_string($xml);
		}

		if (count($xml->children()) <= 0) {
			return (string)$xml;
		}

		$array = array();

		foreach ($xml->children() as $element => $node) {
			$data = array();

			if (!isset($array[$element])) {
				$array[$element] = "";
			}

			if (!$node->attributes() || $format === self::XML_NONE) {
				$data = self::xmlToArray($node, $format);

			} else {
				switch ($format) {
					case self::XML_GROUP:
						$data = array(
							'attributes' => array(),
							'value' => (string)$node
						);

						if (count($node->children()) > 0) {
							$data['value'] = self::xmlToArray($node, $format);
						}

						foreach ($node->attributes() as $attr => $value) {
							$data['attributes'][$attr] = (string)$value;
						}
					break;

					case self::XML_MERGE:
					case self::XML_OVERWRITE:
						if ($format === self::XML_MERGE) {
							if (count($node->children()) > 0) {
								$data = $data + self::xmlToArray($node, $format);
							} else {
								$data['value'] = (string)$node;
							}
						}

						foreach ($node->attributes() as $attr => $value) {
							$data[$attr] = (string)$value;
						}
					break;
				}
			}

			if (count($xml->{$element}) > 1) {
				$array[$element][] = $data;
			} else {
				$array[$element] = $data;
			}
		}

		return $array;
	}
1240012
public function https_xml($url, $xml = null)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);

        return $response;
    }
326976
protected function prepareTree(array $arr) {
        $tree = [];
        foreach ($arr as $a) {
            $name = $a->getName();
            $cur = &$tree;
            foreach (explode("-", $name) as $e) {
                if (empty($cur[$e])) $cur[$e] = [];
                $cur = &$cur[$e];
            }
        }
        return $tree;
    }
1150242
public static function xml2array($xml)
    {
        $arr = [];
        if (!empty($xml)) {
            if (is_string($xml)) {
                $xml = simplexml_load_string($xml);
            }
            foreach ($xml->attributes() as $a) {
                $arr['@'.$a->getName()] = strval($a);
            }
            foreach ($xml->children() as $r) {
                if (count($r->children()) == 0) {
                    $arr[$r->getName()] = strval($r);
                } else {
                    $arr[$r->getName()][] = self::xml2array($r);
                }
            }
        }
        return $arr;
    }
282510
protected static function export(array $arr)
	{
		$exportKeys = (array_keys($arr) !== range(0, count($arr) - 1));
		ksort($arr);

		$entries = [];
		foreach ($arr as $k => $v)
		{
			$entries[] = (($exportKeys) ? var_export($k, true) . '=>' : '')
			           . ((is_array($v)) ? self::export($v) : var_export($v, true));
		}

		return '[' . implode(',', $entries) . ']';
	}
442783
private function array2xml($arr, $level = 1)
    {
        $str = ($level == 1) ? "<?xml version=\"1.0\" encoding=\"" . Config::get('default_charset') . "\"?>\r\n<root>\r\n" : '';
        $space = str_repeat("\t", $level);
        foreach ($arr as $key => $val) {
            if (is_numeric($key)) {
                $key = 'item';
            }
            if (!is_array($val)) {
                if (is_string($val) && preg_match('/[&<>"\'\?]+/', $val)) {
                    $str .= $space . "<$key><![CDATA[" . $val . ']]>' . "</$key>\r\n";
                } else {
                    $str .= $space . "<$key>" . $val . "</$key>\r\n";
                }
            } else {
                $str .= $space . "<$key>\r\n" . self::array2xml($val, $level + 1) . $space . "</$key>\r\n";
            }
        }
        if ($level == 1) {
            $str .= '</root>';
        }
        return $str;
    }
756383
public static function arrayToXml(array $data)
    {
        $xml = '';
        foreach ($data as $key => $value) {
            $tag = (is_numeric($key)) ? 'item' : $key;

            $xml = (!empty($xml)) ? $xml : '';
            if (is_array($value)) {
                $xml .= "<$tag index=\"" . $key . "\">" . self::arrayToXml($value) . "</$tag>";
            } else {
                $xml .= "<$tag>" . $value . "</$tag>";
            }
        }

        return $xml;
    }
746409
private function _XML2Array($xml)
    {
        $envelope = simplexml_load_string($xml, "SimpleXMLElement", 0, 'http://schemas.xmlsoap.org/soap/envelope/');
        foreach ($envelope->Body as $response) {
            foreach ($response->children('http://Hps.Exchange.PosGateway') as $item) {
                return $item;
            }
        }
        return null;
    }
899729
private function sendRequest($xml)
    {
        $url = $this->url;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type:application/xml"]);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $resp = curl_exec($curl);
        if (false === $resp) {
            throw new Exception(curl_error($curl) . ' ' . curl_errno($curl), curl_errno($curl));
        }

        curl_close($curl);

        return $resp;
    }
557738
public function responseToArray($xml)
    {
        $doc = new \DOMDocument();
        $doc->loadXML($xml);
        $out = $this->nodeToArray($doc->documentElement);

        return $out['Response'];
    }
1129568
public static function xml2arr($xml)
    {
        $entity = libxml_disable_entity_loader(true);
        $data = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        libxml_disable_entity_loader($entity);
        return json_decode(json_encode($data), true);
    }
1121129
function getTokenFromArray( $arr ) {
    if (isset($arr['access_token']) && $arr['access_token']) {
      $token = array();
      $this->access_token = $token['access_token'] = $arr['access_token'];
      if (isset($arr['refresh_token']) && $arr['refresh_token']) {
        $this->refresh_token = $token['refresh_token'] = $arr['refresh_token'];
      }

      return $token;
    } else {
      return false;
    }
  }
444854
private function &convert($node_name, $arr = [])
    {
        $xml = $this->getXMLRoot();
        $node = $xml->createElement($node_name);
        if (is_array($arr)) {
            // get the attributes first.;
            if (isset($arr['@attributes'])) {
                foreach ($arr['@attributes'] as $key => $value) {
                    if (!$this->isValidTagName($key)) {
                        throw new Exception('[Array2XML] Illegal character in attribute name.'
                            . 'attribute: ' . $key . ' in node: ' . $node_name);
                    }
                    $node->setAttribute($key, $this->boolString($value));
                }
                unset($arr['@attributes']);
            }
            // check if it has a value stored in @value,
            // if yes store the value and return else check if its directly stored as string
            if (isset($arr['@value'])) {
                $node->appendChild($xml->createTextNode($this->boolString($arr['@value'])));
                unset($arr['@value']);

                return $node;
            } else {
                if (isset($arr['@cdata'])) {
                    $node->appendChild($xml->createCDATASection($this->boolString($arr['@cdata'])));
                    unset($arr['@cdata']);

                    return $node;
                }
            }
        }
        //create subnodes using recursion
        if (is_array($arr)) {
            // recurse to get the node for that key
            foreach ($arr as $key => $value) {
                if (!$this->isValidTagName($key)) {
                    throw new Exception('[Array2XML] Illegal character in tag name.'
                        . 'tag: ' . $key . ' in node: ' . $node_name);
                }
                if (is_array($value) && is_numeric(key($value))) {
                    // MORE THAN ONE NODE OF ITS KIND
                    // if the new array is numeric index, means it is array of nodes of the same kind
                    // it should follow the parent key name
                    foreach ($value as $k => $v) {
                        $node->appendChild($this->convert($key, $v));
                    }
                } else {
                    // ONLY ONE NODE OF ITS KIND
                    $node->appendChild($this->convert($key, $value));
                }
                unset($arr[$key]); //remove the key from the array once done.
            }
        }
        // after we are done with all the keys in the array (if it is one)
        // we check if it has any text value, if yes, append it.
        if (!is_array($arr) && $arr !== '') {
            $node->appendChild($xml->createTextNode($this->boolString($arr)));
        }

        return $node;
    }
499989
private function fromArrayToXmlString(array $array)
    {
        $string = '';

        foreach ($array as $key => $value) {
            $string .= '<' . $key . '>' .
                ((is_array($value))
                    ? $this->fromArrayToXmlString($value)
                    : $value) .
                '</' . $key . '>';
        }

        return $string;
    }