使用PHP从间接链接下载XLSX文件
我希望能够使用PHP将以下文件下载到我的服务器: 问题是它不是文件的直接链接,为了解决这个问题,我尝试使用curl,但没有成功。有什么建议吗?我已尝试使用以下curl片段:使用PHP从间接链接下载XLSX文件,php,curl,Php,Curl,我希望能够使用PHP将以下文件下载到我的服务器: 问题是它不是文件的直接链接,为了解决这个问题,我尝试使用curl,但没有成功。有什么建议吗?我已尝试使用以下curl片段: 但是,输出是一个空的XLSX文件。引用的示例处理常规的非ssl端点,并省略了CURLOPT\u FOLLOWLOCATION选项以允许curl跟随重定向。下载该文件时,以下操作应该可以-您需要下载cacert.pem的副本,并在显示的位置编辑curl函数 <?php $url='https://www.a
但是,输出是一个空的XLSX文件。引用的示例处理常规的非ssl端点,并省略了
CURLOPT\u FOLLOWLOCATION
选项以允许curl跟随重定向。下载该文件时,以下操作应该可以-您需要下载cacert.pem
的副本,并在显示的位置编辑curl函数
<?php
$url='https://www.arcgis.com/sharing/rest/content/items/b5e7488e117749c19881cce45db13f7e/data';
$target=__DIR__ . '/covid-19.xlsx';
$hndl=fopen( $target, 'w' );
$options=array(
CURLOPT_FILE => $hndl
);
$res=curl( $url, $options );
if( $res->info->http_code==200 && file_exists( $target ) )echo 'ok';
fclose( $hndl );
function curl( $url=NULL, $options=NULL ){
/*
Edit this to suit your environment.
Download a copy from https://curl.haxx.se/docs/caextract.html
*/
$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 the Gorilla' );
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 );
}
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
curl_close( $curl );
return $res;
}
?>
请显示您的代码。