从安全的POST请求通过curl下载CSV文件的内容

时间:2014-05-09 10:03:09

标签: php curl

我试图下载在HTTPS页面上按下POST按钮时生成的文件。我必须传递Cookie的内容才能证明我已登录,但我已经在同一网站的其他地方对其进行了测试,并且一切正常。

我已成功生成POST请求以请求我需要的数据,并且我已发送该数据,但我从网络服务器上得不到任何回复。

这是我的代码:

// Build header
$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
$header[] = "Content-Type: application/x-www-form-urlencoded";

// Now do transfer
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://domain/pathtoscript");

// Send referer to make this look real
curl_setopt($ch, CURLOPT_REFERER, $referer);

// And disguise ourselves as a browser
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

// Return results as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

// Now send post data
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

// Define where we can store cookies
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);

// Sender header and permitted encodings
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, '');

// Actually do it !
$result = curl_exec ($ch);

curl_close ($ch);

// Show result in browser (should be CSV text file)
echo $result;

我做错了什么?我不希望打开一个对话框提示我保存文件,我只是喜欢$result中文件的内容,以便我以后可以在我的应用程序中解析它。 / p>

提前致谢!

0 个答案:

没有答案