Michael Walter
Michael Walter

Reputation: 1477

PHP: measure TTFB (Time To First Byte)

Since the TTFB can vary by every request, I want to make a statistic and get an average value for it. Does anyone know how I can measure this via PHP? The website bytecheck.com is able to analyze these data: Here is an example with example.com: http://www.bytecheck.com/results?resource=example.com

Does anyone a suggestions how I could achieve something like this?

Thanks in advance.

Upvotes: 3

Views: 3023

Answers (2)

spinsch
spinsch

Reputation: 1415

You can solve this with curl:

$url = 'https://www.example.com/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "TTFB of ".$url." is: ".$info['starttransfer_time'];

Result

TTFB of https://www.example.com/ is: 0.67417

Upvotes: 7

Jaime Niñoles
Jaime Niñoles

Reputation: 44

There is a good Curl wrapper here: https://packagist.org/packages/curl/curl

If you already have installed Composer, just do 'composer require curl/curl' and use it.

Regards.

Upvotes: 0

Related Questions