我需要读取一个文件的大小,但服务器强迫我先下载它。我注意到其中一个响应头是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);有什么想法吗?
发布于 2012-07-03 21:16:33
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]);发布于 2012-07-03 21:08:26
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
https://stackoverflow.com/questions/11311310
复制相似问题