These days excellent work is being done to attract web site users by creating attractive and well managed web pages. While browsing any internet site or a web page as developer or designer of the web pages we might came across the thought, “how much size of this web page is? or how much costly to render this on my browser?”. Well today lets build a script which reports information of above questions. As usual we will be using PHP for scripting the code. This script includes two things in consideration. First, how many http/https requests are being used to render the page and Second, what is the total size of the rendered web page. We will create a script in a manner that it can be used from command line interface as well as from the browser. For this the cURL module is used. If it is not installed in your PHP set up then go to my previous article of installation and configuration of PHP.
Ok. So now what is the meaning of First part, “how many http/https requests are”. When any web page is rendered or loaded in your browser, it is a combination of one or multiple CSS files, one or multiple javascripts or .js files, code of Html, multiple images, etc… we call them web page resources as all together they make a web page. While web page is loaded all these multiple files are being called from one or different locations with specific web address. These files are called one by one as it cannot called all once at a time. The separate calls for each of these files are known as http/https requests.
Now let’s come to the Second part of the task – “what is the total size of the rendered web page” and that consist of file size of each of these resource we saw above. When http/https requests are responded uninterrupted then that response contains response header (developers and designers knows that). One of the headers is called “Content-Length” and that gives the size of that particular resource. We need to use built in API of PHP ie. get_headers and the argument would be the URL where the http/https request is sent. Bellow is the hint and good start of the code in PHP.
function file_size_rendered($url) {
//where $url in the argument is the location where request is being sent.
$headers = get_headers($url, 1);
if (isset($headers['Content-Length']))
return $headers['Content-Length'];
//Also check whether lower case of "L" is used in Content-length
if (isset($headers['Content-length']))
return $headers['Content-length'];
//if the content header is not found then use the curl request
$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);
}
This article is just a good start to inspire for deeper understanding or the technique of coding. One can browse more or post questions to get answer on more specific tasks.
0 Comment