PHP code example of spatie / lighthouse-php

1. Go to this page and download the library: Download spatie/lighthouse-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / lighthouse-php example snippets


use Spatie\Lighthouse\Lighthouse;

// returns an instance of Spatie\Lighthouse\LighthouseResult
$result = Lighthouse::url('https://example.com')->run();

$result->scores(); // returns an array like this one:
/*
 * [
 *    'performance' => 98,
 *    'accessibility' => 83,
 *    'best-practices' => 90,
 *    'seo' => 92,
 *    'pwa' => 43,  
 * ]
 */

use Spatie\Lighthouse\Lighthouse;
use Spatie\Lighthouse\Enums\Category;

Lighthouse::url('https://example.com')
    ->userAgent('My user agent')
    ->headers(['MyExtraHeader' => 'HeaderValue'])
    ->categories(Category::Performance, Category::Accessibility)
    ->throttleCpu()
    ->run();

$result->audit('first-contentful-paint') // returns this array

/*
 * [
 *     'id' => 'first-contentful-paint'
 *     'title' => 'First Contentful Paint'
 *     'score' => 0.98
 *     'scoreDisplayMode' => 'numeric'
 *     'numericValue' => 1262.95
 *     'numericUnit' => 'millisecond'
 *     'displayValue' => '1.3 s'
 * ]
 */

$result->saveHtml($pathToReport)