May you help me with saving xlsx
Japan statistic file on the server while using PHP. I am using the function below and it works for xls
files. I tried multiple ways: -file_put_content saves with 0 bytes or 12 bytes with HTML tags instead of excel data -curl 0 bytes -request code down below doesn't save files at all.
I want to download and save this file: https://www.esri.cao.go.jp/jp/stat/di/1007ci2.xlsx
I would really appreciate your help.
Thank you, Filip
function Request($url,$File="",$Method='POST',$data=null, $optional_headers = null,$Debug=0)
{
$params = array('http' => array('method' => $Method));
$optional_headers.="Accept:application/pdf;q=0.9,*/*;q=0.8\r\n";
$optional_headers.="Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n";
$optional_headers.="Accept-Encoding:gzip,deflate,sdch,br\r\n";
$optional_headers.="Accept-Language:en-US,en;q=0.8\r\n";
$optional_headers.="Cache-Control:max-age=0\r\n";
$optional_headers.="Connection:keep-alive\r\n";
$optional_headers.="User-Agent:Mozilla/5.0 AppleWebKit/536.5 (KHTML, like Gecko) SepidarBrowser/1.0.100.52 Safari/536.5\r\n";
if ($data !== null) {
$params['http']['content'] = $data;
}
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
return false;
}
$response= @stream_get_meta_data($fp);
$out['header'] = $response['wrapper_data'];
$out['body']='';
if($File!=""){
$fout = @fopen($File, 'w+');
}
while(!@feof($fp)){
$buffering=@fread($fp,1024*8);
// echo "***************\n".strlen($buffering)."\n".$buffering."\n***********************";
if($buffering==''){break;}
if($File!=""){
@fwrite($fout,$buffering);
if($Debug==1)echo strlen($buffering)."-->Download And Stored IN".$File;
}
$out['body'] .=$buffering;
}
if(trim(@$out['header']['Content-Encoding'])=='deflate'){
$out['body']=gzinflate($out['body']);
}
fclose($fp);
return $out;
}
$url = 'https://www.esri.cao.go.jp/jp/stat/di/1007ci2.xlsx';
Request($url,dirname(__FILE__).'/Japan.xls','GET');
If you have a simple curl function that you can re-use then the download of this file is quite easy. You will need to download a copy of cacert.pem
- you can find this here - save to your webserver and edit the path given below to suit.
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 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.