Skip to content

Instantly share code, notes, and snippets.

@totya24
Created July 13, 2016 08:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save totya24/490d44ca8fe6dd88452a0153cc9c3fd0 to your computer and use it in GitHub Desktop.
Save totya24/490d44ca8fe6dd88452a0153cc9c3fd0 to your computer and use it in GitHub Desktop.
Google Page Insight
<!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>
@migors
Copy link

migors commented Dec 18, 2016

Could you please suggest where and how i should put api key? I am trying to post on website page speed insights test. Only code sample i could get to work was yours ;). just need to know where to put api key. $myKEY = "******************************";

I am not a programmer i just try to learn. Need a little help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment