我的XML
系统上有一个压缩的PHP
文件。
如何解压缩以将其加载到XMLDOM
?
每个XML
存档只有一个zip
个文件。
提前致谢。
答案 0 :(得分:2)
您可以使用php类ZipArchive和DOMDocument,即:
<?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';
}
注意:
答案 1 :(得分:0)
<?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);
}