curl_getinfo返回-1作为content-length [英] curl_getinfo returning -1 as content-length

查看:1360
本文介绍了curl_getinfo返回-1作为content-length的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立网站抓取工具,并使用 cURL class ,这些选项用于获取标题,以便我可以提取它们的 mime / type content-length



<$
p>这些是我的curl选项

  $ c-> setopt(CURLOPT_URL,$ theURL); 
$ c-> setopt(CURLOPT_HEADER,false);
$ c-> setopt(CURLOPT_RETURNTRANSFER,true);
$ c-> setopt(CURLOPT_TIMEOUT,10);
$ c-> setopt(CURLOPT_CONNECTTIMEOUT,10);
$ c-> setopt(CURLOPT_NOBODY,TRUE);
$ c-> setopt(CURLOPT_FOLLOWLOCATION,TRUE);
$ c-> setopt(CURLOPT_MAXREDIRS,2);

//在类内
$ theReturnValue = curl_exec($ this-> m_handle);
$ this-> m_status = curl_getinfo($ this-> m_handle);

但总是返回 [download_content_length] =>

CURLOPT_NOBODY >解决方案

CURLINFO_CONTENT_LENGTH_DOWNLOAD (这是PHP绑定使用的)说:


如果大小不知道, p>


I am building a website crawler and using a cURL class with these options for getting headers, so that I can extract their mime/type and content-length before downloading full site to very it as text/html and below specified size limits.

these are my curl options

$c->setopt(CURLOPT_URL, $theURL);
$c->setopt(CURLOPT_HEADER, false);
$c->setopt(CURLOPT_RETURNTRANSFER, true);
$c->setopt(CURLOPT_TIMEOUT, 10);
$c->setopt(CURLOPT_CONNECTTIMEOUT, 10);
$c->setopt(CURLOPT_NOBODY, TRUE);
$c->setopt(CURLOPT_FOLLOWLOCATION, TRUE);
$c->setopt(CURLOPT_MAXREDIRS, 2);

// Within Class
$theReturnValue = curl_exec($this->m_handle);
$this->m_status = curl_getinfo($this->m_handle) ;

but it always return [download_content_length] => -1 even without CURLOPT_NOBODY however the data (whole file) retrived is correct.

解决方案

The libcurl docs for CURLINFO_CONTENT_LENGTH_DOWNLOAD (which is what the PHP binding is using) says:

"this returns -1 if the size isn't known"

这篇关于curl_getinfo返回-1作为content-length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