简体   繁体   中英

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. 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 ?

You can use curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD) which returns:

Content length of download, read from Content-Length: field

In this particular case, -1 seems to be a valid response :

Since 7.19.4, this returns -1 if the size isn't known.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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