Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Created January 21, 2020 18:24
Show Gist options
  • Save INDIAN2020/4b1d96a098368d402237d60270b25b2c to your computer and use it in GitHub Desktop.
Save INDIAN2020/4b1d96a098368d402237d60270b25b2c to your computer and use it in GitHub Desktop.
function get_remote_file_size($url) {
$headers = get_headers($url, 1);
if (isset($headers['Content-Length']))
return $headers['Content-Length'];
//checks for lower case "L" in Content-length:
if (isset($headers['Content-length']))
return $headers['Content-length'];
}
function get_remote_file_size($url) {
$headers = get_headers($url, 1);
if (isset($headers['Content-Length']))
return $headers['Content-Length'];
//checks for lower case "L" in Content-length:
if (isset($headers['Content-length']))
return $headers['Content-length'];
//the code below runs if no "Content-Length" header is found:
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla/5.0
(Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3)
Gecko/20090824 Firefox/3.5.3'),
));
curl_exec($c);
$size = curl_getinfo($c, CURLINFO_SIZE_DOWNLOAD);
return $size;
curl_close($c);
}
include('simple_html_dom.php');
$URL = $argv[1];
// Create DOM from URL or file
$html = file_get_html($URL);
// find all images!!
foreach($html->find('img') as $element){
$size = get_remote_file_size($element->src);
$totalSize = $totalSize + $size;
$totalNumResources += 1;
/*
echo "Total Size So Far: $totalSize.\n";
echo "total resources: $totalNumResources .\n";
echo "IMAGE SIZE: $size.\n";
echo "$element->src.\n";
*/
}
// find all CSS files
foreach($html->find('link') as $element)
{
if (strpos($element->href,'.css') !== false) {
$size = retrieve_remote_file_size($element->href);
echo "SIZE: $size.\n";
$totalSize = $totalSize + $size;
$totalNumResources += 1;
}
}
// find all script tags
foreach($html->find('script') as $element)
{
//make sure this is javascript
if (strpos($element->src,'.js') !== false) {
$size = get_remote_file_size($element->src);
echo " Javascript SIZE: $size.\n";
$totalSize = $totalSize + $size;
$totalNumResources += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment