我有一个脚本,通常从带有类的外部服务器下载xml。 现在我用相同的代码下载一个zip文件,解压缩它,这是要解析的xml。 我最初的问题是如何检索zip文件解压缩并获取xml?
这是我的实际代码:
$ch2=curl_init();
curl_setopt($ch2, CURLOPT_URL, $this->URL);
curl_setopt($ch2, CURLOPT_TIMEOUT, 540);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_SSLVERSION, 3);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
$httpHeader2 = array(
"Content-Type: text/xml; charset=UTF-8",
"Content-Encoding: UTF-8"
);
$zip = curl_exec($ch2);
$this->errno=curl_getinfo( $ch2, CURLINFO_HTTP_CODE );
curl_close($ch2);
如何获取zip文件并将其解压缩以检索内部的xml?
答案 0 :(得分:0)
<?php
//Add this to end of your code
file_put_content('./temp.zip' ,$zip );
$ZipArchive = new ZipArchive;
if ($ZipArchive->open('./temp.zip' ) === TRUE) {
$ZipArchive->extractTo('/my/destination/dir/');
$ZipArchive->close();
echo 'ok';
} else {
echo 'failed';
}
?>