-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgoogle-search-results.php
228 lines (174 loc) · 6.68 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
<?php
require __DIR__ . '/../../../vendor/autoload.php';
// SerpWowException
class SerpWowException extends Exception {}
/*
Provides access to Google Search Results via the SerpWow API
Get a free API key at https://serpwow.com/
*/
class GoogleSearchResults {
public $options;
public $api;
public $api_key;
public function __construct($api_key="API_KEY") {
$this->api_key = $api_key;
}
public function json($q) {
return $this->httpGet('json', 'json', $q, '/live/search', NULL);
}
public function html($q) {
return $this->httpGet('string', 'html', $q, '/live/search', NULL);
}
public function csv($q) {
return $this->httpGet('string', 'csv', $q, '/live/search', NULL);
}
public function locations($q) {
return $this->httpGet('json', 'json', $q, '/live/locations', NULL);
}
public function account() {
return $this->httpGet('json', 'json', NULL, '/live/account', NULL);
}
public function listBatches() {
return $this->httpGet('json', 'json', NULL, '/live/batches', NULL);
}
public function getBatch($batchId) {
return $this->httpGet('json', 'json', NULL, "/live/batches/{$batchId}", NULL);
}
public function startBatch($batchId) {
return $this->httpGet('json', 'json', NULL, "/live/batches/{$batchId}/start", NULL);
}
public function stopBatch($batchId) {
return $this->httpGet('json', 'json', NULL, "/live/batches/{$batchId}/stop", NULL);
}
public function deleteBatch($batchId) {
return $this->httpDelete("/live/batches/{$batchId}");
}
public function deleteBatchSearch($batchId, $searchId) {
return $this->httpDelete("/live/batches/{$batchId}/{$searchId}");
}
public function listBatchSearches($batchId, $page) {
return $this->httpGet("json", "json", NULL, "/live/batches/{$batchId}/searches/{$page}", NULL);
}
public function findBatchSearches($batchId, $page, $searchTerm) {
return $this->httpGet("json", "json", NULL, "/live/batches/{$batchId}/searches/{$page}", $searchTerm);
}
public function listAllBatchSearchesAsJSON($batchId) {
return $this->httpGet("json", "json", NULL, "/live/batches/{$batchId}/searches/json", NULL);
}
public function listAllBatchSearchesAsCSV($batchId) {
return $this->httpGet("json", "json", NULL, "/live/batches/{$batchId}/searches/csv", NULL);
}
public function listBatchResults($batchId) {
return $this->httpGet('json', 'json', NULL, "/live/batches/{$batchId}/results", NULL);
}
public function getBatchResultSet($batchId, $resultSetId) {
return $this->httpGet("json", "json", NULL, "/live/batches/{$batchId}/results/{$resultSetId}", NULL);
}
public function getBatchResultSetAsCSV($batchId, $resultSetId) {
return $this->httpGet("json", "json", NULL, "/live/batches/{$batchId}/results/{$resultSetId}/csv", NULL);
}
public function createBatch($params) {
return $this->httpPost("/live/batches", $params);
}
public function updateBatch($batchId, $params) {
return $this->httpPut("/live/batches/{$batchId}", $params);
}
public function updateBatchSearch($batchId, $searchId, $params) {
return $this->httpPut("/live/batches/{$batchId}/{$searchId}", $params);
}
function httpGet($decode_format, $output, $q, $path, $searchTerm = NULL) {
if($this->api_key == NULL) {
throw new SerpWowException("api_key must be defined in the constructor");
}
$args = [];
$api = new RestClient($args);
$default_q = [
'source' => 'php',
'api_key' => $this->api_key
];
if (isset($searchTerm)) {
$default_q['q'] = $searchTerm;
}
if ($output != 'json') {
$default_q['output'] = $output;
}
if (isset($q)) {
$q = array_merge($default_q, $q);
} else {
$q = $default_q;
}
$result = $api->get("https://api.serpwow.com{$path}", $q);
if($result->info->http_code == 200 || $result->info->http_code == 301 || $result->info->http_code == 302) {
if($decode_format == 'string') {
return $result->response;
} else {
return $result->decode_response();
}
} else {
$error = $result->decode_response();
$msg = $error->request_info->message;
throw new SerpWowException($msg);
return;
}
throw new SerpWowException("Unexpected failure, please contact hello@serpwow.com: $result");
return;
}
function httpDelete($path) {
if($this->api_key == NULL) {
throw new SerpWowException("api_key must be defined in the constructor");
}
$args = [];
$api = new RestClient($args);
$path = $path.'?api_key='.$this->api_key.'&source=php';
$result = $api->delete("https://api.serpwow.com{$path}");
if($result->info->http_code == 200 || $result->info->http_code == 301 || $result->info->http_code == 302) {
return $result->decode_response();
} else {
$error = $result->decode_response();
$msg = $error->request_info->message;
throw new SerpWowException($msg);
return;
}
throw new SerpWowException("Unexpected failure, please contact hello@serpwow.com: $result");
return;
}
function httpPost($path, $data) {
if($this->api_key == NULL) {
throw new SerpWowException("api_key must be defined in the constructor");
}
$args = [];
$api = new RestClient($args);
$path = $path.'?api_key='.$this->api_key.'&source=php';
$result = $api->post("https://api.serpwow.com{$path}", json_encode($data), array('Content-Type' => 'application/json'));
if($result->info->http_code == 200 || $result->info->http_code == 301 || $result->info->http_code == 302) {
return $result->decode_response();
} else {
$error = $result->decode_response();
$msg = $error->request_info->message;
throw new SerpWowException($msg);
return;
}
throw new SerpWowException("Unexpected failure, please contact hello@serpwow.com: $result");
return;
}
function httpPut($path, $data) {
if($this->api_key == NULL) {
throw new SerpWowException("api_key must be defined in the constructor");
}
$args = [];
$api = new RestClient($args);
$path = $path.'?api_key='.$this->api_key.'&source=php';
$result = $api->put("https://api.serpwow.com{$path}", json_encode($data), array('Content-Type' => 'application/json'));
if($result->info->http_code == 200 || $result->info->http_code == 301 || $result->info->http_code == 302) {
return $result->decode_response();
} else {
$error = $result->decode_response();
$msg = $error->request_info->message;
throw new SerpWowException($msg);
return;
}
throw new SerpWowException("Unexpected failure, please contact hello@serpwow.com: $result");
return;
}
}
?>