Php 使用CURL从URL下载文件

Php 使用CURL从URL下载文件,php,curl,download,gzip,Php,Curl,Download,Gzip,我正在尝试使用PHP和CURL下载一个文件。如果我在浏览器中打开链接,我会得到一个xlsx文件,但在PHP中,当我保存文件时,它无法打开。 我发现,如果我使用PHP保存url的内容,该文件是一个gzip文件,如果我将其保存为zip文件,我可以打开它,它就可以了。 问题是我希望服务器上的解压缩文件可以使用,但我无法解压缩zip文件,因为zip archive说它不是正确的zip文件。 这是我正在使用的代码: $fp = fopen ('file.zip', 'w+'); // Here is t

我正在尝试使用PHP和CURL下载一个文件。如果我在浏览器中打开链接,我会得到一个xlsx文件,但在PHP中,当我保存文件时,它无法打开。 我发现,如果我使用PHP保存url的内容,该文件是一个
gzip
文件,如果我将其保存为
zip
文件,我可以打开它,它就可以了。 问题是我希望服务器上的解压缩文件可以使用,但我无法解压缩zip文件,因为zip archive说它不是正确的zip文件。 这是我正在使用的代码:

$fp = fopen ('file.zip', 'w+');

// Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20","http:members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0"));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);

//  write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//   get curl response
$ex = curl_exec($ch);
var_dump($ex); 
curl_close($ch);
fclose($fp);

$file = "images";
$zip = new ZipArchive;
$path = realpath($file);
$res = $zip->open("file.zip");

if ($res === TRUE) {
    $extract = $zip->extractTo($path);
    var_dump($extract);
    if ($extract){
    $zip->close();
    echo "WOOT! $file extracted to $path";
}else{
    echo $zip->getStatusString(); 
    echo 'not extracte';
}

} else {
    echo $zip->getStatusString(); 
    echo "Doh! I couldn't open $file";
}
所以我的问题是。如何将该URL中的excel文件保存在主机中

我试过很多东西,但都不管用

谢谢


这很好地保存了
xlsx
文件,然后可以在Excel中打开该文件。使用此代码保存的文件大小为
145Kb
,而不是原始代码的
141Kb
您应该提到gzip编码(作为CURLOPT_编码),然后您不需要提取文件(下载的文件可以直接打开):


你有什么错误吗?我发现文件保存得很好,打开得很好,不需要解压等-不使用上述代码,尽管
curl\u setopt($ch,CURLOPT\u HTTPHEADER,[“接受编码:标识])-读这个
function curl( $url=NULL, $options=NULL, $headers=false ){
    $cacert='c:/wwwroot/cacert.pem';    #EDIT THIS TO SUIT
    $vbh = fopen('php://temp', 'w+');

    session_write_close();

    $curl=curl_init();
    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
    }
    curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
    curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $curl, CURLOPT_FAILONERROR, true );
    curl_setopt( $curl, CURLOPT_HEADER, false );
    curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
    curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
    curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0' );
    curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
    curl_setopt( $curl, CURLOPT_ENCODING, '' );
    curl_setopt( $curl, CURLOPT_VERBOSE, true );
    curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
    curl_setopt( $curl, CURLOPT_STDERR, $vbh );

    if( isset( $options ) && is_array( $options ) ){
        foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
    }
    if( $headers && is_array( $headers ) ){
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
    }
    $res=(object)array(
        'response'  =>  curl_exec( $curl ),
        'info'      =>  (object)curl_getinfo( $curl ),
        'errors'    =>  curl_error( $curl )
    );
    rewind( $vbh );
    $res->verbose=stream_get_contents( $vbh );
    fclose( $vbh );
    curl_close( $curl );

    return $res;
}




/* Make the curl request and save the file */
$url='http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0';

$saveto='c:/temp/downloaded_excel_file.xlsx'; #EDIT TO SUIT

$fp=fopen( $saveto, 'w' );
$options=array( CURLOPT_FILE => $fp );
$res=curl( $url, $options );
fclose( $fp );

if( $res->info->http_code==200 ){
    echo "OK";
}
$url = 'http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

file_put_contents('test.xlsx', $result);