/** Return the XML code from an Array or Object * @author Carlos André Ferrari <*****@*****.**> * @param mixed $array * @param String $container * @param Boolean $beginning * @param Integer $ident * @return String */ function x2xml($array, $container = 'root', $beginning = true, $ident = 0) { if (is_object($array)) { $array = (array) $array; } $output = $beginning ? '<' . '?' . 'xml version="1.0" encoding="UTF-8"' . '?' . '>' . PHP_EOL : ''; $output .= str_repeat("\t", $ident) . '<' . $container . '>' . PHP_EOL; foreach ($array as $k => $v) { if (!preg_match('@^[a-z0-9A-Z]@', $k)) { continue; } if (preg_match('@^[0-9]@', $k)) { $k = 'item'; } if (is_array($v) || is_object($v)) { $output .= x2xml($v, $k, false, $ident + 1); } else { if (!preg_match('@^[0-9a-zA-Z\\-_\\ \\.\\:\\,\\=\\/]*$@', $v)) { $v = '<![CDATA[' . $v . ']]>'; } if (is_bool($v)) { $v = $v ? 'true' : 'false'; } $output .= $v === '' || $v === null ? str_repeat("\t", $ident + 1) . '<' . $k . '/>' . PHP_EOL : str_repeat("\t", $ident + 1) . '<' . $k . '>' . $v . '</' . $k . '>' . PHP_EOL; } } $output .= str_repeat("\t", $ident) . '</' . $container . '>' . PHP_EOL; return $output; }
} else { // Senão, consulta... $obj = new Correio($codigo); // .. e renova o cache file_put_contents($cache_file, serialize($obj)); $obj->cached = false; } } else { // Retorna erro de código inválido $obj = json_decode('{"hash":null,"track":null,"status":null,"erro":true,"formato":"json","erro_msg":"C\\u00f3digo de encomenda Inv\\u00e1lido!"}'); } // Muda cabeçalho padrão de content para texto simples header("Content-Type: text/plain"); // Retorna no formato solicitado switch ($formato) { case 'serial': exit(serialize($obj)); case 'dump': print_r($obj); exit; case 'xml': header("Content-Type: text/xml"); include_once 'x2xml.php'; exit(x2xml($obj)); case 'json': default: if ($jsonp) { exit($jsonp . '(' . json_encode($obj) . ')'); } exit(json_encode($obj)); }