Skip to main content

Scrape Search Result with Zenserp API using PHP

Search Engine Result Pages (known as SERP) are the search result pages presented by search engines. Generally, we need to go through the search result pages to get the relevant search result data. But sometimes we needs to collect large SERPs data. In that scenario, we need to develop a system to scrape the SERPs data programmatically. But it’s always a challenge for the developers to develop their own scrapping system as it involves captcha-solving, managing proxy servers and parsing of the constantly changing markup of the search results.

So if you’re a company owner or a developer and looking for the solution to get SERP data, then you’re here at the right place. In this tutorial, you will learn how to get scrape SERP data using Zenserp API with PHP.

Zenserp API is an easy to use SERP API that returns highly accurate SERPs data. You don’t need to worry about handling captcha, proxy servers etc. Just use the API interface directly from Zenserp API to scrape the SERP data from any website. You can scrape the SERP data into your desired file format such as raw HTML or CSV data.


Also, read:

So let’s proceed to integrate Zenserp API with PHP to scrape the SERP data with an example.

Step1: Register with Zenserp API

First we need to Register to the Zenserp API to create an account to get the API Access Key to use with API request.

We will use the API key while making HTTP request to Zenserp API like below.

https://app.zenserp.com/api/v2/search?apikey=APIKEY

Step2: Build Zenserp API Search Query

We will need to create search query with the required parameters to scrape search results data. We will use q parameter with keywords to search for data. We will use the location parameter for the Geo-location used in the search query. We will also use search_engine parameter with the Search engine. We can also use other parameters like gl for country code, hl for search interface language to get more filtered SERP data. For more detail, you can checkout documentation to use more options in your search query.

<?php 

$searchQuery = [
	'q' => 'Pied Piper',
	'location' => 'United States',
	'search_engine' => 'google.com',
	'gl' => 'US',
	'hl' => 'en'
];

?>

Step3: Make Zenserp API Request

We will make Zenserp API HTTP request to get the SERP data with PHP Curl library. We will pass the created search query with parameters to scrape the SERP data. We also need to pass the API Key for the API authentication to use the API. The successful API request will return SERP data into raw HTML or CSV format. We will store the response data into $responseData variable.

<?php 

$apiAccessKey = 'YOUR_API_KEY';

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_URL, "https://app.zenserp.com/api/v2/search?" . http_build_query($searchQuery));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"apikey: $apiAccessKey",
));

$responseData = curl_exec($ch);
curl_close($ch);

?>

Step4: Encode Response Data

We will use the response data to encode into JSON data using PHP json_decode() function and store the result JSON data into $result variable for further use.

<?php

$result = json_decode($responseData);
var_dump($result);

?>

Step5: Complete Code to get SERP Data with Zenserp API

Below is the complete example code to make HTTP request to Zenserp API with search query to get SERP data from Google search engine.

<?php

$apiAccessKey = 'YOUR_API_KEY';

$searchQuery = [
	'q' => 'Pied Piper',
	'location' => 'United States',
	'search_engine' => 'google.com',
	'gl' => 'US',
	'hl' => 'en'
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_URL, "https://app.zenserp.com/api/v2/search?" . http_build_query($searchQuery));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"apikey: $apiAccessKey",
));

$responseData = curl_exec($ch);
curl_close($ch);

?>

Below is the response JSON data for the keyword “Pied Piper” after running above example code.

