我尝试在php上编写简单的解析器,只能给我html页面的内容长度。现在我有这个代码:
$urls = array(
'http://Link1.com/',
'http://Link2.com'
);
$mh = curl_multi_init();
$connectionArray = array();
foreach($urls as $key => $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $ch);
$connectionArray[$key] = $ch;
}
$running = null;
do
{
curl_multi_exec($mh, $running);
}while($running > 0);
foreach($connectionArray as $key => $ch)
{
$content = curl_multi_getcontent($ch);
echo $content."<br>";
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
如何从$content获得Content-Length
?
您可以使用返回以下内容的curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD)
:
下载的内容长度,从内容长度:字段中读取
在这种特殊情况下,-1
似乎是一个有效的响应:
从 7.19.4 开始,如果大小未知,则返回 -1。