moddix / serpapi
SerpApi PHP client library
1.0.0
2026-01-12 17:03 UTC
Requires
- php: >=8.2
- ext-json: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2026-03-12 17:33:09 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 codeDto/— Data Transfer Objects for search optionsEnum/— Enumerations for search parametersException/— Custom exception classes
tests/— PHPUnit tests
Running Tests
Run all tests using PHPUnit:
./vendor/bin/phpunit tests
License
MIT License.