Playground Sign in Start free

Scraping Google Search Results with PHP

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

<?php
function scrapeGoogleSearch($query) {
    $apiKey = getenv('UJEEBU_API_KEY') ?: 'YOUR_API_KEY';

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => 'https://api.ujeebu.com/serp?' . http_build_query([
            'search' => $query,
            'search_type' => 'search',  // Web search
            'lang' => 'en',
            'location' => 'US',
            'device' => 'desktop',
            'results_count' => 20
        ]),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'ApiKey: ' . $apiKey
        ]
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode !== 200) {
        throw new Exception("SERP scraping failed: HTTP $httpCode");
    }

    return json_decode($response, true);
}

// Usage
$results = scrapeGoogleSearch('web scraping tools');

// Display organic results
if (isset($results['organic_results'])) {
    echo "Found " . count($results['organic_results']) . " results:\n\n";
    foreach ($results['organic_results'] as $idx => $result) {
        echo ($idx + 1) . ". " . ($result['title'] ?? 'N/A') . "\n";
        echo "   URL: " . ($result['link'] ?? 'N/A') . "\n";
        echo "   " . ($result['snippet'] ?? '') . "\n\n";
    }
}
?>

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:

1. Web Search (Default)

PHP
<?php
$params = [
    'search' => 'web scraping',
    'search_type' => 'search',  // Web search
    'results_count' => 10
];
?>

2. News Search

PHP
<?php
$params = [
    'search' => 'technology news',
    'search_type' => 'news',  // News search
    'results_count' => 20
];
?>

3. Image Search

PHP
<?php
$params = [
    'search' => 'sunset photos',
    'search_type' => 'images',  // Image search
    'results_count' => 50
];
?>

4. Video Search

PHP
<?php
$params = [
    'search' => 'tutorial videos',
    'search_type' => 'videos',  // Video search
    'results_count' => 20
];
?>

5. Maps/Local Search

PHP
<?php
$params = [
    'search' => 'restaurants near me',
    'search_type' => 'maps',  // Maps/Local search
    'location' => 'US',
    '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

PHP
<?php
class RankTracker {
    private $apiKey;
    private $targetDomain;
    
    public function __construct($apiKey, $targetDomain) {
        $this->apiKey = $apiKey;
        $this->targetDomain = $targetDomain;
    }
    
    public function checkRank($keyword) {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => 'https://api.ujeebu.com/serp?' . http_build_query([
                'search' => $keyword,
                'search_type' => 'search',
                'lang' => 'en',
                'location' => 'US',
                'results_count' => 100  // Check top 100
            ]),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ['ApiKey: ' . $this->apiKey]
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode !== 200) {
            return null;
        }

        $data = json_decode($response, true);
        $results = $data['organic_results'] ?? [];

        // Find position of target domain
        foreach ($results as $result) {
            $link = $result['link'] ?? '';
            if (strpos($link, $this->targetDomain) !== false) {
                return [
                    'keyword' => $keyword,
                    'position' => $result['position'] ?? null,
                    'url' => $link,
                    'title' => $result['title'] ?? '',
                    'date' => date('Y-m-d H:i:s')
                ];
            }
        }

        return [
            'keyword' => $keyword,
            'position' => null,  // Not in top 100
            'url' => null,
            'title' => null,
            'date' => date('Y-m-d H:i:s')
        ];
    }
    
    public function trackMultipleKeywords($keywords) {
        $results = [];
        foreach ($keywords as $keyword) {
            $results[] = $this->checkRank($keyword);
            sleep(2);  // Rate limiting
        }
        return $results;
    }
}

// Usage
$tracker = new RankTracker(
    getenv('UJEEBU_API_KEY') ?: 'YOUR_API_KEY',
    'example.com'  // Your domain
);

$keywords = ['web scraping', 'data extraction', 'web scraping tools'];
$rankings = $tracker->trackMultipleKeywords($keywords);

foreach ($rankings as $ranking) {
    if ($ranking['position']) {
        echo "Keyword: {$ranking['keyword']} - Position: {$ranking['position']}\n";
    } else {
        echo "Keyword: {$ranking['keyword']} - Not in top 100\n";
    }
}
?>
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.