如何使用PHP cURL库检索带有http响应头的附件

如何使用PHP cURL库检索带有http响应头的附件,php,curl,Php,Curl,我试图检索使用php cURL库发出的http请求的原始附件。现在我得到的是html响应,但不是附件 还有一个非常类似的问题,就是使用cURL选项-j。我在php cURL库中找不到等效的set_opt 当我在浏览器中发出请求时,标题信息中会引用所需的附件,浏览器知道如何自动下载该附件。在我的脚本中,我只需要将附件的原始数据加载到变量中 如何在php中将附件数据加载到变量中? 来自远程服务器的标头响应: Cache-Control:must-revalidate, post-check=0, p

我试图检索使用php cURL库发出的http请求的原始附件。现在我得到的是html响应,但不是附件

还有一个非常类似的问题,就是使用cURL选项-j。我在php cURL库中找不到等效的set_opt

当我在浏览器中发出请求时,标题信息中会引用所需的附件,浏览器知道如何自动下载该附件。在我的脚本中,我只需要将附件的原始数据加载到变量中

如何在php中将附件数据加载到变量中?

来自远程服务器的标头响应:

Cache-Control:must-revalidate, post-check=0, pre-check=0
Content-Disposition:attachment; filename=exel_file.xls
Content-Length:65
Content-Transfer-Encoding:binary
Content-Type:application/download
Date:Thu, 30 Jun 2016 15:12:05 GMT
Expires:Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified:Thu, 30 Jun 2016 16:33:14 GMT
Pragma:public
Server:Microsoft-IIS/7.5
X-Powered-By:PHP/5.2.17
我的php cURL请求:

$url = "https://subdomain.example.com?parameter=1";
$post = "post1=false&".
        "post2=&".
        "post3=false&".
        "post4=&".
        "post5=&".
        "post6=1243123421";

$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieFile); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieFile); 
curl_setopt($ch, CURLOPT_USERAGENT,  $userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER,    1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$html = curl_exec($ch);
我已经尝试直接访问exel_file.xls,但没有效果

更新:
当我在Chrome开发者工具中找到该文件时,我最终还是设法直接访问了它。我过滤了网络对“doc”的响应,找到了与附件相关的请求。文件位于,而不是。

将CURLOPT_BINARYTRANSFER设置为TRUE并设置内容类型

<?php

$url = "http://slackbuilds.org/slackbuilds/13.37/multimedia/umplayer.tar.gz";
$opts = array(
  CURLOPT_URL =>$url,
  CURLINFO_CONTENT_TYPE => "text/xml",
  CURLOPT_BINARYTRANSFER => TRUE,
  CURLOPT_RETURNTRANSFER => TRUE
);

$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$info = curl_getinfo($ch); 
curl_close($ch);

header("Content-Type: application/x-gzip"); 
header("Content-disposition: attachment; filename=zipfile.tar.gz");

echo $data;
?>
TRUE,
CURLOPT_RETURNTRANSFER=>TRUE
);
$ch=curl_init();
curl_setopt_数组($ch,$opts);
$data=curl\u exec($ch);
$info=curl\u getinfo($ch);
卷曲关闭($ch);
标题(“内容类型:应用程序/x-gzip”);
标题(“内容配置:附件;文件名=zipfile.tar.gz”);
回波数据;
?>

这有帮助吗

没有。如果我想在浏览器中下载文件,您将显示。我正在尝试将附件加载到变量中。用我正在尝试的设置标题是没有意义的。我测试了你提供的旋度选项,但它们不会改变结果。谢谢你看了。