Skip to content

Instantly share code, notes, and snippets.

@typhonius
Created August 18, 2013 03:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save typhonius/6259822 to your computer and use it in GitHub Desktop.
Save typhonius/6259822 to your computer and use it in GitHub Desktop.
A PHP script to look up and parse google pagespeed results.
<?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;
}
@nejib1
Copy link

nejib1 commented Jun 27, 2020

this is deprecated

@phpsubbarao
Copy link

this is deprecated

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