0

在阅读谷歌页面速度 api 开发人员指南后,我想使用 curl 或任何可以从输出中检索它的方法来检索页面速度分数

这里应该是我要使用的链接(我只需要分数"score": 97,因为我会将它存储在数据库中)

https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.my_site.com&key={MY API KEY}

它将给出以下结果

{
 "kind": "pagespeedonline#result",
 "request": {
  "url": "http://www.my___site.com"
 },
 "id": "http://www.my___site.com/",
 "responseCode": 404,
 "title": "404 Error - Page Not Found",
 "score": 97,
 "pageStats": {
  "numberResources": 19,
  "numberHosts": 11,
  "totalRequestBytes": "4982",
  "numberStaticResources": 10,
  "htmlResponseBytes": "67960",
  "cssResponseBytes": "11318",
  "imageResponseBytes": "20824",
  "javascriptResponseBytes": "200855",
  "otherResponseBytes": "52076",
  "numberJsResources": 6,
  "numberCssResources": 2
 },
 "formattedResults": {
  "locale": "en_US",
  "ruleResults": {
   "AvoidLandingPageRedirects": {
    "localizedRuleName": "Avoid landing page redirects",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your page has no redirects. Learn more about avoiding landing page redirects.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/AvoidRedirects"
        }
       ]
      }
     }
    ]
   },
   "EnableGzipCompression": {
    "localizedRuleName": "Enable compression",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "You have compression enabled. Learn more about enabling compression.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/EnableCompression"
        }
       ]
      }
     }
    ]
   },
   "LeverageBrowserCaching": {
    "localizedRuleName": "Leverage browser caching",
    "ruleScore": 90,
    "ruleImpact": 0.49702380952380953,
    "urlBlocks": [
     {
      "header": {
       "format": "Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network."
      }
     },
     {
      "header": {
       "format": "Leverage browser caching for the following cacheable resources:",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/LeverageBrowserCaching"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "$1 ($2)",
         "args": [
          {
           "type": "URL",
           "value": "http://www.google.com/adsense/domains/caf.js"
          },
          {
           "type": "DURATION",
           "value": "60 minutes"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "MainResourceServerResponseTime": {
    "localizedRuleName": "Reduce server response time",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your server responded quickly. Learn more about server response time optimization.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/Server"
        }
       ]
      }
     }
    ]
   },
   "MinifyCss": {
    "localizedRuleName": "Minify CSS",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your CSS is minified. Learn more about minifying CSS.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/MinifyResources"
        }
       ]
      }
     }
    ]
   },
   "MinifyHTML": {
    "localizedRuleName": "Minify HTML",
    "ruleScore": 94,
    "ruleImpact": 0.2277,
    "urlBlocks": [
     {
      "header": {
       "format": "Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up download and parse times."
      }
     },
     {
      "header": {
       "format": "Minify HTML for the following resources to reduce their size by $2 ($3 reduction).",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/MinifyResources"
        },
        {
         "type": "BYTES",
         "value": "2.1KiB"
        },
        {
         "type": "PERCENTAGE",
         "value": "9%"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "Minifying $1 could save $2 ($3 reduction) after compression.",
         "args": [
          {
           "type": "URL",
           "value": "http://dsnextgen.com/?domainname=www.my___site.com&a_id=101686&session_token=undefined"
          },
          {
           "type": "BYTES",
           "value": "2.1KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "9%"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "MinifyJavaScript": {
    "localizedRuleName": "Minify JavaScript",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your JavaScript content is minified. Learn more about minifying JavaScript.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/MinifyResources"
        }
       ]
      }
     }
    ]
   },
   "MinimizeRenderBlockingResources": {
    "localizedRuleName": "Eliminate render-blocking JavaScript and CSS in above-the-fold content",
    "ruleScore": 100,
    "ruleImpact": 2.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your page has $1 blocking script resources. This causes a delay in rendering your page.",
       "args": [
        {
         "type": "INT_LITERAL",
         "value": "2"
        }
       ]
      }
     },
     {
      "header": {
       "format": "None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML."
      }
     },
     {
      "header": {
       "format": "Remove render-blocking JavaScript:",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/BlockingJS"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://pagead2.googlesyndication.com/apps/domainpark/show_afd_ads.js"
          }
         ]
        }
       },
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://googleads.g.doubleclick.net/apps/domainpark/domainpark.cgi?callback=_google_json_callback&output=js&client=ca-dp-oversee_ncd&domain_name=www.my___site.com&st=create&s=www.my___site.com&dt=1381663474340&u_tz=-420&u_his=1&u_h=768&u_w=1024&frm=0"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "OptimizeImages": {
    "localizedRuleName": "Optimize images",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your images are optimized. Learn more about optimizing images.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/OptimizeImages"
        }
       ]
      }
     }
    ]
   },
   "PrioritizeVisibleContent": {
    "localizedRuleName": "Prioritize visible content",
    "ruleScore": 100,
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "You have the above-the-fold content properly prioritized. Learn more about prioritizing visible content.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/PrioritizeVisibleContent"
        }
       ]
      }
     }
    ]
   }
  }
 },
 "version": {
  "major": 1,
  "minor": 14
 }
}

谢谢你帮助我

4

1 回答 1

1

它是 JSON 格式的,因此您需要先获取json_decode它。

$json = json_decode($data);
echo $json->score;
于 2013-10-13T11:34:00.270 回答