Skip to content

Instantly share code, notes, and snippets.

@ko31
Created February 7, 2016 23:45
Show Gist options
  • Save ko31/6928a9e0b463f0619180 to your computer and use it in GitHub Desktop.
Save ko31/6928a9e0b463f0619180 to your computer and use it in GitHub Desktop.
【PageSpeed Insights】APIリクエストのサンプル
<?php
// APIキー
$key = "Your API Key";
// mobile or desktop
$strategy = "mobile";
// URLリスト読み込み
$lines = file('url.txt', FILE_IGNORE_NEW_LINES);
// URLの分だけAPIリクエストして解析
foreach ($lines as $url) {
$api = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=".$url."&strategy=".$strategy."&key=".$key;
$json = file_get_contents($api);
$data = json_decode($json);
if ($data->responseCode == 200) {
$score = $data->ruleGroups->SPEED->score;
} else {
$score = "error";
}
echo $url.",".$score."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment