php
2012-10-10 85 views 0 likes 
0

我需要創建一個登錄到遠程站點的php腳本,然後在該站點上下載一個csv文件。我搜遍了所有,並沒有找到任何我需要的東西。目前我只有以下代碼可以讓我登錄到遠程站點。PHP登錄到遠程站點並下載csv

<?php 
$username=""; 
$password=""; 
$url=''; 
$cookie=""; 
$postdata = 'email='.$username.'&pass='.$password; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;  rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt($ch, CURLOPT_REFERER, $url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt($ch, CURLOPT_POST, true); 
$result = curl_exec($ch); 
echo $result; 
curl_close($ch); 

>

+0

你嘗試過使用EXEC()命令? – artsylar

+0

取決於您需要如何確定要下載哪個csv文件。你事先知道URL嗎?或者每次登錄時都會更改它? – Stobor

+0

@Stobor我知道網址之前它永不改變。 – dewbop12

回答

0

如何是這樣的:?

<?php 
$username=""; 
$password=""; 
$url=''; 
$csvurl=''; 
$cookie=""; 
$postdata = 'email='.$username.'&pass='.$password; 

/*first log in*/ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;  rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt($ch, CURLOPT_REFERER, $url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt($ch, CURLOPT_POST, true); 
$result = curl_exec($ch); 
echo $result; 

/*now download the file*/ 
curl_setopt($ch, CURLOPT_URL, $csvurl); 
curl_setopt($ch, CURLOPT_REFERER, $url); 
$result = curl_exec($ch); 
echo $result; 


curl_close($ch); 
?> 
+0

謝謝@Stobor ..它似乎並沒有工作,雖然..會是因爲登錄後發生csv鏈接不位於着陸頁上。它是否需要重定向到csv鏈接所在的頁面? – dewbop12

+0

取決於該網站在登錄和csv下載之間做些什麼。請注意,我的代碼已經假定登錄url和csv url是不同的。任何更多的細節都需要關於網站的更多信息,或者什麼不工作。 – Stobor

相關問題