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