简体   繁体   中英

how to get external web page size and load time in php

how to get load time and page size in php. I'm using following technique to get load time but do we have better option to calculate load time.(any technique to get load time from header or other technique)

$t = microtime( TRUE );
$file = file_get_contents( "http://google.com/" );
print_r($file );
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";

I just need to confirm if this is the right technique or we have better choice and how to calculate web page size in php.

Code to get page size

$url = 'http://xAppsol.com/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$response = curl_exec($ch);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
print_r($header_size);

This code is providing with header size which is in Kb but how to check size of images and videos on web page. how to calculate exact size of all stuff on webpage which would be in MB.

As far as I know, You need to parse the page and get the size for each element.

$url = 'http://google.com/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$subject = curl_exec($curl);
//get the download size of page
print("Download size: " . curl_getinfo($curl, CURLINFO_SIZE_DOWNLOAD) .'<br>');

preg_match_all('/(?:src=)"([^"]*)"/m', $subject, $matchessrc);

preg_match_all('/link.*\s*(?:href=)"([^"]*)"/m', $subject, $matcheslink);

$matches = array_merge($matchessrc[1], $matcheslink[1]);

$domain = parse_url($url, PHP_URL_SCHEME). '://'.parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);

$checked = array();
foreach($matches as $m)
{
    if($m[0] == '/')
        $m = $domain.$m;
    elseif(substr($m, 0, 5) != 'http:' and substr($m, 0, 6) != 'https:')
        $m = $domain.'/'.$path.'/'.$m;

    if(in_array($m, $checked))
        continue;

    $checked[] = $m;

    $curl = curl_init($m);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $subject = curl_exec($curl);
    //get the download size of element
    print("Download size: " . curl_getinfo($curl, CURLINFO_SIZE_DOWNLOAD) .'<br>');
}

This will search form src elements (usually images and scripts), and href in <link> (usually css).

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