【问题标题】:Google PageSpeed Insights API integrationGoogle PageSpeed Insights API 集成
【发布时间】:2019-07-09 07:07:17
【问题描述】:

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

【问题讨论】:

    标签: wordpress google-pagespeed-insights-api


    【解决方案1】:

    是的,这是可能的。您可以 fetch() 这个端点使用 URL 编码: 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
        }
    });
    

    API 有速率限制,因此您可能希望将结果缓存一段时间(我为此使用 WordPress 瞬态)。

    【讨论】:

    • JSON 结果中时间的实际值在哪里?我在文档中找不到它?
    猜你喜欢
    • 2016-12-24
    • 2015-03-20
    • 2019-05-07
    • 2016-09-04
    • 2021-07-17
    • 1970-01-01
    • 2023-02-09
    • 2016-12-05
    • 1970-01-01
    相关资源
    最近更新 更多