1

我需要读取文件的大小,但服务器强迫我先下载它。我注意到响应标头之一是 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);

有任何想法吗?

4

2 回答 2

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

于 2012-07-03T13:08:26.407 回答
0

curl 符合 force-download 标头,但 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]);
于 2012-07-03T13:16:32.977 回答