使用 CURL 从 URL 下载文件

Download file using CURL from URL

我正在尝试使用 PHP 和 CURL 下载文件。如果我在浏览器中打开 link,我会得到一个 xlsx 文件,但是当我使用 PHP 保存文件时,它无法打开。 我发现如果我使用 PHP 来保存 url 的内容,该文件是一个 gzip 文件,如果我将它保存为 zip 文件,我可以打开它并且没关系。 问题是我希望服务器上的提取文件可以使用,但我无法提取 zip 文件,因为 zip 存档说它不是正确的 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 文件?

我试了很多东西都没有用。

谢谢

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";
}

这很高兴地保存了 xlsx 文件,然后可以在 Excel 中打开该文件。用这个保存的文件大小是145Kb而不是141Kb,原代码是

你应该提到gzip编码(如CURLOPT_ENCODING),那么你不需要解压文件(下载的文件可以直接打开):

$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);