-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathgoogle-search-results.php
304 lines (263 loc) · 6.89 KB
/
google-search-results.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// Exception
class SerpApiSearchException extends Exception {}
/***
* GoogleSearch engine
* @see https://serpapi.com/search-api
*/
class GoogleSearch extends SerpApiSearch {
public function __construct($api_key) {
parent::__construct($api_key, 'google');
}
}
/***
* BingSearch engine
* @see https://serpapi.com/bing-search-api
*/
class BingSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'bing');
}
}
/***
* DuckDuckGoSearch engine
* @see https://serpapi.com/duckduckgo-search-api
*/
class BaiduSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'baidu');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* Yahoo search
* @see https://serpapi.com/yahoo-search-api
*/
class YahooSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'yahoo');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* Yandex search
* @see https://serpapi.com/yandex-search-api
*/
class YandexSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'yandex');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* Ebay search
* @see https://serpapi.com/ebay-search-api
*/
class EbaySearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'ebay');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* YouTube search
* @see https://serpapi.com/youtube-search-api
*/
class YouTubeSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'youTube');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* WalmartSearch search
* @see https://serpapi.com/walmart-search-api
*/
class WalmartSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'walmart');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* HomeDepotSearch engine
* @see https://serpapi.com/home-depot-search-api
*/
class HomeDepotSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'walmart');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* AppleStoreAppSearch engine
* @see https://serpapi.com/apple-app-store
*/
class AppleStoreAppSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'apple_app_store');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* NaverSearch engine
* @see https://serpapi.com/naver-search-api
*/
class NaverSearch extends SerpApiSearch {
public function __construct($api_key=NULL) {
parent::__construct($api_key, 'naver');
}
/***
* Method is not supported.
*/
public function get_location($q, $limit) {
throw new SerpApiSearchException("location is not currently supported by Bing");
}
}
/***
* Wrapper around serpapi.com
*/
class SerpApiSearch {
public $options;
public $api;
public $api_key;
public $engine;
public function __construct($api_key = NULL, $engine = 'google') {
// register engine
if($engine) {
$this->engine = $engine;
} else {
throw new SerpApiSearchException("engine must be defined");
}
// register private api key
if($api_key) {
$this->api_key = $api_key;
}
}
public function set_serp_api_key($api_key) {
if($api_key == NULL)
throw new SerpApiSearchException("serp_api_key must have a value");
$this->api_key = $api_key;
}
/***
* get_json
* @return [Hash] search result "json like"
*/
public function get_json($q) {
return $this->search( 'json', $q);
}
/***
* get_html
* @return [String] raw html search result
*/
public function get_html($q) {
return $this->search('html', $q);
}
/***
* Get location using Location API
*/
function get_location($q, $limit) {
$query = [
'q' => $q,
'limit' => $limit
];
return $this->query("/locations.json", 'json', $query);
}
/***
* Retrieve search result from the Search Archive API
*/
function get_search_archive($search_id) {
return $this->query("/searches/$search_id.json", 'json', []);
}
/***
* Get account information using Account API
*/
function get_account() {
return $this->query('/account', 'json', []);
}
/**
* Run a search
*/
function search($output, $q) {
return $this->query('/search', $output, $q);
}
function query($path, $output, $q) {
$decode_format = $output == 'json' ? 'json' : 'php';
if($this->api_key == NULL) {
throw new SerpApiSearchException("serp_api_key must be defined either in the constructor or by the method set_serp_api_key");
}
$api = new RestClient([
'base_url' => "https://serpapi.com",
'user_agent' => 'google-search-results-php/1.3.0',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
]);
$default_q = [
'output' => $output,
'source' => 'php',
'api_key' => $this->api_key,
'engine' => $this->engine
];
$q = array_merge($default_q, $q);
$result = $api->get($path, $q);
// GET https://serpapi.com/search?q=Coffee&location=Portland&format=json&source=php&engine=google&serp_api_key=demo
if($result->info->http_code == 200)
{
// html response
if($decode_format == 'php') {
return $result->response;
}
// json response
return $result->decode_response();
}
if($result->info->http_code == 400 && $output == 'json')
{
$error = $result->decode_response();
$msg = $error->error;
throw new SerpApiSearchException($msg);
}
throw new SerpApiSearchException("Unexpected exception: $result->response");
}
}