如何通过PHP在localhost中使用Page Speed Insight API?

问题描述

嗨,我正在尝试使用PHP中的Google Speed Insights API来获取页面速度分析

$api = 'API KEY';
$url = 'https://www.stackoverflow.com/';
$url_sh = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=".$url."&key=".$api;

$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_URL,$url_sh);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result,true));

但是得到NULL值

我也尝试了json_decode,但是得到了相同的输出

解决方法

很久以前不推荐使用版本1的api,最新版本是版本5,因此您只需将网址更改为

https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=".$url."&key=".$api;

您可以read the getting started documents here