Scrape SERP Data with SerpStack API using PHP

SERP (Search Engine Results Pages) data is the web pages that are served to users when they search for anything using search engine, such as Google, Bing etc. The users generally enters their search keywords and search engine displays search results web pages.

If a user wants thousands of search results based on specific terms and phrases known as keywords, then the user needs to done it manually or need to create web scraper. But it’s very time consuming and inconsistent.

So if you’re looking for solution to get unlimited search results (SERP) data, then you’re here at right place. In this tutorial you will learn how to get SERP data using SerpStack API with PHP. The SerpStack API takes simple HTTP GET URL structure and returns search results data into JSON and CSV format. The API can be used with all major search engines to get SERP data.

Also, read:

As we will cover this tutorial with live example to design dynamic pricing page using Bootstrap, PHP and MySQL, so the major files for this example is following.


So let’s proceed to integrate SerpStack API with PHP to get SERP data with example.

Step1: Get API Access Key
First we need to create an account on SerpStack and we will be provided a unique API access_key. We will use this API key to build search query with keywords to pass with HTTP GET request. We will pass access_key and search keywords query to search from Google search engine. You can also pass engine parameter with search engine if wants to search from other than Google.

<?php
$searchQuery = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => 'php tutorial',
]);
?>

Step2: Make HTTP GET Request to get SERP Data
We will make a HTTP GET request with search query to get search results data. The results data will be returned as JSON by default. We can also pass output parameter with value CSV to get search result into CSV format.

<?php
$ch = curl_init(sprintf('%s?%s', 'https://api.serpstack.com/search', $searchQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$searchJsonData = curl_exec($ch);
curl_close($ch);
?>


Step3: Display SERP Data
After getting SERP data in JSON format through HTTP GET request, we will decode the JSON data and display all SERP data.

<?php
$searchResult = json_decode($searchJsonData, true);
echo "Total Search results: ", $searchResult['search_information']['total_results'], PHP_EOL;
foreach ($searchResult['organic_results'] as $number => $result) {
echo "{$number}. {$result['title']}", PHP_EOL;
}
?>

Step4: Complete Code to SERP Data
Here is the complete PHP code to get SERP data and print the total number of search results and organic search results.

<?php
$searchQuery = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => 'php tutorial',
]);
$ch = curl_init(sprintf('%s?%s', 'https://api.serpstack.com/search', $searchQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$searchJsonData = curl_exec($ch);
curl_close($ch);
$searchResult = json_decode($searchJsonData, true);
echo "Total Search results: ", $searchResult['search_information']['total_results'], PHP_EOL;
foreach ($searchResult['organic_results'] as $number => $result) {
echo "{$number}. {$result['title']}", PHP_EOL;
}
?>

Step5: Conclusion
In this tutorial we have explained how to get SERP data with SerpStack API using PHP. You can also gone through documentation to integrate the API in other programming languages like Python, Nodejs, jQuery, Go and Ruby.


You may also like: