2017-04-20 40 views
0

我需要從這個網站下載一個壓縮的.csv文件。 http://www.phrfsocal.org/web-lookup-2/該文件是右側表格上方的鏈接下載數據。 gotcha是動態創建的鏈接。所以我需要先提取它。PHP curl從caspio驅動的網站下載一個壓縮的CSV

這部分似乎工作正常。我得到這個鏈接的href。 https://b6.caspio.com/dp.asp?appSession=68982476236455965042483715808486764445346819370685922723164994812296661481433499615115137717633929851735433386281180144919150987&RecordID=&PageID=2&PrevPageID=&cpipage=&download=1

當我粘貼鏈接到一個新的瀏覽器選項卡,瀏覽器下載包含我感興趣的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(); 

回答

1

下面的代碼會下載的zip文件並將其解壓到指定的文件夾中。確保該文件夾是可寫的。所以在這個例子中確保臨時文件夾具有寫權限。

您也不需要獲取頁面的html版本來提取鏈接。我有一個玩弄網址,你可以通過使用cpipage變量獲得每個頁面的zip文件。您可以更改$page_num變量以從指定頁面獲取壓縮文件。

$page_num = 1; 

$url = 'https://b6.caspio.com/dp.asp?AppKey=0dc330000cbc1d03fd244fea82b4&RecordID=&PageID=2&PrevPageID=&cpipage=' .$page_num. '&download=1'; 

$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, $url); 
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); 


$zip = new ZipArchive; 
$extractPath = "temp"; 
if($zip->open($zipFile) != "true"){ 
echo "Error :- Unable to open the Zip File"; 
} 
/* Extract Zip File */ 
$zip->extractTo($extractPath); 
$zip->close(); 
+0

Chris,This works great。實際上,任何頁面上的壓縮.csv都包含所有數據。非常感謝。 – davewhirlwind

+0

沒問題,很高興我可以幫忙:) – Chris