Example #1
0
 function xml2json($domElement)
 {
     $result = '';
     if ($domElement->nodeType == XML_COMMENT_NODE) {
         return '';
     }
     if ($domElement->nodeType == XML_TEXT_NODE) {
         /* text node, just return content */
         $text = trim($domElement->textContent);
         $text = addslashes($text);
         if ($text != '') {
             $result = '"' . $text . '"';
         } else {
             $text = '""';
         }
     } else {
         /* some other kind of node, needs to be processed */
         $aChildren = array();
         $aValues = array();
         /* attributes are considered child nodes with a special key name
            starting with @ */
         if ($domElement->hasAttributes()) {
             foreach ($domElement->attributes as $key => $attr) {
                 $len = array_push($aValues, array('"' . $attr->value . '"'));
                 $aChildren['@' . $key] = $len - 1;
             }
         }
         if ($domElement->hasChildNodes()) {
             //has children
             foreach ($domElement->childNodes as $child) {
                 if ($child->nodeType == XML_COMMENT_NODE) {
                     continue;
                 }
                 if ($child->nodeType == XML_TEXT_NODE) {
                     $text = trim($child->textContent);
                     $text = addslashes($text);
                     if ($text == '') {
                         continue;
                     }
                     array_push($aValues, array('"' . $text . '"'));
                 } else {
                     $childTag = $child->tagName;
                     $json = xml2json($child);
                     if ($json == '') {
                         continue;
                     }
                     if (array_key_exists($childTag, $aChildren)) {
                         array_push($aValues[$aChildren[$childTag]], $json);
                     } else {
                         $len = array_push($aValues, array($json));
                         $aChildren[$childTag] = $len - 1;
                     }
                 }
             }
         }
         $nChildren = count($aChildren);
         $nValues = count($aValues);
         if ($nChildren == 0 && $nValues == 0) {
             return '';
         }
         if ($nValues == 1 && $nChildren == 0) {
             $result .= $aValues[0][0];
         } else {
             $bIsObject = true;
             if ($nChildren != $nValues) {
                 $bIsObject = false;
             }
             $result .= $bIsObject ? '{' : '[';
             $sep = '';
             $aChildren = array_flip($aChildren);
             for ($i = 0; $i < $nValues; $i++) {
                 $aValue = $aValues[$i];
                 $result .= $sep;
                 if (isset($aChildren[$i])) {
                     if (!$bIsObject) {
                         $result .= '{';
                     }
                     $result .= '"' . $aChildren[$i] . '":';
                 }
                 //if (count($aValue) > 1) {
                 $result .= '[';
                 $result .= implode(',', $aValue);
                 $result .= ']';
                 //} else {
                 //    $result .= $aValue[0];
                 //}
                 if (isset($aChildren[$i]) && !$bIsObject) {
                     $result .= '}';
                 }
                 $sep = ',';
             }
             $result .= $bIsObject ? '}' : ']';
         }
     }
     return $result;
 }
Example #2
0
function curl_request($url, $accept)
{
    //Ricevo l'arrey con gli header della risposta
    $arrey_headers = get_headers($url, 1);
    //Salvo il Content-Type della risposta
    $content = $arrey_headers["Content-Type"];
    //Richiesta CURL
    if (!function_exists('curl_init')) {
        die('Sorry cURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/turtle, text/csv, application/json,  application/xml"));
    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    //Genero nome random per il file temporaneo
    $nomefiletemp = random_string(5);
    //Creo un file temporaneo dove salvare il risultato
    $ourFileHandle = fopen($nomefiletemp . ".txt", 'w+') or die("can't open file");
    fwrite($ourFileHandle, $output);
    fclose($ourFileHandle);
    //Leggo il Content-type della risposta e se necessario converto
    switch ($content) {
        case "application/json":
            $xml = json2xml($nomefiletemp . ".txt");
            break;
        case "text/csv":
            $xml = csv2xml($nomefiletemp . ".txt");
            break;
        case "text/turtle":
            $xml = turtle2xml($nomefiletemp . ".txt");
            break;
        case "application/xml":
            $xml = $output;
            break;
            //Errore
        //Errore
        case "text/plain":
            sendHTTPError($output, 'Sorgente errore --> aggregatore');
            break;
        default:
            unlink($nomefiletemp . ".txt");
            sendHTTPError('Attenzione', 'Formato ricevuto non accettato, Sorgente errore --> aggregatore');
            break;
    }
    //remove temp file
    unlink($nomefiletemp . ".txt");
    //Genero nome random per il file temporaneo
    $nomefiletemp2 = random_string(5);
    //Creo file temporaneo con la risposta convertita in formato XML
    $ourFileHandle2 = fopen($nomefiletemp2 . ".xml", 'w+') or die("can't open file");
    fwrite($ourFileHandle2, $xml);
    fclose($ourFileHandle2);
    //Converto nel formato richiesto dal narratore
    switch ($accept) {
        case "application/json":
            $result = xml2json($nomefiletemp2 . ".xml");
            break;
        case "text/csv":
            $result = xml2csv($nomefiletemp2 . ".xml");
            break;
        case "text/turtle":
            $result = turtle2xml($nomefiletemp2 . ".xml");
            break;
        case "application/xml":
            $result = $xml;
            break;
        default:
            unlink($nomefiletemp2 . ".xml");
            sendHTTPError('Attenzione', 'Formato richiesto non accettato');
            break;
    }
    //remove temp file
    unlink($nomefiletemp2 . ".xml");
    return $result;
}
Example #3
0
    $xml = trim($xml);
    $xml = str_replace('\\"', '"', $xml);
    $xml = str_replace('&quot;', "'", $xml);
    $xml = preg_replace('/\\n/', ' ', $xml);
} elseif (isset($argv)) {
    $cliArgs = arguments($argv);
    if (isset($cliArgs['obj'])) {
        $jsObject = $cliArgs['obj'];
    } else {
        $jsObject = "Fusion.appDefJson";
    }
    if (isset($cliArgs['file'])) {
        $xml = file_get_contents($cliArgs['file']);
        if (!$xml) {
            die('file not found:' . $cliArgs['file']);
        }
        echo $jsObject . "=";
    }
} else {
    die('no XML input');
}
//echo "/*";
//print_r($xml);
//echo "*/";
$document = DOMDocument::loadXML($xml);
if ($document == null) {
    die('/* invalid xml document:' . $xml . ' */');
}
$root = $document->documentElement;
echo '{"' . $root->tagName . '":' . xml2json($root) . '}';