Search code examples
performancepagespeedpagespeed-insights

How to get the main indicator?


I create script for check speed of sites

<form name="form1" method="post" action="">
  <label for="url">URL:</label>
  <input type="text" name="url" id="url">
  <br>
  <input type="submit" name="submit" id="submit" value="Send">
</form>
<?php

if((isset($_POST['url'])) and ($_POST['url']!=""))
{
    echo "<h2>".$_POST['url']."</h2>";
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url='.$_POST['url'].'&key=..........................');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

echo "<pre>".$result."</pre>";
}
?>

But in the results I can not find the most important value. I tried to analyze JSON which gives out a script, but I do not see anything similar. Maybe someone can advise where to get this figure?

enter image description here


Solution

  • In the JSON response you want:

    ['lighthouseResult']['categories']['performance']['score']

    You are looking for this part of the response:

    "categories": {
      "performance": {
        "id": "performance",
        "title": "Performance",
        "score": 1,
    

    Notice that the score is expressed as a decimal, so 1 = 100, 0.5 = 50 etc.