开发者

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

开发者 https://www.devze.com 2022-12-18 11:13 出处:网络
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

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'
));
cur开发者_JAVA技巧l_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


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?


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)?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号