dpkt31779 2010-01-31 20:48
浏览 65
已采纳

为什么我不能通过cURL / PHP生成Excel文件?

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

  • 写回答

2条回答 默认 最新

  • dtuzjzs3853 2010-01-31 20:54
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图