以下使用this code from GitHub
(1) 从Google Developers Console 获取您的 Google API 密钥
(2) 自定义以下 php 代码,或以编程方式生成。请注意,在第 19 行,您需要使用自己的 API 密钥并输入自己的 URL
<?php
/**
* @param $url
* @param $apiKey
* @return mixed
*/
function isMobileReady($url, $apiKey)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key='.$apiKey.'&url='.$url.'&strategy=mobile',
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
//result as an array look for ["pass"]=> bool(true) } or false
$result = json_decode(isMobileReady('https://www.panchroma.ca/en/', 'AIzaSyDSrus1NcAIFXOWQjoAgwEOVChX_KEnhg_dummy_api_key'), true);
var_dump($result);
(3) pagespeed 结果被转储到屏幕上,我认为移动友好测试结果最重要的信息靠近顶部。寻找
{ ["USABILITY"]=> array(2) { ["score"]=> int(98) ["pass"]=> bool(true) } }
分数是您的 Google PageSpeed 分数 [0-100] 并且“通过”布尔值是 true 或 false,true 相当于通过了Mobile Friendly Test
满分to this author
===
更新
针对如何测试多个 URL 的问题,我有以下一个建议。
和here's the live result。
这会将很多东西转储到页面,搜索“分数”以查看 3 个示例 URL 的移动设备友好测试结果
<?php
/**
* @param $url
* @param $apiKey
* @return mixed
*/
function isMobileReady($url, $apiKey)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key='.$apiKey.'&url='.$url.'&strategy=mobile',
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
$websites = array( "https://www.panchroma.ca/en/", "https://www.google.ca","https://www.youtube.com" );
foreach($websites as $website) {
$result = json_decode(isMobileReady($website, 'AIzaSyDSrus1NcAIFXOWQjoAgwEOVChX_KEnhg_dummy_api_key'), true);
var_dump($result);
}
?>
希望这会有所帮助!