简体   繁体   English

Google PageSpeed Insights API 集成

[英]Google PageSpeed Insights API integration

I am trying to integrate PageSpeed Insights API in my wordpress website, so when ever customer comes, he can check his website speed using pagespeed insight.我正在尝试将 PageSpeed Insights API 集成到我的 wordpress 网站中,因此每当客户来访时,他都可以使用 pagespeed 洞察来检查他的网站速度。 Basically i want to put a textbox with button (like this https://developers.google.com/speed/pagespeed/insights/ ) which uses google page speed insight api or function and display speed result in my website.... Is it possible?基本上我想放一个带有按钮的文本框(比如这个https://developers.google.com/speed/pagespeed/insights/ )它使用谷歌页面速度洞察 api 或函数并在我的网站上显示速度结果......是有可能吗? if yes how can i do this?如果是,我该怎么做?

Yes this is possible.是的,这是可能的。 You can fetch() this endpoint with the URL encoded: https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=您可以使用编码的 URL fetch()此端点: https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url= : https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url= url https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=

Edit: Of course, you could also AJAX to CURL too (or even CURL on pageload).编辑:当然,您也可以 AJAX 到 CURL(甚至页面加载时的 CURL)。

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

I have implemented this without needing an API key, but your mileage may vary.我已经在不需要 API 密钥的情况下实现了这一点,但您的里程可能会有所不同。

Here is some sample JavaScript:下面是一些示例 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). API 的速率有限,因此您可能希望将结果缓存一段时间(为此我使用 WordPress 瞬态)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM