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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证