首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略Content-Type: CURL中的application/force-download

忽略Content-Type: CURL中的application/force-download
EN

Stack Overflow用户
提问于 2012-07-03 20:50:16
回答 2查看 1.1K关注 0票数 1

我需要读取一个文件的大小,但服务器强迫我先下载它。我注意到其中一个响应头是Content-Type: application/force-download,这似乎绕过了我的curl输入……

代码语言:javascript
复制
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
curl_exec($ch);
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-03 21:16:33

Curl符合强制下载头部,但是可以使用file_get_contents将文件下载限制为1字节。这就解决了问题!

代码语言:javascript
复制
$postdata = http_build_query(
    array(
        'username' => "",
        'password' => ""
    )
);
$params = array(
    'http' => array
    (
      'method' => 'POST',
      'header'=>"Content-type: application/x-www-form-urlencoded\r\n",
      'content' => $postdata
    )
);
$ctx = stream_context_create($params);
file_get_contents($url,false,$ctx,0,1);
$size = str_replace("Content-Length: ","",$http_response_header[4]);
票数 0
EN

Stack Overflow用户

发布于 2012-07-03 21:08:26

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

这两行重置CURLOPT_NOBODY

CURLOPT_NOBODY将方法更改为HEAD,CURLOPT_POST将其更改为POST。

来源:http://php.net/manual/en/function.curl-setopt.php

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11311310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档