0

我需要创建一个登录到远程站点的 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);

?>

4

1 回答 1

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);
?>
于 2012-10-10T05:29:19.950 回答