Playground Sign in Start free

Scraping Google Search Results with PHP

Download runnable code Sign in to prefill your key
Using the SERP API Download
Different Search Types Download
Building a Rank Tracker Download
1

Overview

Google Search Engine Results Pages (SERPs) contain valuable data for SEO professionals, marketers, and researchers. Scrape organic results, ads, featured snippets, and more using the Ujeebu SERP API.

What You Can Extract

Organic search results
Featured snippets
People Also Ask
Sponsored ads
Image & video results
Local pack results
2

Using the SERP API

The easiest way to scrape Google is using Ujeebu's dedicated SERP API endpoint.

Basic SERP Scraping

curl -X GET "https://api.ujeebu.com/serp" \
  -H "ApiKey: YOUR_API_KEY" \
  -G \
  --data-urlencode "search=web scraping tools" \
  --data-urlencode "search_type=search" \
  --data-urlencode "lang=en" \
  --data-urlencode "location=US" \
  --data-urlencode "results_count=20"

Response Structure

JSON - API Response
{
  "metadata": {
    "google_url": "https://www.google.com/search?gl=US&hl=en&num=20&q=web+scraping+tools",
    "number_of_results": 1234567,
    "query_displayed": "web scraping tools",
    "results_time": "0.52 seconds"
  },
  "organic_results": [
    {
      "position": 1,
      "title": "Result Title",
      "link": "https://example.com",
      "snippet": "Result description text...",
      "displayed_link": "example.com"
    }
  ],
  "related_questions": ["What are web scraping tools?", "Is web scraping legal?"],
  "pagination": {
    "api": {
      "current": "https://api.ujeebu.com/serp?search=web+scraping+tools&...",
      "next": "https://api.ujeebu.com/serp?search=web+scraping+tools&page=2&..."
    }
  }
}
3

Different Search Types

The SERP API supports multiple Google search types:

# The SERP API returns different result types via search_type:
# search (web), news, images, videos, or maps
curl -X GET "https://api.ujeebu.com/serp" \
  -H "ApiKey: YOUR_API_KEY" \
  -G \
  --data-urlencode "search=web scraping" \
  --data-urlencode "search_type=news" \
  --data-urlencode "results_count=10"
4

Building a Rank Tracker

Create a simple rank tracking system to monitor your website's position for target keywords.

Complete Rank Tracker Example

import requests
import time
from datetime import datetime

class RankTracker:
    def __init__(self, api_key, target_domain):
        self.api_key = api_key
        self.target_domain = target_domain

    def check_rank(self, keyword):
        response = requests.get("https://api.ujeebu.com/serp",
            headers={"ApiKey": self.api_key},
            params={
                "search": keyword,
                "search_type": "search",
                "lang": "en",
                "location": "US",
                "results_count": 100  # Check top 100
            })

        if response.status_code != 200:
            return None

        results = response.json().get("organic_results", [])

        # Find the position of the target domain
        for result in results:
            link = result.get("link", "")
            if self.target_domain in link:
                return {
                    "keyword": keyword,
                    "position": result.get("position"),
                    "url": link,
                    "title": result.get("title", ""),
                    "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                }

        return {
            "keyword": keyword,
            "position": None,  # Not in top 100
            "url": None,
            "title": None,
            "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }

    def track_multiple_keywords(self, keywords):
        results = []
        for keyword in keywords:
            results.append(self.check_rank(keyword))
            time.sleep(2)  # Rate limiting
        return results

# Usage
tracker = RankTracker("YOUR_API_KEY", "example.com")

keywords = ["web scraping", "data extraction", "web scraping tools"]
rankings = tracker.track_multiple_keywords(keywords)

for ranking in rankings:
    if ranking["position"]:
        print(f"Keyword: {ranking['keyword']} - Position: {ranking['position']}")
    else:
        print(f"Keyword: {ranking['keyword']} - Not in top 100")
5

Best Practices

01

Use Rate Limiting

Add 2-3 second delays between requests to avoid being blocked. Google has strict rate limits.

Essential
02

Specify Location

Use the location parameter to get geo-targeted results. Important for local SEO tracking.

Recommended
03

Cache Results

Cache SERP data to avoid redundant requests. Search results don't change frequently.

Performance
04

Handle Errors

Always check HTTP status codes and implement retry logic for transient failures.

Production

Ready to Start Scraping Google?

Try the SERP API in our interactive playground or explore the documentation.