Hello All.
Im trying to code a small app that will tell me the server download speed. (not upload speed).
I figured useing curl whould be best to download file.
Then use microtime before and after the curl. To find the time it took to download.
Divide filesize by time it took.
Here Is My Attempt:
$time_start = microtime(true);
// initialize curl with given url
$ch = curl_init("http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg");
// 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, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
//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 = 6576848 / 1048576; // 6576848=filesize || 1048576=how many bytes in a mb
$time_end = microtime(true);
$time = $time_end - $time_start; //Time it took
$mbps = $time / $bytes;
echo 'MBps: '.$mbps;
clearstatcache();
}
curl_close($ch);
It says my speed is MBps: 75.556655877259 :goof: … Thats wrong by the way…
Any idea where I am going wrong? … Or know a better method to calculate MBps server download speed?
Much Thanks
Very Appretiated!