PHP登录远程站点并下载csv

时间:2012-10-10 05:19:22

标签: php

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

&GT;

1 个答案:

答案 0 :(得分: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);
?>