【问题标题】:Find score inside json result of PageSpeed Insights API v5在 PageSpeed Insights API v5 的 json 结果中查找分数
【发布时间】:2019-04-16 20:36:43
【问题描述】:

我正在尝试 Google v5 API 页面速度洞察,但我在 JSON 结果中找不到 SCORE。

这是https://www.googleapis.com/pagespeedonline/v5/runPagespeed的api

在 v4 中有一个 ruleGroups.SPEED.score 包含一个整数和分数。

我在哪里可以找到 v5 中的分数?

【问题讨论】:

    标签: pagespeed google-pagespeed-insights-api


    【解决方案1】:

    “我认为是如下:json.lighthouseResult.categories.performance.score

    返回一个最大为 1 的小数。所以你必须乘以 100 才能得到百分比。对我有用..但是我似乎每次都没有得到相同的价值。它会波动......”正如 Jeroen 所说。100% 正确

    “这不可能是正确的。我已经检查了几页,json.lighthouseResult.categories.performance.score 比developers.google.com/speed/pagespeed/insights 的分数高得多 - 帕斯卡·巴约拉特"

    这是不正确的,因为您看到的是桌面结果而不是移动结果。 90% 情况下的桌面 99 或 100。

    试试这个:

    URL: https://www.google.com/
    Return-Fields: lighthouseResult/categories/*/score
    
    * is a wildcard
    
    Indentations (PrettyPrint): no
    Strategy: Mobile
    Categories:
      Performance
      Progressive Web App (PWA)
      Best practices
      Accessibility
      SEO
    

    使用这些参数,API URL 为:

    https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fstackoverflow.com%2F&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=mobile&category=performance&category=pwa&category=best-practices&category=accessibility&category=seo
    

    JSON-Response 看起来像这样:

    {
    "lighthouseResult": {
        "categories": {
            "performance": {
                "score":0.87
            },
            "accessibility": {
                "score":0.7
            },
            "best-practices": {
                "score":0.77
            },
            "seo": {
                "score":0.9
            },
            "pwa": {
                "score":0.56
            }
        }
    }
    

    }

    【讨论】:

      【解决方案2】:

      我根据以前的答案创建了简单的 php 脚本,可以帮助您找到桌面和移动分数。只需提供您可以找到的网站 url 和 API 密钥here

      <?php
      
      $key = '';
      $url = '';
      
      $mobile = find_score( $url, 'mobile', $key );
      $desctop = find_score( $url, 'desktop', $key );
      
      echo "Mobile: " . $mobile . '; ';
      echo "Desctop: " . $desctop . '; '; 
      
      /**
       * Find PSI api score for certain device of certain url
       *
       * @param string $url
       * @param string $device Possible values: desctop, mobile
       * @param string $key
       *
       * @return string | integer
       */
      function find_score( $url, $device, $key = '' ) {
      
          $url = 
              "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" . $url . "&category=performance&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=" . $device . "&key=" . $key;
      
          $init = curl_init();
      
          curl_setopt($init, CURLOPT_URL, $url);
          curl_setopt($init, CURLOPT_RETURNTRANSFER, true);
      
          $responce = curl_exec( $init );
          curl_close($init);
      
          $responce = json_decode( $responce );
      
          if ( ! empty( $responce->lighthouseResult->categories->performance->score ) ) {
              $score = $responce->lighthouseResult->categories->performance->score;
              $score = $score * 100;
          } else {
              $score = 'API Error';
          }
      
          return $score;
      }
      

      【讨论】:

        【解决方案3】:

        它是 Jeroen 描述的 json.lighthouseResult.categories.performance.score

        您可以使用以下示例返回所有可能的审计类别:

        • 网址:https://www.google.com/
        • 返回字段:lighthouseResult/categories/*/score
          • * 是通配符
        • 压痕(PrettyPrint):no
        • 策略:Desktop
        • 分类:
          • Performance
          • Progressive Web App (PWA)
          • Best practices
          • Accessibility
          • SEO
        • API 密钥:{YOUR_API_KEY}
          使用这些参数,API URL 为:

          https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fstackoverflow.com%2F&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=desktop&category=performance&category=pwa&category= best-practices&category=accessibility&category=seo&key={YOUR_API_KEY}


        JSON-Response 看起来像这样:
        {
            "lighthouseResult": {
                "categories": {
                    "performance": {
                        "score":0.99
                    },
                    "accessibility": {
                        "score":0.7
                    },
                    "best-practices": {
                        "score":0.77
                    },
                    "seo": {
                        "score":0.9
                    },
                    "pwa": {
                        "score":0.56
                    }
                }
            }
        }
        

        【讨论】:

        • 我正在寻找在单个 api 命中中显示策略(移动和桌面)性能得分。有什么我们可以做到的吗?
        • 目前似乎只提供桌面分数。我怎样才能只获得移动设备?
        【解决方案4】:

        我认为是这样的: json.lighthouseResult.categories.performance.score

        返回一个最大为 1 的小数。所以你必须乘以 100 才能得到百分比。对我有用..但是我似乎每次都没有得到相同的价值。它波动...

        【讨论】:

        • 那是不正确的。我已经检查了几页,json.lighthouseResult.categories.performance.score 比developers.google.com/speed/pagespeed/insights 的分数高得多
        • 这是正确的答案。我在几个网站上测试过它是正确的。谢谢。
        • 为了应对波动的分数,运行审计 5 次并取中值。有助于处理审计运行中的异常值。
        猜你喜欢
        • 2019-05-07
        • 1970-01-01
        • 1970-01-01
        • 2016-09-04
        • 2016-12-24
        • 2019-07-09
        • 2023-02-09
        • 2019-11-17
        • 1970-01-01
        相关资源
        最近更新 更多