Integrate search data into your PHP application. This library is the official wrapper for SerpApi.
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
This is the new library provided by SerpApi as a replacement for our old library that can be found here. Feel free to contact us in case you need any help: contact@serpapi.com
The full documentation is available here.
The following services are provided:
SerpApi provides a script builder to get you started quickly.
PHP 7.2+ with ext-curl and ext-json must be installed along with composer dependency management tool.
Tested PHP versions:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4
- 8.5
Package available from packagist.
composer require serpapi/serpapi-phprequire 'vendor/autoload.php';
use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'q' => 'coffee',
]);
print_r($results->organic_results);This example runs a search for "coffee" on Google. It returns the results as a PHP object decoded from JSON.
See the playground to generate your own code.
The API key can be set in the constructor or later via setApiKey:
use SerpApi\Client;
// via constructor
$client = new Client('Your Private Key');
// or later
$client = new Client();
$client->setApiKey('Your Private Key');Get your API key from serpapi.com/dashboard.
The default engine is google. You can change it via the second constructor parameter:
use SerpApi\Client;
$client = new Client(getenv('API_KEY'), 'bing');
$results = $client->search(['q' => 'coffee']);The default request timeout is 120 seconds. Customize it via the third constructor parameter:
use SerpApi\Client;
$client = new Client(getenv('API_KEY'), 'google', 30);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google',
'tbm' => 'isch',
'q' => 'coffee',
]);
print_r($results->images_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_scholar',
'q' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_autocomplete',
'q' => 'coffee',
]);
print_r($results->organic_results);- source: tests/ExampleSearchGoogleAutocompleteTest.php see: https://serpapi.com/google-autocomplete-api
use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_shopping',
'q' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_maps',
'q' => 'pizza',
'll' => '@40.7455096,-74.0083012,15.1z',
'type' => 'search',
]);
print_r($results->local_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_jobs',
'q' => 'coffee',
]);
print_r($results->jobs_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_events',
'q' => 'Events in Austin',
'location' => 'Austin, Texas, United States',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_lens',
'url' => 'https://i.imgur.com/5bGzZi7.jpg',
'gl' => 'us',
'hl' => 'en',
]);
print_r($results->visual_matches);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_play',
'q' => 'kite',
'store' => 'apps',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'google_local_services',
'q' => 'electrician',
'data_cid' => '6745062158417646970',
]);
print_r($results->local_ads);- source: tests/ExampleSearchGoogleLocalServicesTest.php see: https://serpapi.com/google-local-services-api
use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'bing',
'q' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'baidu',
'q' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'yahoo',
'p' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'youtube',
'search_query' => 'coffee',
]);
print_r($results->video_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'walmart',
'query' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'ebay',
'_nkw' => 'water',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'naver',
'query' => 'coffee',
]);
print_r($results->ads_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'home_depot',
'q' => 'table',
]);
print_r($results->products);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'apple_app_store',
'term' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'engine' => 'duckduckgo',
'q' => 'coffee',
]);
print_r($results->organic_results);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$locations = $client->location(['q' => 'Austin', 'limit' => 3]);
echo "Number of locations: " . count($locations) . "\n";
print_r($locations);NOTE: api_key is not required for this endpoint.
First, run a search and save the search ID:
use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$results = $client->search([
'q' => 'Coffee',
'location' => 'Austin, Texas',
]);
$search_id = $results->search_metadata->id;Now retrieve the previous search from the archive (free of charge):
$archived = $client->searchArchive($search_id);
print_r($archived);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$account = $client->account();
print_r($account);use SerpApi\Client;
$client = new Client(getenv('API_KEY'));
$html = $client->html(['q' => 'Coffee']);
echo strlen($html) . " bytes of HTML\n";SerpApiException includes structured context for HTTP and API errors (status code, endpoint, search params, search id).
use SerpApi\Client;
use SerpApi\SerpApiException;
try {
$client = new Client('invalid_key');
$client->search(['q' => 'test']);
} catch (SerpApiException $exception) {
echo $exception->getMessage() . "\n";
// HTTP request failed with status: 401 error: Invalid API key... from url: https://serpapi.com/search
echo $exception->getSerpApiError() . "\n";
echo $exception->getResponseStatus() . "\n";
echo $exception->getSearchId() . "\n";
print_r($exception->getSearchParams());
print_r($exception->toArray());
}We love "true open source", "continuous integration", and Test Driven Development (TDD). We use PHPUnit to test our infrastructure around the clock using GitHub Actions to achieve the best QoS (Quality Of Service).
The tests/ directory includes specifications which serve the dual purposes of examples and functional tests.
Set your secret API key in your shell before running tests:
export API_KEY="your_secret_key"Install dependencies and run the test suite:
make install
make testContributions are welcome. Feel free to submit a pull request!
- 1.0 - First stable version
SerpApi supports all the major search engines. Google has the more advanced support with all the major services available: Images, News, Shopping and more...
The full documentation is available here.
Authors: Victor Benarbia victor@serpapi.com, Alaa Abdulridha alaa@serpapi.com For more information: https://serpapi.com