0

我正在尝试使用接下来的 cURL 调用 Rest web 服务:

  1. 生成证书
  2. 下载此证书

这些功能中的每一个都是单独工作的,但是当将它们收集到一个服务中时,下载对话框没有打开,而且我总是将 text/html 作为内容类型,我通过 Firebug 看到了它。

这是下载的代码(来自 php.ent):

 if (file_exists($filename)) {
            header("Content-Length: " . filesize($filename));
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . $certName);

            readfile($filename, false);
            exit();
        }

这就是我调用我的服务的方式:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
$dat=array( 
 // many args
            );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dat);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
    if ($curl_errno > 0) {
       echo "cURL Error ($curl_errno): $curl_error\n";
    } else {
       echo "Data received: $data\n";
   }
    curl_close($ch);

伙计们怎么了?

4

1 回答 1

1
  1. Your browser makes an HTTP request
  2. Your server runs a PHP script that runs cURL
  3. cURL gets data from another server and ignores the Content-Disposition header because it is cURL and not a browser
  4. The PHP script running cURL outputs Data received: $data as the body of an HTML document
  5. The browser receives that HTTP document

If you want the cURL using program to act as a proxy, then you need to proxy the HTTP headers and not add extra data to the output.

于 2013-06-13T11:55:27.870 回答