0

I'm trying to download a file that is generated when a POST button is pressed on an HTTPS page. I'm having to pass the contents of a cookie to show that I'm logged in but I've tested that elsewhere on the same site and that's all working fine.

I'm successfully generating the POST request to request the data that I need and I'm sending it but I'm getting nothing back from the web server at all.

Here's my code:

// 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;

What am I doing wrong ? I don't want a dialogue box to open to prompt me to save the file, I'd just like the content of the file in $result so that I can parse it later in my application.

Thanks in advance !

Philip Lee
  • 157
  • 3
  • 16
  • you are not returning anything... where's the download code? (header, echo the result...) – Sal00m May 09 '14 at 10:06
  • can you show the code where you make the POST request to. – Latheesan May 09 '14 at 10:06
  • You may be able to get some help from [this answer](http://stackoverflow.com/a/22264028/391161) that I wrote a while back. – merlin2011 May 09 '14 at 10:15
  • All POST parameters are put into the $postData variable - that part works. What download code would I have to add to get the response into the $result variable ? – Philip Lee May 09 '14 at 10:15
  • That's a useful example @merlin2011 - thanks. I've made the changes that I thought I'd need (edited above too) but without success :-( – Philip Lee May 09 '14 at 10:43

0 Answers0