2015-10-23 86 views
0

我想要寫一個PHP腳本:如何使用php登錄網頁,瀏覽並下載csv文件?

A.登錄到網站 B.導航到特定頁面(這是一個CSV文件下載) C.存儲在一個字符串的結果。

喜歡的東西:

$site->connect("dashboard.site.com"); 
$site->post_vars(array("username"=>"mike","password"=>"2345"); 
$csv_file = $site->download("dashboard.site.com/file.csv"); 

我讀過有關史努比類,但文件似乎不存在的,所以我不能肯定,如果這個事情可以做。

+0

你試過了什麼? – Axalix

+0

還沒有。明天要嘗試窺探第一件事。 – danjfoley

+0

嘗試「捲曲」,看到這裏的一些例子:http://stackoverflow.com/questions/3008817/login-to-remote-site-with-php-curl – Axalix

回答

0

我想出瞭如何使用Curl來完成它。我自己的登錄名和密碼以及商家組當然都隱藏在這段代碼中。但是,如何做到這一點:我希望這可以幫助任何處於相同情況的人。

$cookie_jar = tempnam('/tmp','cookie'); 

// log in 
$c = curl_init('https://dashboard.powerreviews.com/dashboard/login'); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar); 
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false); 
$page = curl_exec($c); 
curl_close($c); 

$post_fields = array("j_username" => "USERNAME", "j_password" => "PASSWORD", "command"=>"Login"); 
$post_data = http_build_query($post_fields); 

$c = curl_init('https://dashboard.powerreviews.com/process_login'); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar); 
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar); 
curl_setopt($c, CURLOPT_POST, true); 
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($c, CURLOPT_POSTFIELDS, $post_data); 
$page = curl_exec($c); 
curl_close($c); 

// switch to Reviews Account 
$c = curl_init('https://dashboard.powerreviews.com/programs/epc?accessMerchantGroup=xxxxx'); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar); 
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar); 
$csv_file = curl_exec($c); 
curl_close($c); 


// download RA unsubscribes 
$c = curl_init('https://dashboard.powerreviews.com/dashboard/unsubscribes.csv'); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar); 
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar); 
$csv_file = curl_exec($c); 
curl_close($c); 
echo $csv_file; 
相關問題