WebPagetest API Tutorial with Example

Before we learn more about webpagetest API, let's understand-

What is WebPagetest?

Webpagetest is one of the most popular and free tools for measuring webpage performance. Webpagetest enables you to run web performance tests on your site from a number of different locations across the world in a number of different browsers.

How to use WebPagetest API

Enter your website and click Start Test. Next, you will see the output window like below

How to use WebPagetest API

From this page, you can find values for following parameters of your webpage

WebPagetest API

Webpagetest has 2 primary API

  1. To Run Tests – http://www.webpagetest.org/runtest.php.
  2. To Check Test Status - http://www.webpagetest.org/testStatus.php
  3. To get Test Results - http://www.webpagetest.org/testStatus.php

To Run Tests:

Set Parameters

To Check Test Status

How to use WebPagetest API

How to use WebPagetest API

This information gives the test ID, the start time, the number of runs the test requested, etc.

To Check Test Results

Pass the tested to the API - http://www.webpagetest.org/xmlResult/141107_12_BXZ/

You will see the Test results in XML format like below

How to use WebPagetest API

Php Code sample to use Webpagetest API

	<?php
	$url = <a href=http://gtupapers.com //url to test
	$api_key = "<your-copy-api-key>"; // your api key
	  $webpagetest = "http://www.webpagetest.org/runtest.php?url=$url&runs=1&f=xml&k=$api_key"; 
	$xmlres = simplexml_load_file($webpagetest);
	$testid = $xmlres->data->testId;        
	echo "Test id : ".$testid." For url : ".$url;
	?>
 <?php
	$test_id = "<test-id-you-got-from-request-call>";
	$weburl = "http://www.webpagetest.org/xmlResult/$test_id/";
	 $xmlres = simplexml_load_file($weburl);
	 if($xmlres){
	$loadtime = ($xmlres->data->average->firstView->loadTime)/1000;
	echo "WebPage loadtime is : ".$loadtime;
	}
	?>

 

YOU MIGHT LIKE: