Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/serpapi-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run static analysis
# PHPStan needs PHP 7.4+; the library itself still supports 7.2.
if: matrix.php-versions >= '7.4'
run: composer run-script analyse

- name: Run test suite
run: composer run-script test
env:
API_KEY: ${{ secrets.API_KEY }}
SERPAPI_KEY: ${{ secrets.SERPAPI_KEY || secrets.API_KEY }}
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Default target
all: install readme test
all: install readme analyse test

# Clean up the project
clean:
Expand All @@ -9,10 +9,21 @@ clean:
install:
composer install --prefer-dist --no-progress

# Run static analysis
analyse:
vendor/bin/phpstan analyse --no-progress --memory-limit=512M

# Run the tests
test:
vendor/bin/phpunit -c phpunit.xml

# Generate README from ERB template
readme:
erb -T '-' README.md.erb > README.md

# Run the demo scripts end to end
demo:
@for file in demo/*.php; do \
echo "running demo: $$file"; \
php $$file || exit 1; \
done
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require 'vendor/autoload.php';

use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'q' => 'coffee',
]);
Expand Down Expand Up @@ -86,7 +86,7 @@ The default engine is `google`. You can change it via the second constructor par
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'), 'bing');
$client = new Client(getenv('SERPAPI_KEY'), 'bing');
$results = $client->search(['q' => 'coffee']);
```

Expand All @@ -97,7 +97,7 @@ The default request timeout is 120 seconds. Customize it via the third construct
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'), 'google', 30);
$client = new Client(getenv('SERPAPI_KEY'), 'google', 30);
```

## Search API
Expand All @@ -106,7 +106,7 @@ $client = new Client(getenv('API_KEY'), 'google', 30);
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google',
'tbm' => 'isch',
Expand All @@ -123,7 +123,7 @@ see: [https://serpapi.com/search-api](https://serpapi.com/search-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_scholar',
'q' => 'coffee',
Expand All @@ -139,7 +139,7 @@ see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_autocomplete',
'q' => 'coffee',
Expand All @@ -155,7 +155,7 @@ see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-au
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_shopping',
'q' => 'coffee',
Expand All @@ -171,7 +171,7 @@ see: [https://serpapi.com/google-shopping-api](https://serpapi.com/google-shoppi
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_maps',
'q' => 'pizza',
Expand All @@ -189,7 +189,7 @@ see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_jobs',
'q' => 'coffee',
Expand All @@ -205,7 +205,7 @@ see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_events',
'q' => 'Events in Austin',
Expand All @@ -222,7 +222,7 @@ see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-a
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_lens',
'url' => 'https://i.imgur.com/5bGzZi7.jpg',
Expand All @@ -240,7 +240,7 @@ see: [https://serpapi.com/google-lens-api](https://serpapi.com/google-lens-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_play',
'q' => 'kite',
Expand All @@ -257,7 +257,7 @@ see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'google_local_services',
'q' => 'electrician',
Expand All @@ -274,7 +274,7 @@ see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'bing',
'q' => 'coffee',
Expand All @@ -290,7 +290,7 @@ see: [https://serpapi.com/bing-search-api](https://serpapi.com/bing-search-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'baidu',
'q' => 'coffee',
Expand All @@ -306,7 +306,7 @@ see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'yahoo',
'p' => 'coffee',
Expand All @@ -322,7 +322,7 @@ see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'youtube',
'search_query' => 'coffee',
Expand All @@ -338,7 +338,7 @@ see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'walmart',
'query' => 'coffee',
Expand All @@ -354,7 +354,7 @@ see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'ebay',
'_nkw' => 'water',
Expand All @@ -370,7 +370,7 @@ see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'naver',
'query' => 'coffee',
Expand All @@ -386,7 +386,7 @@ see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'home_depot',
'q' => 'table',
Expand All @@ -402,7 +402,7 @@ see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'apple_app_store',
'term' => 'coffee',
Expand All @@ -418,7 +418,7 @@ see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store)
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'engine' => 'duckduckgo',
'q' => 'coffee',
Expand All @@ -437,7 +437,7 @@ see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$locations = $client->location(['q' => 'Austin', 'limit' => 3]);

echo "Number of locations: " . count($locations) . "\n";
Expand All @@ -453,7 +453,7 @@ First, run a search and save the search ID:
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'q' => 'Coffee',
'location' => 'Austin, Texas',
Expand All @@ -473,7 +473,7 @@ print_r($archived);
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$account = $client->account();
print_r($account);
```
Expand All @@ -483,7 +483,7 @@ print_r($account);
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$html = $client->html(['q' => 'Coffee']);

echo strlen($html) . " bytes of HTML\n";
Expand Down Expand Up @@ -522,7 +522,7 @@ The `tests/` directory includes specifications which serve the dual purposes of
Set your secret API key in your shell before running tests:

```bash
export API_KEY="your_secret_key"
export SERPAPI_KEY="your_secret_key"
```

Install dependencies and run the test suite:
Expand Down
18 changes: 9 additions & 9 deletions README.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def snippet(path)
property = source[/assertResponseHasProperty\(\$response,\s*'([^']+)'/, 1] || 'organic_results'

code = "use SerpApi\\Client;\n\n"
code += "$client = new Client(getenv('API_KEY'));\n"
code += "$client = new Client(getenv('SERPAPI_KEY'));\n"
code += "$results = $client->search(#{params});\n\n"
code += "print_r($results->#{property});\n"

Expand Down Expand Up @@ -72,7 +72,7 @@ require 'vendor/autoload.php';

use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'q' => 'coffee',
]);
Expand Down Expand Up @@ -110,7 +110,7 @@ The default engine is `google`. You can change it via the second constructor par
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'), 'bing');
$client = new Client(getenv('SERPAPI_KEY'), 'bing');
$results = $client->search(['q' => 'coffee']);
```

Expand All @@ -121,7 +121,7 @@ The default request timeout is 120 seconds. Customize it via the third construct
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'), 'google', 30);
$client = new Client(getenv('SERPAPI_KEY'), 'google', 30);
```

## Search API
Expand Down Expand Up @@ -213,7 +213,7 @@ see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$locations = $client->location(['q' => 'Austin', 'limit' => 3]);

echo "Number of locations: " . count($locations) . "\n";
Expand All @@ -229,7 +229,7 @@ First, run a search and save the search ID:
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$results = $client->search([
'q' => 'Coffee',
'location' => 'Austin, Texas',
Expand All @@ -249,7 +249,7 @@ print_r($archived);
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$account = $client->account();
print_r($account);
```
Expand All @@ -259,7 +259,7 @@ print_r($account);
```php
use SerpApi\Client;

$client = new Client(getenv('API_KEY'));
$client = new Client(getenv('SERPAPI_KEY'));
$html = $client->html(['q' => 'Coffee']);

echo strlen($html) . " bytes of HTML\n";
Expand Down Expand Up @@ -298,7 +298,7 @@ The `tests/` directory includes specifications which serve the dual purposes of
Set your secret API key in your shell before running tests:

```bash
export API_KEY="your_secret_key"
export SERPAPI_KEY="your_secret_key"
```

Install dependencies and run the test suite:
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5.52 || ^9.6 || ^10.5 || ^11.5 || ^12.5 || ^13.0"
"phpunit/phpunit": "^8.5.52 || ^9.6 || ^10.5 || ^11.5 || ^12.5 || ^13.0",
"phpstan/phpstan": "^1.12 || ^2.1"
},
"autoload": {
"psr-4": {
Expand All @@ -58,6 +59,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit -c phpunit.xml"
"test": "vendor/bin/phpunit -c phpunit.xml",
"analyse": "vendor/bin/phpstan analyse --no-progress --memory-limit=512M"
}
}
Loading