SERP (Search Engine Results Pages) Data is the search result pages data that serves by the Search Engines like Google, Bing or Baidu.
Most of the businesses always looking for the accurate search result data. But it’s not an easy task to get real time accurate search result data.
The search engines constantly keeps on changing their SERP structure and algorithm. The things that’s working right now will soon becomes the thing of the past. So it’s very important to use updated method to scrape accurate SERP data.
So if you’re running a business or developer and looking for the solution to scrape SERP data, then you’re here at the right place. In this tutorial you will learn how to scrape SERP data using SERP API with PHP.
Also, read:
- Scraping Review Data using ReviewAPI with PHP
- Scrape SERP Data with SerpStack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Search Result Scraping with Zenserp API using PHP
- Web Scraping with Zenscrape API using PHP
- Web Scraping with Real-Time ScrapeStack REST API using PHP
SerpApi is a real time Google Search API that get Google results from anywhere in the world with location parameter and returns results into JSON data.
Here are the major features of SerpApi:
- No waiting, real-time accurate results
- Automatically solves CAPTCHAs
- Ability to get results from any location
- JSON Results like maps, shopping and knowledge graph
- Security with Legal US Shield
So let’s start integrating the SerpApi with PHP.
Step1: Get API Secret Key
First we will register to SerpAPI to get the API Secret Key. We will use the secret key to access the API.
https://serpapi.com/search?api_key = API_SECRET_KEY
Step2: Get Google Search Results PHP API
We will get the Google Search Results PHP API using composer.
$ composer require serpapi/google-search-results-php
Step3: Use Google Search Results PHP API
After getting the API files through composer, we need to load the dependency in your script to use API classes and methods.
<?php require __DIR__ . '/vendor/autoload.php'; ?>
or you can clone the repository and include the following classes in your script.
<?php require 'path/to/google-search-results'; ?>
Step4: Create Search Query
We will create search query with search parameters to get desired search result data. We will also pass API secret key with search query to access the API.
<?php $apiSecretKey = 'API_SECRET_KEY'; $searchQuery = [ "engine" => "google", "q" => "Coffee", "location" => "Austin, Texas, United States", "google_domain" => "google.com", "gl" => "us", "hl" => "en", "api_key" => $apiSecretKey ]; ?>
Step5: Complete Code
Here is the complete code to get Google search result data using SerpApi. We will make HTTP GET request using class GoogleSearchResults() by passing search query with parameters and get the results JSON data.
<?php require 'path/to/google_search_results'; $apiSecretKey = 'API_SECRET_KEY'; $searchQuery = [ "engine" => "google", "q" => "Coffee", "location" => "Austin, Texas, United States", "google_domain" => "google.com", "gl" => "us", "hl" => "en", "api_key" => $apiSecretKey ]; $client = new GoogleSearchResults(); $results = $client->json($searchQuery); print_r($results); ?>
Following is the short sample SERP JSON data return by above code.
{ "search_metadata": { "id": "5cc470041befcd91e24b126a", "status": "Success", "created_at": "2019-04-27 15:06:44 UTC", "processed_at": "2019-04-27 15:06:44 UTC", "google_url": "https://www.google.com/search?q=Coffee&oq= Coffee&uule=w+CAIQICIaQXVzdGluLFRleGFzLFVuaXRlZCBTdGF0ZXM&hl=en &gl=us&sourceid=chrome&ie=UTF-8", "total_time_taken": 2.59 }, "search_parameters": { "q": "Coffee", "location_requested": "Austin, Texas, United States", "location_used": "Austin,Texas,United States", "google_domain": "google.com", "hl": "en", "gl": "us", "device": "desktop" }, "search_information": { "organic_results_state": "Results for exact spelling", "total_results": 3240000000, "time_taken_displayed": 0.54, "query_displayed": "Coffee" }, "local_map": { "link": "https://www.google.com/search?gl=us&hl=en&q= Coffee&npsic=0&rflfq=1&rldoc=1&rlha=0 &rllag=30267374,-97742553,138&tbm=lcl &sa=X&ved=2ahUKEwijjeaarNrlAhUPKqwKHc8DAh8QtgN6BAgLEAQ", "image": "data:image/gif;base64,R0lGODlhAQABAIAAAP//// ///yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "gps_coordinates": { "latitude": 30.267374, "longitude": -97.742553, "altitude": 138 } }, "local_results": { "more_locations_link": "https://www.google.com/search?gl=us&hl=en&q= Coffee&npsic=0&rflfq=1&rldoc=1&rlha=0&rllag=30267374,-97742553,138 &tbm=lcl&sa=X&ved=2ahUKEwijjeaarNrlAhUPKqwKHc8DAh8QjGp6BAgLEEc", "places": [ { "position": 1, "title": "Houndstooth Coffee", "place_id": "11265938073076301333", "lsig": "AB86z5Vdw6C2pJpM0xQ6JUx2KONU", "place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google &gl=us&google_domain=google.com&hl=en&location_requested =Austin%2C+Texas%2C+United+States&lsig= AB86z5Vdw6C2pJpM0xQ6JUx2KONU&ludocid=11265938073076301333&q=Coffee&tbm=lcl", "rating": 4.6, "reviews": 625, "price": "$$", "type": "Coffee shop", "hours": "Closed ⋅ Opens 6:30AM", "address": "401 Congress Ave #100c", "description": "Cozy hangout for carefully sourced brews", "thumbnail": "data:image/gif;base64,R0lGODlhAQABAIAAAP ///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "gps_coordinates": { "latitude": 30.2661803, "longitude": -97.74290409999999 } }, { "position": 2, "title": "The Hideout Coffee House", "place_id": "15498522356495312950", "lsig": "AB86z5WSxdnDKVF_iLXNN6Lg0UQ5", "place_id_search": "https://serpapi.com/search.json?device=desktop &engine=google&gl=us&google_domain=google.com &hl=en&location_requested=Austin%2C+Texas%2C+United+States &lsig=AB86z5WSxdnDKVF_iLXNN6Lg0UQ5 &ludocid=15498522356495312950&q=Coffee&tbm=lcl", "rating": 4.4, "reviews": 296, "price": "$", "type": "Coffee shop", "hours": "Closed ⋅ Opens 7AM", "address": "617 Congress Ave", "description": "Area institution with a theater upstairs", "thumbnail": "data:image/gif;base64,R0lGODlhAQABAIAAAP ///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "gps_coordinates": { "latitude": 30.268568999999996, "longitude": -97.74215099999999 } } } ] }
Step6: Conclusion
Here in this tutorial we have explained how to integrate SerpApi with PHP to scrape the Google search result data. You can check the more advanced options by going through the documentation to use in your script.
You may also like:
- Get Geolocation Data with IPWHOIS.IO API
- IP Geolocation API Integration with PHP
- Geocoding using Positionstack API with PHP
- Get Real-time Flight Data using Aviationstack API with PHP
- User Agent Lookup using Userstack API with PHP
- Reverse IP Lookup using ipapi with PHP
- Weather Data with Weatherstack API using PHP
- Website Visitors IP Lookup with IPStack API using PHP