卷曲不返回内容长度标题

时间:2010-05-02 04:46:35

标签: php curl

尝试使用curl获取图像文件大小,但不返回内容长度标题:

$url ="http://www.collegefashion.net/wp-content/plugins/feed-comments-number/image.php?1263";
$fp = curl_init();
curl_setopt($fp, CURLOPT_NOBODY, true);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($fp, CURLOPT_FAILONERROR,1);
curl_setopt($fp, CURLOPT_REFERER,'');
curl_setopt($fp, CURLOPT_URL, $url);
curl_setopt($fp, CURLOPT_HEADER,1);
curl_setopt($fp, CURLOPT_USERAGENT,'Mozilla/5.0');
$body = curl_exec($fp);

的var_dump($体):

HTTP/1.1 200 OK
Date: Sun, 02 May 2010 02:50:20 GMT
Server: Apache/2.0.63 (CentOS)
X-Powered-By: W3 Total Cache/0.8.5.2
X-Pingback: http://www.collegefashion.net/xmlrpc.php
Cache-Control: no-cache, must-revalidate
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Content-Type: image/png

它通过ssh工作:

curl -i http://www.collegefashion.net/wp-content/plugins/feed-comments-number/image.php?1263
HTTP/1.1 200 OK
Date: Sun, 02 May 2010 03:38:43 GMT
Server: Apache/2.0.63 (CentOS)
X-Powered-By: W3 Total Cache/0.8.5.2
X-Pingback: http://www.collegefashion.net/xmlrpc.php
Cache-Control: no-cache, must-revalidate
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Content-Length: 347
Content-Type: image/png

2 个答案:

答案 0 :(得分:1)

CURLOPT_NOBODY发出HEAD请求,而带-i的命令行是GET请求...

如果你在命令行版本中使用-I,它们会更相似。

答案 1 :(得分:0)

检查 curl_getinfo()

$size = curl_getinfo($fp, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

curl_exec()之后执行。

另一个选择是将CURLOPT_HEADER设置为false,然后执行strlen($body) - 忽略这一点,我没有注意到您正在使用{{ 1}}。

相关问题