I have been trying to retrieve the screenshot from the API but when I decode the image and save it I get a broken image. Below is the code I am using. I created a tinyurl to a sample file containing a google response if you wish to test it.
$name = 'test';
$result = file_get_contents('http://tinyurl.com/q4smyod');
$result = json_decode($result, true);
$decoded=base64_decode($result['screenshot']['data']);
file_put_contents('img/'.$name.'.jpg',$decoded);
If you open up and run Google PageSpeed Insights through the Audit tab in Chrome Developer Tools, there is a button in the left part of the panel that will allow you to download the report as a JSON file.
Google Pagespeed Insights requires a Google API Key. Keys are free and can be obtained from Google. To get a key, you will need a Google account, such as a Gmail account. If you do not already have a Google account, you can create one here: https://accounts.google.com/SignUp.
Today, we're happy to announce that Pagespeed Insights (PSI) now uses Lighthouse as its analysis engine. This allows developers to get the same performance audits and recommendations everywhere: on the web, from the command line, and in Chrome DevTools.
As mentioned in my comment the issue is being caused by an error on googles encryption when working with the php api. If you are having this issue simply use the following replace functions to fix the encoding.
$data = str_replace('_','/',$result['screenshot']['data']);
$data = str_replace('-','+',$data);
$decoded = base64_decode($data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With