PHP code example of smnandre / pagespeed-api

1. Go to this page and download the library: Download smnandre/pagespeed-api 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/ */

    

smnandre / pagespeed-api example snippets


use PageSpeed\Api\PageSpeedApi;

$pageSpeedApi = new PageSpeedApi();

// or with API key (optional)
$pageSpeedApi = new PageSpeedApi('YOUR_API_KEY');

// Analyze a page
$analysis = $pageSpeedApi->analyse('https://example.com/');

// ...with a specific strategy (mobile or desktop)
$analysis = $pageSpeedApi->analyse('https://example.com/', 'mobile');

// ...with a specific locale (e.g., fr_FR)
$analysis = $pageSpeedApi->analyse('https://example.com/', locale: 'fr_FR');

// ...with a specific category (performance, accessibility, best-practices, seo)
$analysis = $pageSpeedApi->analyse('https://example.com/', categories: 'performance');

use PageSpeed\Api\PageSpeedApi;

$pageSpeedApi = new PageSpeedApi();
$analysis = $pageSpeedApi->analyse('https://www.example.com');

$scores = $analysis->getAuditScores();

// array (
//   'performance' => 100,
//   'accessibility' => 88,
//   'best-practices' => 100,
//   'seo' => 90,
// )

use PageSpeed\Api\PageSpeedApi;

$pageSpeedApi = new PageSpeedApi();
$analysis = $pageSpeedApi->analyse('https://www.example.com');

$metrics = $analysis->getLoadingMetrics();

// array (
//   'CUMULATIVE_LAYOUT_SHIFT_SCORE' => 'FAST',
//   'EXPERIMENTAL_TIME_TO_FIRST_BYTE' => 'AVERAGE',
//   'FIRST_CONTENTFUL_PAINT_MS' => 'FAST',
//   'FIRST_INPUT_DELAY_MS' => 'FAST',
//   'INTERACTION_TO_NEXT_PAINT' => 'FAST',
//   'LARGEST_CONTENTFUL_PAINT_MS' => 'FAST',
// )