PHP-从url下载xlsx并保存在服务器上

PHP-从url下载xlsx并保存在服务器上,php,download,request,xlsx,Php,Download,Request,Xlsx,请您帮助我在使用PHP时在服务器上保存xlsxJapan统计文件。我正在使用下面的函数,它适用于xls文件。 我尝试了多种方法: -用0字节或12字节的HTML标记而不是excel数据保存文件内容 -卷曲0字节 -下面的请求代码根本不保存文件 我想下载并保存此文件: 我非常感谢你的帮助 谢谢,, 菲利普 如果您有一个简单的curl函数,可以重复使用,那么下载这个文件就相当容易了。您需要下载一份cacert.pem——保存到您的Web服务器,并编辑下面给出的路径以适应需要 function cu

请您帮助我在使用PHP时在服务器上保存
xlsx
Japan统计文件。我正在使用下面的函数,它适用于
xls
文件。 我尝试了多种方法: -用0字节或12字节的HTML标记而不是excel数据保存文件内容 -卷曲0字节 -下面的请求代码根本不保存文件

我想下载并保存此文件:

我非常感谢你的帮助

谢谢,, 菲利普


如果您有一个简单的curl函数,可以重复使用,那么下载这个文件就相当容易了。您需要下载一份
cacert.pem
——保存到您的Web服务器,并编辑下面给出的路径以适应需要

function curl( $url, $options=array(), $headers=array() ){

    # EDIT this to suit...
    $cacert='c:/wwwroot/cacert.pem';

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

    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 )
    );
    curl_close( $curl );
    return $res;
}




/* Create the name and path for the saved file */
$saveto='c:/temp/1007ci2.xlsx';

/* begin writing to a stream */
$fp=fopen( $saveto, 'w' );

/* fetch this url */
$url='https://www.esri.cao.go.jp/jp/stat/di/1007ci2.xlsx';

/* indicate we wish to download a file and save it to the stream already created */
$options=array(
    CURLOPT_FILE    =>  $fp
);

/* do the download */
$res=curl( $url, $options );

/* all ok? */
if( $res->info->http_code==200 ){
    echo "OK";
}

/* close the file pointer */
fclose( $fp );

谢谢RamRaider。它下载了这个文件,但是里面有html标记,而不是excel数据——当我运行它时,这个文件是12KB而不是not。这下载了完美的文件,它现在在Excel中打开供我查看。。。不支持TLS1.2或在此下载的文件中禁用设置中的TLS1.2。然后,您应该痛击您的管理员以更新PHP环境。此版本的终止日期为2015年9月14日!强制上述功能使用特定版本的TLS支持(特别是
CURLOPT\u SSLVERSION=>CURL\u SSLVERSION\u TLSv1\u 0
)会产生
“内阁办公室网站自2018年11月29日以来一直加密(TLS1.2),URL已更改为“https”,如下所示。”
~因此,除了升级之外,您似乎没有什么办法可以避免这个问题。
function curl( $url, $options=array(), $headers=array() ){

    # EDIT this to suit...
    $cacert='c:/wwwroot/cacert.pem';

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

    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 )
    );
    curl_close( $curl );
    return $res;
}




/* Create the name and path for the saved file */
$saveto='c:/temp/1007ci2.xlsx';

/* begin writing to a stream */
$fp=fopen( $saveto, 'w' );

/* fetch this url */
$url='https://www.esri.cao.go.jp/jp/stat/di/1007ci2.xlsx';

/* indicate we wish to download a file and save it to the stream already created */
$options=array(
    CURLOPT_FILE    =>  $fp
);

/* do the download */
$res=curl( $url, $options );

/* all ok? */
if( $res->info->http_code==200 ){
    echo "OK";
}

/* close the file pointer */
fclose( $fp );