我正在尝试直接通过PHP下载和解析谷歌电子表格。 我是否需要以不同的方式从谷歌文档下载导出的CSV文件?
$csv = file_get_contents('https://docs.google.com/spreadsheets/d/KEY/export?format=csv');
print_r($csv);
我也用文件试过了:
$csv = file('https://docs.google.com/spreadsheets/d/KEY/export?format=csv');
并使用cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://docs.google.com/spreadsheets/d/KEY/export?format=csv");
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$csv = curl_exec($ch);
curl_close($ch);
var_dump($csv);
当我从浏览器调用URL时,我得到一个很小的CSV文件。通过这种方式,我获得了谷歌文档网站的来源。