I'm trying to use a post form to generate an excel file after choosing which data to grab from a MySQL database. I'm using cURL because I essentially want to double-post: first to save any settings of which fields are being used, and second to generate the Excel file, all with one button.
I have a PHP page set up with the correct headers to generate a simple Excel file from tab-separated content (not dealing w/ line endings or tabs in the fields, just keeping it simple). If I hit that page directly with the browser (and include dummy data in the PHP), my browser downloads an Excel file.
I'm getting the data through to this page via cURL post (if I don't send headers via cURL--??).
And here's about where I get lost:
How do I create/download the data as an Excel file via cURL?
My guess is it's something to do w/ the headers?
Setting headers in PHP worked fairly well:
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Cache-control: max-age=0");
When I set them via cURL instead, I did this (well, these are all of my cURL options):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $foo);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Disposition: attachment; filename=test.xls',
'Content-Type: application/vnd.ms-excel; charset=UTF-8',
'Pragma: public',
'Cache-control: max-age=0'
));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response_code = curl_exec($ch);
if ($response_code!== false) {
echo 'response: '.$response_code;
} else {
echo 'error: '.curl_error($ch);
}
curl_close ($ch);
When I do this, the cURL $response_code I get on the sending page looks like this (I've broken the lines up - it's all one line):
HTTP/1.1 200 OK
Date: Sun, 31 Jan 2010 20:43:38 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: text/html
When I don't include the headers via cURL, $_POST is working. But when I do include the headers, $_POST does not work.
That Transfer-Encoding: chunked
seems like a clue. But initial research didn't help me, partly because, as I mentioned, I'm lost.
Any ideas?
Thank you!
-Jeremy