Logo

Navigation
  • Home
  • Android
  • Web
  • Raspberry Pi
  • PHP
  • Go

Check your server download speed via PHP using CURL

By Rajinder Deol | on May 7, 2014 | 3 Comments
Technology Web
check speed

Most of the time, when we buy server infrastructure, we want to verify the download speed of the server. This becomes more critical when we have a proxy server which is downloading images or media from different servers over the internet.

Below is a very simple example to calculate server download speed using Curl in PHP.

php error_reporting(E_ALL | E_STRICT);

// Initialize cURL with given url
$url = 'http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

set_time_limit(65);

$execute = curl_exec($ch);

$info = curl_getinfo($ch); // Time spent downloading

$time = $info['total_time'] - $info['namelookup_time'] - $info['connect_time'] - $info['pretransfer_time'] - $info['starttransfer_time'] - $info['redirect_time'];

// Echo friendly messages
header('Content-Type: text/plain');
printf("Downloaded %d bytes in %0.4f seconds.\n", $info['size_download'], $time);
printf("Which is %0.4f mbps\n", $info['size_download'] * 8 / $time / 1024 / 1024);
printf("CURL said %0.4f mbps\n", $info['speed_download'] * 8 / 1024 / 1024);
echo "\n\ncurl_getinfo() said:\n", str_repeat('-', 31 + strlen($url)), "\n";

foreach ($info as $label =--> $value){
printf("%-30s %s\n", $label, $value);
}

The output will be:
Downloaded 6576848 bytes in 16.2153 seconds.
Which is 3.0944 mbps
CURL said 3.0755 mbps

If you print curl_getinfo() the output will be:

———————————————————————————————
url http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg
content_type image/jpeg
http_code 200
header_size 263
request_size 198
filetime -1
ssl_verify_result 0
redirect_count 0
total_time 16.314966
namelookup_time 0.000287
connect_time 0.021524
pretransfer_time 0.021595
size_upload 0
size_download 6576848
speed_download 403117
speed_upload 0
download_content_length 6576848
upload_content_length 0
starttransfer_time 0.056275
redirect_time 0

Related

Share this story:
  • tweet

Recent Posts

  • RaspberryPi with PiHole to block ads everywhere

    September 22, 2021 - 2 Comments
  • Reviving RaspberryPi 1 Model B in 2021 After 7 years

    September 13, 2021 - 1 Comment
  • NodeJS Consumer for AWS SQS using Docker

    November 10, 2018 - 3 Comments

Author Description

3 Responses to “Check your server download speed via PHP using CURL”

  1. July 24, 2015

    BusinessFirstFamily Reply

    I will definitely be reading more if you keep producing posts such as this one.

  2. April 3, 2017

    YouAreCopyCat Reply

    well this is copied article and code

    So many errors . try to be genuine next time

    • April 4, 2017

      Rajinder Deol Reply

      Hi,

      it is simple PHP and Curl so nothing fancy or unique. I have tried to use them to check server time. However if you share what errors you have got I will help you more.

Leave a Reply Cancel Reply

Your email address will not be published. Required fields are marked *


*
*

Recent Posts

  • RaspberryPi with PiHole to block ads everywhere
  • Reviving RaspberryPi 1 Model B in 2021 After 7 years
  • NodeJS Consumer for AWS SQS using Docker
  • Laravel development using docker-compose with nginx and Mysql
  • Writing Your First Service in Golang Go-Micro

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 9 other subscribers

Categories

  • Android (7)
  • Education (9)
  • Go (2)
  • PHP (5)
  • Raspberry Pi (4)
  • Technology (17)
  • Web (8)

Author details:

Raj Deol

Raj Deol

Traveller, Foodie and electronics enthusiast.

View Full Profile →

© 2016. All Rights Reserved.