Do you know, Google provides a free PageSpeed Insights API to analyze websites? It gives free access to performance monitoring of web pages and returns data with suggestions for how to improve.
Using this API, we can build any system that analyzes the given URLs and displays the grading of the websites. It provides a lot of information (more than you expect 😉 ). So, we need to extract the data that we need from the response.
In this article, we will integrate Google PageSpeed Insights API in PHP and extract some of the data for testing. Without delay, let’s write some code.
First of all, we need get the API URL where we will send the data using GET method. We can to pass different optional parameters according to our need. We will call the below API URL.
We need to pass url and other optional parameters. Here,
url means the URL of website we need to analyze. The list of accepted parameters are as follows:
urlis the website url we need to analyze
categoryindicates the information of categories we need. The accepted values are
strategyis the medium of analysis. Accepted value are
- You can also set other parameters such as
Note: We can pass API key to execute multiple requests at once.
Let’s execute the code using CURL. In the below code, we simply set the new URL with parameters and send request to the API route. The response is stored in
The response from the API is pretty large. We need to extract the information we need. For this, we will first convert the
json object to array.
Most of our data is stored in
categoryGroups keys. So, we can get the desired values giving the correct key path.
To store all our required information, let’s create an array named
$response. Below is a complete sample of how to access and store API response information.