忽略内容类型:CURL中的应用程序/强制下载

时间:2012-07-03 12:50:16

标签: php file curl download

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

$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);

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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 :(得分:0)

Curl符合强制下载标头,但file_get_contents可用于将文件下载下载限制为仅1个字节。这解决了这个问题!

$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]);
相关问题