{
	"query": {
		"q": "Pied Piper",
		"hl": "English",
		"gl": "US",
		"location": "United States",
		"domain": "google.com",
		"device": "desktop",
		"url": "https://google.com/search?q=Pied+Piper&uule=w+CAIQICINVW5pdGVkIFN0YXRlcw&gl=US"
	},
	"organic": [
		{
			"position": 1,
			"title": "Pied Piper of Hamelin - Wikipedia",
			"url": "https://en.wikipedia.org/wiki/Pied_Piper_of_Hamelin",
			"destination": "https://en.wikipedia.org/wiki/Pied_Piper_of_Hamelin",
			"description": "The Pied Piper of Hamelin is the titular character of a legend from the 
			town of Hamelin (Hameln), Lower Saxony, Germany, as well as the title of the fairy tale that ..."
		},
		{
			"position": 2,
			"questions": [
				{
					"question": "What is the meaning of the Pied Piper?"
				},
				{
					"question": "What is the legend of the Pied Piper?"
				},
				{
					"question": "What is the moral of the story pied piper?"
				}
			]
		},
		{
			"position": 3,
			"videos": [
				{
					"title": "Walt Disney's Fables - Pied Piper",
					"url": "https://www.youtube.com/watch?v=UXOCHn7Vfec"
				},
				{
					"title": "The True History of The Pied Piper of Hamelin | Fairy Tales With Jen",
					"url": "https://www.youtube.com/watch?v=obIAwAAcAAg"
				}
			]
		},
		{
			"position": 4,
			"title": "The Chilling True Story Behind the Pied Piper of Hamelin - The Portalist",
			"url": "https://theportalist.com/the-chilling-true-story-behind-the-pied-piper-of-hamelin",
			"destination": "https://theportalist.com › The Chilling True Story Behind the Pied Piper of Hamelin",
			"description": "Aug 2, 2017 - Is there a true story behind the legend of the Pied Piper of Hamelin? 
			The myth may be based on a real-life tragedy."
		},
		{
			"position": 5,
			"localPack": [
				{
					"title": "Pied Piper Pub & Inn and The Cat House Lounge",
					"url": "http://www.cathouselounge.com/",
					"street": "Eureka Springs, AR",
					"stars": 4.3,
					"review": 482,
					"phone": null,
					"description": "Biker-friendly hangout with live music",
					"price": "$$",
					"type": "Pub",
					"extensions": null
				},
				{
					"title": "Pied Piper Pest, Termite & Lawn Service",
					"url": "http://thepiedpiper.biz/",
					"street": "Eufaula, OK",
					"stars": 5,
					"review": 130,
					"phone": "(918) 689-3000",
					"description": "Open ⋅ Closes 11:30PM",
					"price": "",
					"type": "Pest control service",
					"extensions": null
				},
				{
					"title": "The Pied Piper Bar",
					"url": "http://piedpipersf.com/",
					"street": "San Francisco, CA",
					"stars": 4.2,
					"review": 174,
					"phone": null,
					"description": "Posh American eatery in historic hotel",
					"price": "$$",
					"type": "Bar & grill",
					"extensions": null
				}
			],
			"mapUrl": "https://google.com/search?q=Pied+Piper&uule=w+CAIQICINVW5pdGVkIFN0YXRlcw&gl=US/search?
			gl=US&q=Pied+Piper&npsic=0&rflfq=1&rldoc=1&rlha=0&rllag=36538514%2C-108071483%2C1282005&tbm=lcl&sa=X&ved=2ahUKEwjk0MGd9-biAhUSmxQKHSmmDwMQtgN6BAgJEAQ"
		},
		...
		{
			"position": 10,
			"title": "The Disturbing True Story of the Pied Piper of Hamelin | Ancient Origins",
			"url": "https://www.ancient-origins.net/myths-legends/disturbing-true-story-pied-piper-hamelin-001969",
			"destination": "https://www.ancient-origins.net/.../disturbing-true-story-pied-piper-hamelin-001969",
			"description": "Aug 14, 2014 - Many are familiar with the story of the Pied Piper of Hamelin.
			Few realise however, that the story is based on real events, which evolved over ..."
		}
	],
	"related_searches": [
		{
			"title": "pied piper nursery rhyme",
			"url": "https://google.com/search?q=Pied+Piper&uule=w+CAIQICINVW5pdGVkIFN0YXRlcw&
			gl=US/search?gl=US&q=pied+piper+nursery+rhyme&sa=X&ved=2ahUKEwjk0MGd9-biAhUSmxQKHSmmDwMQ1QIoAHoECAsQAQ"
		},
		{
			"title": "pied piper poem",
			"url": "https://google.com/search?q=Pied+Piper&uule=w+CAIQICINVW5pdGVkIFN0YXRlcw&gl=
			US/search?gl=US&q=pied+piper+poem&sa=X&ved=2ahUKEwjk0MGd9-biAhUSmxQKHSmmDwMQ1QIoAXoECAsQAg"
		},
		{
			"title": "pied piper music",
			"url": "https://google.com/search?q=Pied+Piper&uule=w+CAIQICINVW5pdGVkIFN0YXRlcw&gl=
			US/search?gl=US&q=pied+piper+music&sa=X&ved=2ahUKEwjk0MGd9-biAhUSmxQKHSmmDwMQ1QIoB3oECAsQCA"
		}
	],
	"number_of_results": 16200000
}

Step6: Conclusion

In this tutorial you have learned how to integrate Zenserp API with PHP to get the SERP data. You can use more advance options to make your search more accurate by going through the documentation. You can also integrate the Zenserp API with other programming languages like Python, Ruby, Go, Nodejs etc. get the SERPs data.

You may also like: