Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google PageSpeed Insights API Screenshot to file

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);
like image 333
James Avatar asked Apr 02 '15 05:04

James


People also ask

How do I export PageSpeed Insights?

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.

How do I get PageSpeed API?

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.

Does PageSpeed Insights use Lighthouse?

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.


1 Answers

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);
like image 129
James Avatar answered Oct 19 '22 13:10

James