简体   繁体   English

如何获得响应php cURL的“内容长度”

[英]How to get `Content-length` of response php cURL

I try to write simple parser on php, with can give me only content-length of html page. 我尝试在php上编写简单的解析器,它只能给我HTML页面的内容长度。 For now I have this Code : 现在我有这个代码:

$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);

How can I get Content-Length from $content ? 如何从$ content获取Content-Length

You can use curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD) which returns: 您可以使用curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD)返回:

Content length of download, read from Content-Length: field 下载内容的长度,请从“内容长度:”字段中读取

In this particular case, -1 seems to be a valid response : 在这种情况下, -1似乎是一个有效的响应

Since 7.19.4, this returns -1 if the size isn't known. 从7.19.4开始,如果大小未知,则返回-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM