如何获得主要指标?

How to get the main indicator?

我创建了用于检查网站速度的脚本

<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>";
}
?>

但在结果中我找不到最重要的值。我试图分析给出脚本的 JSON,但我没有看到任何类似的东西。也许有人可以建议从哪里得到这个数字?

在您想要的 JSON 响应中:

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

您正在寻找这部分回复:

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

注意分数是用小数表示的,所以 1 = 100、0.5 = 50 等等