spatie/lighthouse-php
Run Google Lighthouse audits from PHP. Test any URL and retrieve category scores (performance, accessibility, SEO, etc.) and individual audit details. Configure headers, user agent, categories, CPU throttling, and max load wait, then run and parse results.
Running Lighthouse will return an instance of Spatie\Lighthouse\LighthouseResult. On this page, we'll cover the various methods you can call on LighthouseResult
use Spatie\Lighthouse\Lighthouse;
// instance of `Spatie\Lighthouse\LighthouseResult`
$result = Lighthouse::url('https://example.com');
You can use the scores method to get scores of the five categories Lighthouse runs audits for.
$result->scores(); // returns an array like this one:
/*
* [
* 'performance' => 98,
* 'accessibility' => 83,
* 'best-practices' => 90,
* 'seo' => 92,
* 'pwa' => 43,
* ]
*/
Lighthouse will run various audits on your website. You can learn more about the meaning of each audit in the Lighthouse docs.
You can get an array with names of all audits using the auditNames() method.
$allAuditNames = $result->auditNames();
Here's how you can get the results of an audit:
$result->audit('first-contentful-paint');
/* returns an array like this one.
*
* [
* 'id' => 'first-contentful-paint'
* 'title' => 'First Contentful Paint'
* 'score' => 0.98
* 'scoreDisplayMode' => 'numeric'
* 'numericValue' => 1262.95
* 'numericUnit' => 'millisecond'
* 'displayValue' => '1.3 s'
* ]
*/
To get the results of all audits in one go, call, audits()
$result->audits();
There are a few convenience methods to get the most interesting metrics for the performance category.
You can call all these methods on $result. The ones ending on inMs return results in milliseconds.
firstContentfulPaintInMs()largestContentfulPaintInMs()speedIndexInMs()totalBlockingTimeInMs()timeToInteractiveInMs()cumulativeLayoutShift()totalPageSizeInBytes()Some of these methods have equivalent methods that return a formatted result. So instead of milliseconds they return a formatted string like 1.3 s
formattedFirstContentfulPaint()formattedLargestContentfulPaint()formattedSpeedIndex()formattedTotalBlockingTime()formattedTimeToInteractive()formattedCumulativeLayoutShift()The result will also contain the settings that were used to run Lighthouse.
// returns an array
$settings = $result->configSettings();
To get a specific config setting, you can pass a key in dot notation to configSettings(). Here's how you can the used browser width.
$width = $result->configSettings('screenEmulation.width');
There are also a couple of convenience methods to quickly get interesting pieces of config that you can call on $result:
formFactor(): returns desktop or mobileuserAgent(): returns the user agent that was usednetworkThrottlingWasEnabled(): returns a booleancpuThrottlingWasEnabled: return a booleanTo get the raw results that were returned from Lighthouse, you can use the rawResults method.
// returns an array with everything returned from Lighthouse
$result->rawResults();
You can a specific value from the raw results by passing a key using dot notation.
// returns the lighthouse version
$result->rawResults('lhr.lighthouseVersion');
Lighthouse computes a benchmark index as a rough approximation of the host device's CPU performance.
$result->benchmarkIndex(); // returns an integer
How can I help you explore Laravel packages today?