php - PHP读取在线Excel文件而不下载文件
问题描述
我正在创建一个网络股票应用程序。我想在此链接中阅读此 Excel 或 Google 表格文件:
无需先下载文件。
PHP是否可以读取并选择表格然后在线读取整个行和列?
我应该使用什么库?
我尝试使用 phpspreadsheet 但我无法做到。不知道我是否做错了。
我也尝试使用 Google Sheet API。但在链接中我找不到谷歌电子表格 ID。
那么使用 php 在线阅读 excel 或 google 电子表格的解决方案是什么?
更新 :
@ADyson 建议使用相同的标头通过 php 获取数据下载。
所以这是我的代码:
function dfCurl($url){
$ch = curl_init($url);
$dir = '../';
$fileName = basename($url);
$saveFilePath = $dir . $fileName;
$fp = fopen($saveFilePath, 'wb');
$headers = [
':authority: www.idx.co.id',
':method: GET',
':path: /Portals/0/StaticData/ListedCompanies/Corporate_Actions/New_Info_JSX/Jenis_Informasi/01_Laporan_Keuangan/02_Soft_Copy_Laporan_Keuangan//Laporan%20Keuangan%20Tahun%202021/TW1/AALI/FinancialStatement-2021-I-AALI.xlsx',
':scheme: https',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding: gzip, deflate, br',
'accept-language: en-AU,en;q=0.9,id-ID;q=0.8,id;q=0.7,zh-CN;q=0.6,zh;q=0.5,ja-JP;q=0.4,ja;q=0.3,en-GB;q=0.2,en-US;q=0.1',
'cache-control: no-cache',
'cookie: _ga=GA1.3.1396311216.1625813381; skipFeedback=1; __cf_bm=mF3lXNPMKeLe1xFzY5eKQM1TFN.yz7lxgWMJEYWBcjA-1635412963-0-AUMv9HWMpGhIN6c7eDVT++ok0dYE1NL+PyRJBpNUfZviT8bNS5Zm4UQjAm7gUJad4Qv7h+Q9ak+u/Q18tGDx0pU=',
'pragma: no-cache',
'referer: https://www.idx.co.id/perusahaan-tercatat/laporan-keuangan-dan-tahunan/',
'sec-ch-ua: "Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
'sec-ch-ua-mobile: ?1',
'sec-ch-ua-platform: "Android"',
'sec-fetch-dest: document',
'sec-fetch-mode: navigate',
'sec-fetch-site: same-origin',
'sec-fetch-user: ?1',
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Mobile Safari/537.36'
];
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
我打电话给使用:
public function Export() {
// Initialize a file URL to the variable
$url = 'https://www.idx.co.id/Portals/0/StaticData/ListedCompanies/Corporate_Actions/New_Info_JSX/Jenis_Informasi/01_Laporan_Keuangan/02_Soft_Copy_Laporan_Keuangan//Laporan%20Keuangan%20Tahun%202021/TW1/AALI/FinancialStatement-2021-I-AALI.xlsx';
dfCurl($url);
}
但不幸的是,我仍然无法正确下载 xlsx 文件。我刚得到一个 1 kb 的文件,当打开文件时,我从 excel 中得到错误。
那么使用 php 从 url 下载的正确方法是什么?
链接是:
谢谢你
解决方案
最后,我可以从该站点使用 php 下载。
我想做的就是使用 curl :
function dfCurl($url){
$ch = curl_init($url);
$dir = '../';
$fileName = basename($url);
$saveFilePath = $dir . $fileName;
$fp = fopen($saveFilePath, 'wb');
$headers = [
'authority: www.idx.co.id',
'method: GET',
'scheme: https',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'cache-control: no-cache',
'pragma: no-cache',
'user-agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Mobile Safari/537.36',
'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
];
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
就这样。