-
-
Save loughlincodes/e896a011cf5722d562a218bf82737e04 to your computer and use it in GitHub Desktop.
A PHP script to look up and parse google pagespeed results.
This file contains 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
<?php | |
$url = 'URL GOES HERE'; | |
$key = 'KEY GOES HERE'; | |
// View https://developers.google.com/speed/docs/insights/v1/getting_started#before_starting to get a key | |
$data = json_decode(file_get_contents("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=$url&key=$key")); | |
$dat = $data->formattedResults->ruleResults; | |
foreach($dat as $d) { | |
$name = $d->localizedRuleName; | |
$score = $d->ruleScore; | |
print "\nTest: " . $name . "\n"; | |
print "Score: " . $score . "\n"; | |
if ($score != 100) { | |
if (isset($d->urlBlocks[0]->header->args)) { | |
$advice_header = replace_placeholders($d->urlBlocks[0]->header->format, $d->urlBlocks[0]->header->args); | |
} | |
else { | |
$advice_header = $d->urlBlocks[0]->header->format; | |
} | |
print "Advice: " . $advice_header . "\n"; | |
foreach ($d->urlBlocks[0]->urls as $url) { | |
$advice = replace_placeholders($url->result->format, $url->result->args); | |
print $advice . "\n"; | |
} | |
} | |
}; | |
function replace_placeholders($format, $args) { | |
$i = 1; | |
foreach ($args as $arg) { | |
$format = str_replace("\$" . $i, "$arg->value", $format); | |
$i++; | |
} | |
return $format; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment