Check Site Loading Speed using CURL curl_getinfo() in PHP.

Monday, June 22, 2009 10:06
Posted in category PHP, Programming

We use curl_getinfo() in to get the information of last transfer that made use of curl functions in PHP.  Using the following code we can get the time taken to load a webpage.

$ch = curl_init('http://www.inetminds.com/parsing-xml-feeds-in-php-with-simplexml-111/');

// set  cURL to return the contents in a variable instead of giving it to the browser.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Execute
$contents = curl_exec($ch);

// Check if any error occured
if(!curl_errno($ch)){
// $info = curl_getinfo($ch);
$total_time     = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
$url             =curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

echo $ulr 'Took ' . $total_time . ' seconds to load ' ;
}
// Close handle
curl_close($ch);

Using curl_init(‘http://www.inetminds.com/parsing-xml-feeds-in-php-with-simplexml-111/’) we have Initialized the session to load ‘http://www.inetminds.com/parsing-xml-feeds-in-php-with-simplexml-111/’ and returns a cURL handle which can be passed to other cURL functions.

Using curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) we loaded the page to variable $ch.

Using curl_getinfo($ch, CURLINFO_TOTAL_TIME) we retrieved the total time taken for the last CURL transfer.

Using curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) we retrieved the url effected or used in the last CURL transfer.

Below are some more options that can be used whit curl_getinfo() for different requirements.

* CURLINFO_EFFECTIVE_URL – Last effective URL
* CURLINFO_HTTP_CODE – Last received HTTP code
* CURLINFO_FILETIME – Remote time of the retrieved document, if -1 is returned the time of the document is unknown
* CURLINFO_TOTAL_TIME – Total transaction time in seconds for last transfer
* CURLINFO_NAMELOOKUP_TIME – Time in seconds until name resolving was complete
* CURLINFO_CONNECT_TIME – Time in seconds it took to establish the connection
* CURLINFO_PRETRANSFER_TIME – Time in seconds from start until just before file transfer begins
* CURLINFO_STARTTRANSFER_TIME – Time in seconds until the first byte is about to be transferred
* CURLINFO_REDIRECT_TIME – Time in seconds of all redirection steps before final transaction was started
* CURLINFO_SIZE_UPLOAD – Total number of bytes uploaded
* CURLINFO_SIZE_DOWNLOAD – Total number of bytes downloaded
* CURLINFO_SPEED_DOWNLOAD – Average download speed
* CURLINFO_SPEED_UPLOAD – Average upload speed
* CURLINFO_HEADER_SIZE – Total size of all headers received
* CURLINFO_HEADER_OUT – The request string sent
* CURLINFO_REQUEST_SIZE – Total size of issued requests, currently only for HTTP requests
* CURLINFO_SSL_VERIFYRESULT – Result of SSL certification verification requested by setting CURLOPT_SSL_VERIFYPEER
* CURLINFO_CONTENT_LENGTH_DOWNLOAD – content-length of download, read from Content-Length: field
* CURLINFO_CONTENT_LENGTH_UPLOAD – Specified size of upload
* CURLINFO_CONTENT_TYPE – Content-Type: of downloaded object, NULL indicates server did not send valid Content-Type: header

Source : http://in2.php.net/manual/en/function.curl-getinfo.php

Bookmark and Share

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

You can leave a response, or trackback from your own site.

2 Responses to “Check Site Loading Speed using CURL curl_getinfo() in PHP.”

  1. tahsin hasan says:

    November 12th, 2009 at 12:53 am

    Hi,

    Plz see my problem.

    *** code starts here

    function byte_convert($bytes)
    {
    $symbol = array(‘B’, ‘KB’, ‘MB’, ‘GB’, ‘TB’, ‘PB’, ‘EB’, ‘ZB’, ‘YB’);

    $exp = 0;
    $converted_value = 0;
    if( $bytes > 0 )
    {
    $exp = floor( log($bytes)/log(1024) );
    $converted_value = ( $bytes/pow(1024,floor($exp)) );
    }

    return sprintf( ‘%.2f ‘.$symbol[$exp], $converted_value );
    }

    $userAgent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)’;
    $url = ‘http://ksino.com/’;

    // initialize curl with given url
    $ch = curl_init($url);
    // make sure we get the header
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // make it a http HEAD request
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    // add useragent
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    //Tell curl to write the response to a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // The maximum number of seconds to allow cURL functions to execute.
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
    // Tell curl to stop when it encounters an error
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);

    $execute = curl_exec($ch);

    // Check if any error occured
    if(!curl_errno($ch))
    {
    $bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    // Display the File Size
    echo byte_convert($bytes);

    $total_time = curl_getinfo($ch);

    print_r($total_time);
    //echo ‘Took ‘ . $total_time . ‘ seconds to send a request to ‘ . $url;
    clearstatcache();
    }
    curl_close($ch);

    **** code ends here

    plz run the code, and try to refresh it several time. and u will see the ‘total_time’ value will be changed every time. why?

    tahsin.

  2. PHP_Starter says:

    January 15th, 2010 at 4:13 am

    Because the total time depends on the number of connections to the server at a time of running this script(also many other factors effects this).

Leave a Reply