如何使用PHP解压xml文件

如何使用PHP解压xml文件,php,xml,zip,xmldom,Php,Xml,Zip,Xmldom,我的PHP系统上有一个压缩的XML文件 如何将其解压缩以加载到XMLDOM 我每个zip存档只有一个XML文件 提前感谢。您可以使用php类,即: open($zipFile)) { //获取zip文件中的xml文件名 $xmlFile=$zip->getNameIndex(0);//您只有一个xml文件在zip的IDE中 $zip->close(); //读取zip中的xml文件,而不将其解压缩到磁盘(内存) $xml=file_get_contents(“zip://$zipFile#$xm

我的
PHP
系统上有一个压缩的
XML
文件

如何将其解压缩以加载到
XMLDOM

我每个
zip
存档只有一个
XML
文件


提前感谢。

您可以使用php类,即:

open($zipFile))
{
//获取zip文件中的xml文件名
$xmlFile=$zip->getNameIndex(0);//您只有一个xml文件在zip的IDE中
$zip->close();
//读取zip中的xml文件,而不将其解压缩到磁盘(内存)
$xml=file_get_contents(“zip://$zipFile#$xmlFile”);
//创建新文档
$dom=新的DOMDocument('1.0','UTF-8');
//加载xml文件
$dom->loadXML($xml);
//在这里,您可以操作XMLDOM、添加、删除节点等。
//保存并回显XML
echo$dom->saveXML();
}否则{
echo“zip打开失败”;
}

注:

  • 我已经用测试了代码,并且它按照预期工作
  • 0){
    $epgVersion=TRUE;
    foreach($contents作为$content){
    $file_name=trim($content);
    $xmlDirectory=“/home/customscript/epg/epg_xml_data/$date”;
    如果(!is_dir($xmlDirectory)){
    mkdir($xmlDirectory);
    }
    $xmlTmpDirectory=“/home/customscript/epg/epg_xml_tmp”;
    如果(!is_directory($xmlTmpDirectory)){
    mkdir($xmlTmpDirectory);
    }
    $local_file=$xmlDirectory.“/”$file_name;
    //尝试下载$server\u文件并保存到$local\u文件
    if(ftp_-get($ftp_-conn,$local_-file,$file_-name,ftp_-BINARY)){
    echo“成功写入$local\u文件\n”;
    //投寄至AppV
    postToAppinventiv($local_文件);
    副本($xmlDirectory./“$file\u name,$xmlTmpDirectory./”$file\u name);
    $fileParseStatus=parseXmlFile($xmlTmpDirectory./“$file_name,2);
    如果($fileParseStatus){
    echo“文件“$File\u name”已成功处理。\n”;
    if(取消链接($xmlTmpDirectory./“$file_name)){
    echo“文件“$File\u name”已成功删除。\n”;
    }否则{
    回显“删除文件“$File\u name”失败。\n”;
    }
    }否则{
    echo“无法打开“$file\u name”XML文件。\n”;
    }
    }否则{
    echo“出现问题\n”;
    }
    if(ftp\u delete($ftp\u conn,$file\u name)){
    echo“$file\u name已成功从ftp中删除\n”;
    }否则{
    echo“$file\u name无法从ftp中删除\n”;
    }
    }
    echo“已完成XML文件的处理。\n”;
    }否则{
    echo“没有要处理的xml文件。\n”;
    }
    }否则{
    echo“无法连接到FTP服务器。\n”;
    }
    ftp_关闭($ftp_连接);
    如果($epgVersion){
    $epgUpdateQuery=“更新版本集EPG\U版本=EPG\U版本+1”;
    $epgUpdateQueryStatus=mysql\U查询($epgUpdateQuery,$link);
    if($epgUpdateQueryStatus){
    echo“EPG版本更新成功。\n”;
    }否则{
    echo“EPG版本更新查询失败。\n”;
    }
    }
    回显“结束时间-”。日期('Y-m-d H:i:s')。“\n”;
    函数parseXmlFile($filename,$queryType){
    $link=$GLOBALS['link'];
    $file\u get=文件内容($filename);
    如果($file\u get){
    $xml=simplexml\u load\u string($file\u get);
    $chan_category=mysql_escape_字符串(trim($xml->channel->category));
    $channel_name=mysql_escape_字符串(trim($xml->channel->{'display-name'}));
    $service\u id=mysql\u escape\u字符串(trim($xml->channel->serviceid));
    $channel_logo=mysql_escape_字符串(trim($xml->channel->ChannelLogo));
    $Program=$xml->Program;
    $Program_date=trim($xml->Program->attributes()->start);//删除当前日期以上的数据
    $expld_date=分解(“,$PROGRAME_date”);
    $s_curdate=trim($expld_date[0]);
    $start_curtime_date=日期(“Y-m-d”,标准时间($s_CURTDATE));
    如果($queryType==1){
    $deleteType=“>=”;
    }else if($queryType==2){
    $deleteType=“=”;
    }
    $sql\u del=“从'Program\u Details'中删除,其中Service\u Id='$Service\u Id'和日期('Start\u Time`)$deleteType'$Start\u curtime\u date';
    mysql\u查询($sql\u del,$link);
    //echo$sql\u del.PHP\u EOL;
    $i=0;
    $insert_PROGRAMES_sql=array();
    foreach(作为$prog的计划){
    $cast=$prog->cast;
    $actors='';
    $董事='';
    $producers='';
    若有($cast){
    foreach($c){
    $actor_arr=array();
    foreach($c->actor as$t){
    $actor_arr[]=(string)mysql_escape_string(trim($t));
    }
    $actors=内爆(“,”,$actor_arr);
    $directors=mysql_escape_字符串(trim($c->director));
    $producers=mysql_escape_字符串(trim($c->producer));
    }
    }
    $start=(字符串)trim($prog->attributes()->start);
    $stop=(字符串)trim($prog->attributes()->stop);
    $expld_sdate=explode(“,$start”);
    $s_date=trim($expld_sdate[0]);
    $start_time=date(“Y-m-d H:i:s”,strottime($s_date));
    $expld_edate=爆炸(“,$stop”);
    $e_date=trim($expld_edate[0]);
    $end_time=date(“Y-m-d H:i:s”,strottime($e_date));
    //创建两个新的DateTime对象。。。
    $date1=新日期时间($start);
    $date2=新日期时间($stop);
    //diff方法返回一个新的DateInterval对象。。。
    $diff=$date2->diff($date1);
    //在DateInterval上调用format方法
    
    <?php
    //set the correct xml headers to output to browser
    header("Content-type: text/xml");
    $zipFile = "file.zip";
    $zip = new ZipArchive;
    if ($zip->open($zipFile))
    {
        //get the xml filename inside the zip
        $xmlFile =  $zip->getNameIndex(0); //you only  have 1 xml file iside of the zip
        $zip->close();
        //read the xml file inside the zip without extracting it to disk (memory)
        $xml = file_get_contents("zip://$zipFile#$xmlFile");
        //create a new document
        $dom = new DOMDocument('1.0', "UTF-8");
        //load teh xml file
        $dom->loadXML($xml);
        //Here you can  manipulate the XML dom , add, remove nodes, etc.
        //save and echo the XML
        echo $dom->saveXML();
    } else {
        echo 'zip open failed failed';
    }
    
    <?php
    
    set_time_limit(0);
    
    echo "Start Time - " . date('Y-m-d H:i:s') . "\n";
    
    //MySQL connection
    //$link = mysql_connect('HOST', 'userbane', 'password')or die("Dtabase Not connected.");
    $link = mysql_connect('HOST', 'userbane', 'password')or die("Dtabase Not connected.");
    mysql_select_db("unified", $link);
    
    //FTP connection
    $ftp_server = "xx.xx.xxxx";
    $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    
    $epgVersion = FALSE;
    
    if (ftp_login($ftp_conn, 'usename', 'password')) {
    
        $date = date('Y-m-d');
    
        //Processing ZIP Files
        echo "Processing ZIP Files\n";
        $contents = ftp_nlist($ftp_conn, "-t *.zip");
    
        if (count($contents) > 0) {
    
            $epgVersion = TRUE;
    
            foreach ($contents as $content) {
                $file_name = trim($content);
    
                $zipDirectory = "/home/customscript/epg/epg_zip_data/$date";
    
                if (!is_dir($zipDirectory)) {
                    mkdir($zipDirectory);
                }
    
                $zipTmpDirectory = "/home/customscript/epg/epg_zip_tmp";
    
                if (!is_dir($zipTmpDirectory)) {
                    mkdir($zipTmpDirectory);
                }
    
                $local_file = $zipDirectory . "/" . $file_name;
                // try to download $server_file and save to $local_file
                if (ftp_get($ftp_conn, $local_file, $file_name, FTP_BINARY)) {
                    echo "Successfully written to $local_file\n";
    
                    //Posting to Appinventiv
                    postToAppinventiv($local_file);
            //continue;
    
                    $zip = new ZipArchive;  //unzip file
                    if ($zip->open($local_file) === TRUE) {  //extract contents to /data/ folder
                        $zip->extractTo($zipTmpDirectory . '/');
                        $zip->close();            //close the archive
    
                        $files = glob($zipTmpDirectory . "/" . "*.xml");
    
                        if (is_array($files)) {
    
                            foreach ($files as $filename) {
    
                                $fileParseStatus = parseXmlFile($filename, 1);
                                if ($fileParseStatus) {
                                    echo "File '$filename' has been successfully processed.\n";
                                    if (unlink($filename)) {
                                        echo "File '$filename' deleted successfully.\n";
                                    } else {
                                        echo "File '$filename' deleted Failed.\n";
                                    }
                                } else {
                                    echo "Unable to open '$filename' XML file.\n";
                                }
                            }
                        }
                        //unlink($local_file);
                    } else {
                        echo "Failed to open the archive!\n";
                    }
                } else {
                    echo "There was a problem\n";
                }
    
                if (ftp_delete($ftp_conn, $file_name)) {
                    echo "$file_name deleted from ftp successfully\n";
                } else {
                    echo "$file_name could not be deleted from ftp\n";
                }
            }
            echo "Processing of ZIP Files are done.\n";
        } else {
            echo "No zip file to process.\n";
        }
    
        //Processing XML Files
        echo "Processing XML Files\n";
        $contents = ftp_nlist($ftp_conn, "-t *.xml");
    
        if (count($contents) > 0) {
    
            $epgVersion = TRUE;
    
            foreach ($contents as $content) {
                $file_name = trim($content);
    
                $xmlDirectory = "/home/customscript/epg/epg_xml_data/$date";
    
                if (!is_dir($xmlDirectory)) {
                    mkdir($xmlDirectory);
                }
    
                $xmlTmpDirectory = "/home/customscript/epg/epg_xml_tmp";
    
                if (!is_dir($xmlTmpDirectory)) {
                    mkdir($xmlTmpDirectory);
                }
    
                $local_file = $xmlDirectory . "/" . $file_name;
                // try to download $server_file and save to $local_file
                if (ftp_get($ftp_conn, $local_file, $file_name, FTP_BINARY)) {
                    echo "Successfully written to $local_file\n";
    
                    //Posting to Appinventiv
                    postToAppinventiv($local_file);
    
                    copy($xmlDirectory . "/" . $file_name, $xmlTmpDirectory . "/" . $file_name);
    
                    $fileParseStatus = parseXmlFile($xmlTmpDirectory . "/" . $file_name, 2);
                    if ($fileParseStatus) {
                        echo "File '$file_name' has been successfully processed.\n";
                        if (unlink($xmlTmpDirectory . "/" . $file_name)) {
                            echo "File '$file_name' deleted successfully.\n";
                        } else {
                            echo "File '$file_name' deleted Failed.\n";
                        }
                    } else {
                        echo "Unable to open '$file_name' XML file.\n";
                    }
                } else {
                    echo "There was a problem\n";
                }
    
                if (ftp_delete($ftp_conn, $file_name)) {
                    echo "$file_name deleted from ftp successfully\n";
                } else {
                    echo "$file_name could not be deleted from ftp\n";
                }
            }
            echo "Processing of XML Files are done.\n";
        } else {
            echo "No xml file to process.\n";
        }
    } else {
        echo "Unable to connect with the FTP server.\n";
    }
    
    ftp_close($ftp_conn);
    
    if ($epgVersion) {
    
        $epgUpdateQuery = "UPDATE version set EPG_VERSION=EPG_VERSION+1";
        $epgUpdateQueryStatus = mysql_query($epgUpdateQuery, $link);
        if ($epgUpdateQueryStatus) {
            echo "EPG version updated successfully.\n";
        } else {
            echo "EPG version update query failed.\n";
        }
    }
    
    echo "End Time - " . date('Y-m-d H:i:s') . "\n";
    
    function parseXmlFile($filename, $queryType) {
        $link = $GLOBALS['link'];
    
        $file_get = file_get_contents($filename);
        if ($file_get) {
            $xml = simplexml_load_string($file_get);
            $chan_category = mysql_escape_string(trim($xml->channel->category));
            $channel_name = mysql_escape_string(trim($xml->channel->{'display-name'}));
            $service_id = mysql_escape_string(trim($xml->channel->serviceid));
            $channel_logo = mysql_escape_string(trim($xml->channel->ChannelLogo));
            $programme = $xml->programme;
    
            $programme_date = trim($xml->programme->attributes()->start);  // delete data above current date
            $expld_date = explode(" ", $programme_date);
            $s_curdate = trim($expld_date[0]);
            $start_curtime_date = date("Y-m-d", strtotime($s_curdate));
    
            if ($queryType == 1) {
                $deleteType = ">=";
            } else if ($queryType == 2) {
                $deleteType = "=";
            }
    
            $sql_del = "DELETE FROM `Program_Details` WHERE Service_Id = '$service_id' and date(`Start_Time`) $deleteType '$start_curtime_date'";
            mysql_query($sql_del, $link);
            //echo $sql_del . PHP_EOL;
    
            $i = 0;
            $insert_programs_sql = array();
            foreach ($programme as $prog) {
                $cast = $prog->cast;
                $actors = '';
                $directors = '';
                $producers = '';
                if ($cast) {
                    foreach ($cast as $c) {
                        $actor_arr = array();
                        foreach ($c->actor as $t) {
                            $actor_arr[] = (string) mysql_escape_string(trim($t));
                        }
                        $actors = implode(',', $actor_arr);
                        $directors = mysql_escape_string(trim($c->director));
                        $producers = mysql_escape_string(trim($c->producer));
                    }
                }
                $start = (string) trim($prog->attributes()->start);
                $stop = (string) trim($prog->attributes()->stop);
    
                $expld_sdate = explode(" ", $start);
                $s_date = trim($expld_sdate[0]);
                $start_time = date("Y-m-d H:i:s", strtotime($s_date));
    
    
                $expld_edate = explode(" ", $stop);
                $e_date = trim($expld_edate[0]);
                $end_time = date("Y-m-d H:i:s", strtotime($e_date));
    
                // Create two new DateTime-objects...
                $date1 = new DateTime($start);
                $date2 = new DateTime($stop);
    
                // The diff-methods returns a new DateInterval-object...
                $diff = $date2->diff($date1);
    
                // Call the format method on the DateInterval-object
                $hour = $diff->format('%h');
                $min = $diff->format('%i');
                $duration = ($hour * 60) + $min;
    
                $prod_id = mysql_escape_string(trim($prog->programmeid));
                $title = mysql_escape_string(trim($prog->title));
                $desc = mysql_escape_string(trim($prog->desc));
                $category = mysql_escape_string(trim($prog->category));
                $subcat = mysql_escape_string(trim($prog->{'sub-category'}));
                $VideoUrl = mysql_escape_string(trim($prog->ProgrammeVideoUrl));
                $ImageUrl = mysql_escape_string(trim($prog->ImageUrl));
                $insert_programs_sql[] = "('$service_id','$channel_name','$prod_id','$title','$desc','$category','$subcat','$ImageUrl','$VideoUrl','$start_time','$end_time','$chan_category','$channel_logo','$actors','$directors','$producers','$duration')";
                $i++;
                if ($i === 2000) {
                    $sql_prog = "INSERT INTO `Program_Details`(`Service_Id`,`Service_Name`, `Program_Id`, `Title`, `Description`, `Category`, `sub_categories`, `image_urls`, `video_urls`, `Start_Time`,`end_time`,`channel_categories`,`channel_logos`,actors,directors,producers,Duration) VALUES " . implode(',', $insert_programs_sql);
    
                    $epg_programs_insert_query_status = mysql_query($sql_prog, $link);
                    if (!$epg_programs_insert_query_status) {
                        die(mysql_errno($link));
                    }
                    $i = 0;
                    $insert_programs_sql = array();
                }
            }
    
            if (count($insert_programs_sql) > 0) {
                $sql_prog = "INSERT INTO `Program_Details`(`Service_Id`,`Service_Name`, `Program_Id`, `Title`, `Description`, `Category`, `sub_categories`, `image_urls`, `video_urls`, `Start_Time`,`end_time`,`channel_categories`,`channel_logos`,actors,directors,producers,Duration) VALUES " . implode(',', $insert_programs_sql);
    
                $epg_programs_insert_query_status = mysql_query($sql_prog, $link);
                if (!$epg_programs_insert_query_status) {
                    die(mysql_errno($link));
                }
            }
    
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    function postToAppinventiv($file) {
        $url = "http://172.31.xx.xxx/admin/epgzip";
        //$file_name_with_full_path = realpath("./$file");
        //$post = array('epg_zip' => new CurlFile($file));
        $post = array('epg_zip' => '@' . $file);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'abvjjlk: xxxxxxxxxxx',
            'authorization: xxxxxxxxxxx'
        ));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($ch);
        //curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if ($info['http_code'] == 200) {
            logging("INFO", $file . ":" . $result);
            return $result;
        } else {
            logging("ERROR", $result);
            return FALSE;
        }
    }
    
    function logging($type, $message) {
        $logFileName = '/home/customscript/epg/logs/' . date('Y-m-d') . ".log";
    
        $message = date('Y-m-d H:i:s') . '|' . $type . '|' . $message . PHP_EOL;
        error_log($message, 3, $logFileName);
    }