我正在尝试将wordpress Insights API集成到我的PageSpeed网站中,这样当任何客户到来时,他都可以使用页面速度洞察力来检查他的网站速度。基本上我想放一个带有按钮的文本框(像这个https://developers.google.com/speed/pagespeed/insights/ ),它使用谷歌页面速度洞察应用程序接口或函数,并在我的网站上显示速度结果...有可能吗?如果是,我该怎么做?
发布于 2019-02-21 04:30:23
是的,这是可能的。您可以使用URL fetch()
:https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=
对此端点进行编码
编辑:当然,你也可以使用AJAX来进行CURL (或者在页面加载时使用CURL )。
下面是文档:https://developers.google.com/speed/docs/insights/v5/get-started
我在不需要API密钥的情况下实现了这一点,但您的使用里程可能会有所不同。
下面是一些示例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
}
});
该应用编程接口是有速率限制的,因此您可能希望在一段时间内缓存结果(为此,我使用WordPress瞬态)。
https://stackoverflow.com/questions/54704241
复制相似问题