0

我有一个脚本,通常从带有类的外部服务器下载 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?

4

1 回答 1

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';
    }
?>
于 2013-12-30T14:45:53.190 回答