widgetsburritos / webpagetest
PHP Library for Interacting with Web Page Test
Installs: 11 088
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 4
Open Issues: 0
Requires
- php: >=7.1
- guzzlehttp/guzzle: ^6.2
Requires (Dev)
- drupal/coder: ^8.2
- phpdocumentor/reflection-docblock: ^2.0
- phpunit/phpunit: ^6.0
- shrikeh/teapot: ^2.3
README
A php library for interacting with webpagetest.org.
Requires PHP 7.1+
Usage
Instantiating a new instance
To obtain a key, see Request API Key.
<?php use WidgetsBurritos\WebPageTest\WebPageTest; $wpt = new WebPageTest('YOUR_API_KEY');
To specify an alternate connection handler:
<?php
use WidgetsBurritos\WebPageTest\WebPageTest;
$wpt = new WebPageTest('YOUR_API_KEY', $handler);
To specify an alternate hosting instance:
<?php
use WidgetsBurritos\WebPageTest\WebPageTest;
$wpt = new WebPageTest('YOUR_API_KEY', $handler, 'https://www.example.com');
Status Codes
It is recommended to use an external library, such as Teapot instead of hardcoding status codes.
<?php
use Teapot\StatusCode;
# Examples:
StatusCode::CONTINUING; // 100 (Test Started)
StatusCode::SWITCHING_PROTOCOLS; // 101 (Test Pending)
StatusCode::OK; // 200 (Test Complete)
StatusCode::BAD_REQUEST; // 400 (Test Not Found)
StatusCode::UNAUTHORIZED; // 401 (Test Request Not Found)
StatusCode::PAYMENT_REQUIRED; // 402 (Test Cancelled)
?>
^ Note: the switching protocol and payment required status checks are not a mistake. As of 10/25/2017, webpagetest.org returns a "101 switching protocols" status for pending tests and a "402 Payment Required" status for cancelled tests.
Running a URL test
<?php if ($response = $wpt->runTest('https://www.google.com')) { if ($response->statusCode == StatusCode::OK) { // All test info is available in $response->data. $test_id = $response->data->testId; } } ?>
The library automatically populates the k
, f
and url
query string parameters. Optionally, you can supply additional parameters by passing in array.
<?php $options = [ 'label' => 'My Test Label', 'noimages' => 1, 'mobile' => 1, ]; if ($response = $wpt->runTest('https://www.google.com', $options)) { if ($response->statusCode == StatusCode::OK) { // All test info is available in $response->data. $test_id = $response->data->testId; } } ?>
See the Web Page Test API documentation for more information on supported parameters.
Getting a test's status
<?php if ($response = $wpt->getTestStatus($test_id)) { // All test info is available in $response->data. if ($response->statusCode == StatusCode::OK) { // Test is complete. } else if ($response->statusCode == StatusCode::CONTINUING) { // Test is running. } else if ($response->startCode == StatusCode::SWITCHING_PROTOCOLS) { // Test is waiting to start. } else if ($response->statusCode == StatusCode::PAYMENT_REQUIRED) { // Test has been cancelled. else { // Test failed. } } ?>
Getting a test's results
<?php if ($response = $wpt->getTestResults($test_id)) { // All test result info is available in $response->data. if ($response->statusCode == StatusCode::OK) { // Test is complete. } else if (in_array($response->statusCode, [StatusCode::CONTINUING, StatusCode::SWITCHING_PROTOCOLS])) { // Test is not yet complete. } else { // Test failed. } } ?>
Getting test locations
<?php if ($response = $wpt->getLocations()) { if ($response->statusCode == StatusCode::OK) { // All locations info is available in $response->data. } } ?>
Cancelling a Test
You can cancel a test by simply running:
<?php $wpt->cancelTest($test_id); ?>