Search code examples
phpxmlparsingxml-parsingunzip

Parse a ziped XML file with PHP from an URL


I would like to parse a XML file from URL and with PHP. For the moment, with this code, I can have the content of the XML file, but I am not succeeding in using the simplexml_load_string in order to use it as a XML content.

Indeed, when I echo zip, the text appears without the XML tags. Would you have an idea? Thanks.

<?php
$file = 'http://url_here_.zip';
$newfile = 'tmp_name_file.zip';

if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";}

 $zip = new ZipArchive();

 if ($zip->open($newfile)!==TRUE) {
    exit("cannot open <$filename>\n");
   } else {
    $zip->getFromName('Scrutins_XIV.xml');
    $xml = simplexml_load_string(file_get_contents($zip));
    $zip->close();
   }
 ?>

Solution

  • Try this:

    $xml_string = $zip->getFromName('Scrutins_XIV.xml');
    if ( $xml_string!= false ) {
        $xml = simplexml_load_string($xml_string);
    }