响应cURL请求时应使用什么内容配置?

时间:2019-05-20 10:17:08

标签: csv http-headers content-disposition

假设我们有一个客户端(实际上是服务器端的PHP脚本)和一个服务器(也是服务器端的PHP脚本)。

客户端通过cURL向服务器发出HTTP请求。客户端接受text/csv,因此设置了相应的标头,并且客户端希望将响应保存到文件中,因此正确设置了CURLOPT_FILE选项。

问题是服务器在处理请求并发回CSV“编码”内容时是否应使用inlineattachment作为Content-Disposition标头的值?

用于测试的非常简单的伪代码:服务器执行以下操作:     

if (file_exists($attachment_location)) {
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Cache-Control: public"); // needed for internet explorer
    header("Content-Type: text/csv");
    header("Content-Length:".filesize($attachment_location));
    header("Content-Disposition: inline");
    readfile($attachment_location);
    die();
} else {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}

还是应该像这样:

<?php
$attachment_location = "./c3m.csv";

if (file_exists($attachment_location)) {
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Cache-Control: public"); // needed for internet explorer
    header("Content-Type: text/csv");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Length:".filesize($attachment_location));
    header("Content-Disposition: attachment; filename=c3m.csv");
    readfile($attachment_location);
    die();
} else {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}

1 个答案:

答案 0 :(得分:2)

cURL并不关心Content-Disposition,因此它与您赋予它的价值无关。