The SERP API enables developers to scrape data from search engine results and incorporate it into their online applications. Based on the query supplied in the API settings, it returns real-time results from the search engine (Google, Bing, etc.). When you wish to integrate the Search Engine Results Page (SERP) on your website, the SERP API comes in handy.
There are a number of SERP APIs available for retrieving search engine results, with Serpstack being one of the top free SERP APIs. The Serpstack API allows you to scrape Google SERP data in real time (including image and video search results). Serpstack offers a simple REST API that returns responses in JSON or CSV, allowing it to be used with any programming language (PHP, Python, Ruby, Nodejs, etc.). We’ll teach you how to use PHP to combine a Search Engine Results Page with the Serpstack SERP API in this tutorial.
To integrate SERP API with serpstack in PHP, follow the simple steps below.
1. Obtain an API Access Key Create a serpstack account before getting started.
2. The API key can be found in the dashboard’s Your API Access Key section.
Configuring the API
To authenticate and use the serpstack API, you’ll need the Access Key.
To pass needed params via the serpstack API, use the http build query() function to build the query string.
In the access key parameter, enter the API Access Key.
In the query parameter, enter the search terms.
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => 'codexworld',
]);
Send an HTTP GET request.
To get the search results, use cURL in PHP to make an HTTP GET request to the SERP API.
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.serpstack.com/search', $queryString);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL resource
curl_close($ch);
HTTPS Encryption: To perform a secure API request, start the URL with a https and employ HTTPS (SSL) encryption.
https://api.serpstack.com/search
Data from the SERPs
The Google search results will be delivered in JSON format after a successful API request. To convert a JSON response to an array in PHP, use the json decode() function.
// Convert API json response to array
$api_result = json_decode($api_response, true);
Example Code for Using the Serpstack API to Get SERP Data
The whole PHP code for getting Google search results is provided below.
<?php
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => 'codexworld',
]);
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.serpstack.com/search', $queryString);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL resource
curl_close($ch);
// Convert API json response to array
$api_result = json_decode($api_response, true);
?>