-
-
Save kublaj/7ad24168b097b4203be26e88e6ae4413 to your computer and use it in GitHub Desktop.
Google Page Insight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="hu"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Page insights</title> | |
</head> | |
<body> | |
<form method="POST" action=""> | |
<input type="text" name="url" placeholder="url"><input type="submit" value="Get insights" /> | |
</form> | |
<?php | |
function getSslPage($url) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_REFERER, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
if(!empty($_POST['url'])){ | |
/** | |
* to get an API key: https://console.developers.google.com/apis/api/pagespeedonline/overview?project=chrome-startpage | |
*/ | |
$url = rawurlencode($_POST['url']); | |
$mobileData = getSslPage('https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url='. $url .'&filter_third_party_resources=true&locale=hu_HU&screenshot=false&strategy=mobile'); // add API key, if it possible (&key=[API_KEY]) | |
$desktopData = getSslPage('https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url='. $url .'&filter_third_party_resources=true&locale=hu_HU&screenshot=false&strategy=desktop'); // add API key, if it possible (&key=[API_KEY]) | |
$m = json_decode($mobileData,1); | |
$d = json_decode($desktopData,1); | |
$mobileScore = $m['ruleGroups']['SPEED']['score']; | |
$mobileUsability = $m['ruleGroups']['USABILITY']['score']; | |
$desktopScore = $d['ruleGroups']['SPEED']['score']; | |
$mobileData = (int)$m['pageStats']['htmlResponseBytes'] + (int)$m['pageStats']['cssResponseBytes'] + (int)$m['pageStats']['imageResponseBytes'] + (int)$m['pageStats']['javascriptResponseBytes'] + (int)$m['pageStats']['otherResponseBytes']; | |
$desktopData = (int)$d['pageStats']['htmlResponseBytes'] + (int)$d['pageStats']['cssResponseBytes'] + (int)$d['pageStats']['imageResponseBytes'] + (int)$d['pageStats']['javascriptResponseBytes'] + (int)$d['pageStats']['otherResponseBytes']; | |
echo '<table>'; | |
echo '<tr><th align="left">Mobile score</th><td>'. $mobileScore .'</td></tr>'; | |
echo '<tr><th align="left">Mobile usability</th><td>'. $mobileUsability .'</td></tr>'; | |
echo '<tr><th align="left">Desktop score</th><td>'. $desktopScore .'</td></tr>'; | |
echo '<tr><th align="left">Mobile requests size (in bytes)</th><td>'. $mobileData .'</td></tr>'; | |
echo '<tr><th align="left">Desktop requests size (in bytes)</th><td>'. $desktopData .'</td></tr>'; | |
echo '</table>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment