moddix/serpapi

SerpApi PHP client library

Installs: 23

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/moddix/serpapi

1.0.0 2026-01-12 17:03 UTC

This package is auto-updated.

Last update: 2026-01-12 17:07:35 UTC


README

A flexible PHP client for interacting with the SerpApi search API. Supports Google and Bing search engines, with extensible options and robust error handling.

Requirements

  • PHP 8.2 or higher
  • PSR-18 compatible HTTP client (e.g., Symfony HttpClient)
  • PSR-17 compatible request factory (e.g., Nyholm/psr7)

Features

  • Easy integration with SerpApi for Google and Bing search
  • Strongly-typed search options (DTOs) for each engine
  • Extensible architecture for adding new search engines
  • Comprehensive exception handling (API, network, validation, server errors)
  • PSR-18 HTTP client and PSR-17 request factory support

Installation

Install via Composer:

composer require moddix/serpapi

Usage Example

use Moddix\SerpApi\GoogleSearchClient;
use Moddix\SerpApi\Dto\GoogleSearchOptions;
use Moddix\SerpApi\Dto\SerpApiOptions;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;

$apiKey = 'your_serpapi_key';
$httpClient = new Psr18Client();
$requestFactory = new Psr17Factory();
$googleOptions = new GoogleSearchOptions(gl: 'us', hl: 'en');
$serpApiOptions = new SerpApiOptions(no_cache: true);

$client = new GoogleSearchClient(
    $apiKey,
    $httpClient,
    $requestFactory,
    $googleOptions,
    $serpApiOptions
);

try {
    $results = $client->search('pizza');
    print_r($results);
} catch (\Moddix\SerpApi\Exception\ApiException $e) {
    // Handle API error
} catch (\Moddix\SerpApi\Exception\NetworkException $e) {
    // Handle network error
}

Project Structure

  • src/ — Main source code
    • Dto/ — Data Transfer Objects for search options
    • Enum/ — Enumerations for search parameters
    • Exception/ — Custom exception classes
  • tests/ — PHPUnit tests

Running Tests

Run all tests using PHPUnit:

./vendor/bin/phpunit tests

License

MIT License.