1

我正在尝试将 PageSpeed Insights API 集成到我的 wordpress 网站中,因此当客户来到时,他可以使用 pagespeed 洞察力检查他的网站速度。基本上我想放一个带有按钮的文本框(比如这个https://developers.google.com/speed/pagespeed/insights/),它使用谷歌页面速度洞察 api 或函数并在我的网站中显示速度结果......是有可能吗?如果是的话,我该怎么做?

4

1 回答 1

-2

Yes this is possible. You can fetch() this endpoint with the URL encoded: https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=

Edit: Of course, you could also AJAX to CURL too (or even CURL on pageload).

Here is the documentation: https://developers.google.com/speed/docs/insights/v5/get-started

I have implemented this without needing an API key, but your mileage may vary.

Here is some sample JavaScript:

fetch('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' + encodeURIComponent('https://example.com/')).then(function(response){
    return response.json(); //This returns a promise
}).then(function(json){
    if ( json && json.captchaResult === 'CAPTCHA_NOT_NEEDED' ){
        //Output the data you want to display on the front-end from the json
    }
});

The API is rate limited, so you'll probably want to cache the results for a certain period of time (I use WordPress transients for this).

于 2019-02-20T20:30:23.147 回答