0
我需要从这个网站下载一个压缩的.csv文件。 http://www.phrfsocal.org/web-lookup-2/该文件是右侧表格上方的链接下载数据。 gotcha是动态创建的链接。所以我需要先提取它。PHP curl从caspio驱动的网站下载一个压缩的CSV
当我粘贴链接到一个新的浏览器选项卡,浏览器下载包含我感兴趣的CSV zip文件。
然而,当使用curl尝试获得拉链,它代替获取链接下方表格的html。似乎无法弄清楚如何抓住.zip。 以下是我的代码,第一部分找到链接,似乎正在工作。
第二部分是我遇到麻烦的地方。
PS我有此页面的拥有者的许可每晚使用Cron作业下载此数据。 在此先感谢, 戴夫
$url = "http://www.phrfsocal.org/web-lookup-2/";
// url to the dynamic content doesn't seem to change.
$url = "https://b6.caspio.com/dp.asp?AppKey=0dc330000cbc1d03fd244fea82b4";
$header = get_web_page($url);
// Find the location of the Download Data link and extract the href
$strpos = strpos($header['content'], 'Download Data');
$link = substr($header['content'], $strpos, 300);
$link = explode(" ", $link);
$link = explode('"', $link[2]);
$url1 = $link[1];
print_r($url1);
print "<p>";
// Now Go get the zip file.
$zipFile = "temp/SoCalzipfile.zip"; // Local Zip File Path
$zipResource = fopen($zipFile, "w+");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if (!$page) {
echo "Error :- " . curl_error($ch);
}
curl_close($ch);
echo "zip file recieved";
/* Open the Zip file */
$zip = new ZipArchive;
$extractPath = "temp";
if ($zip->open($zipFile) != "true") {
echo "Error :- Unable to open the Zip File";
}emphasized text
/* Extract Zip File */
$zip->extractTo($extractPath);
$zip->close();
Chris,This works great。实际上,任何页面上的压缩.csv都包含所有数据。非常感谢。 – davewhirlwind
没问题,很高兴我可以帮忙:) – Chris