简体   繁体   中英

Why can't I generate Excel file through cURL/PHP?

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

Well i beleive CURLOPT_HTTPHEADER sets the headers for the request... not the response. As far as $_POST goes what do you mean it doesnt work? Did you set the CURLOPT for a post request (the default is get)?

I think you are fundamentally mistaken about the way content is delivered. The content-type headers just point out what format to expect, but you still have to actually generate data in that format.

Looking at the Excel file format, that is not a trivial task. There is a PHP based class that uses COM to write an Excel file (and works only on a Windows server): here I haven't used it myself though.

Then there's PHPExcel that makes use of the new, XML-Based Excel format and works platform independently. I don't know this either, but it looks good.

Remember, there's always the possibility of delivering plain old CSV, and opening that in Excel. It leaves with you with a lot less options (no column design etc.) but would work out-of-the-box without any libraries or extensions.

Also, I'm unsure what you want to use cURL for in this scenario? You are not trying to download anything on the server side, if I understand it correctly, are you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM