The SERP API allows the developer to scrape data from the search engine result and use the search results in the web application. It returns realtime results from the search engine (Google, Bing, etc.) based on the query specified in the API settings. The SERP API is very useful when you want to integrate the Search Engine Results Page (SERP) on the website.
There are various SERP API available to fetch the search engine results, Serpstack is one of the best free SERP API among them. Serpstack API enables you to scrape SERP data (including image or video search result) from Google in realtime. Serpstack provides easy-to-use REST API that returns response in JSON or CSV, it helps to use this API with any programming languages (PHP, Python, Ruby, Nodejs, etc.). In this tutorial, we will show you how to integrate Search Engine Results Page with Serpstack SERP API using PHP.
Follow the below simple steps to integrate SERP API with serpstack in PHP.
1. Before getting started, create an account on serpstack.
2. In the dashboard, you will get the API key under the Your API Access Key.
The Access Key is required to authenticate and access the serpstack API.
http_build_query()
function to pass required params in the serpstack API.access_key
parameter.query
parameter.$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => 'codexworld',
]);
To fetch the search data, call SERP API via HTTP GET request using cURL in PHP.
// 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 make secure API request use HTTPS (SSL) encryption by calling API URL begins with an https
.
https://api.serpstack.com/search
After a successful API request, the Google search results will be returned as JSON format. Use json_decode()
function to convert the JSON response to array in PHP.
// Convert API json response to array
$api_result = json_decode($api_response, true);
The complete code is given below to get Google search results using PHP.
<?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);
?>
The serpstack API is free to use, there also premium plans are available for advanced uses. In the example code, we have used some required parameters on API call. Various configuration options are available in serpstack API, you can use these to customize the search result. For a complete reference, see the documentation of serpstack API.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request