Last active
January 12, 2019 07:31
-
-
Save k4zuki02h4t4/3d57599381db9c99feebd28f4862b23e to your computer and use it in GitHub Desktop.
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 | |
define( 'PAGESPEED_INSIGTHS_ENDPOINT', 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?' ); | |
define( 'PAGESPEED_INSIGTHS_LOCALE', 'ja' ); | |
define( 'PAGESPEED_INSIGTHS_API_KEY', 'your_api_key' ); | |
$score = 0; | |
/** | |
* @see https://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed | |
*/ | |
$parameter = array( | |
'url' => 'https://example.com', | |
'category' => 'performance', | |
'locale' => PAGESPEED_INSIGTHS_LOCALE, | |
'strategy' => 'mobile', | |
'key' => PAGESPEED_INSIGTHS_API_KEY, | |
); | |
$pagespeed = PAGESPEED_INSIGTHS_ENDPOINT . http_build_query( $parameter ); | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $pagespeed ); | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_FORBID_REUSE, true ); | |
curl_setopt( $ch, CURLOPT_FRESH_CONNECT, true ); | |
$response = curl_exec( $ch ); | |
$httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); | |
curl_close( $ch ); | |
if ( $httpcode === 200 ) { | |
$response = json_decode( $response ); | |
if ( isset( $response->lighthouseResult->categories->performance->score ) ) { | |
$score = $response->lighthouseResult->categories->performance->score * 100; | |
} | |
} | |
var_dump( $score ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment