|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ZanySoft\LaravelSerpApi\Lib; |
|
6
|
|
|
|
|
7
|
|
|
use RestClient; |
|
|
|
|
|
|
8
|
|
|
use ZanySoft\LaravelSerpApi\Exceptions\SerpApiSearchException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Wrapper around serpapi.com |
|
12
|
|
|
*/ |
|
13
|
|
|
class SerpApiSearch |
|
14
|
|
|
{ |
|
15
|
|
|
protected $options; |
|
16
|
|
|
protected $api; |
|
17
|
|
|
protected $api_key; |
|
18
|
|
|
protected $engine; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param $api_key |
|
22
|
|
|
* @param $engine |
|
23
|
|
|
* @throws SerpApiSearchException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($api_key = null, $engine = 'google') |
|
26
|
|
|
{ |
|
27
|
|
|
// register engine |
|
28
|
|
|
if ($engine) { |
|
29
|
|
|
$this->engine = $engine; |
|
30
|
|
|
} else { |
|
31
|
|
|
throw new SerpApiSearchException("engine must be defined"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// register private api key |
|
35
|
|
|
if ($api_key) { |
|
36
|
|
|
$this->api_key = $api_key; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* set serpapi key |
|
42
|
|
|
* |
|
43
|
|
|
* @param $api_key |
|
44
|
|
|
* @return void |
|
45
|
|
|
* @throws SerpApiSearchException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function set_serp_api_key($api_key) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($api_key == null) { |
|
50
|
|
|
throw new SerpApiSearchException("serp_api_key must have a value"); |
|
51
|
|
|
} |
|
52
|
|
|
$this->api_key = $api_key; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* get json search result |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $query |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function get_json(array $query) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->search('json', $query); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* get raw html search result |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $query |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
* @throws SerpApiSearchException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function get_html(array $query) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->search('html', $query); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get location using Location API |
|
80
|
|
|
* @param $query |
|
81
|
|
|
* @param $limit |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
* @throws SerpApiSearchException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function get_location($query, $limit) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->query("/locations.json", 'json', [ |
|
88
|
|
|
'q' => $query, |
|
89
|
|
|
'limit' => $limit, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retrieve search result from the Search Archive API |
|
95
|
|
|
* |
|
96
|
|
|
* @param $search_id |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
* @throws SerpApiSearchException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function get_search_archive($search_id) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->query("/searches/$search_id.json", 'json', []); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get account information using Account API |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
* @throws SerpApiSearchException |
|
110
|
|
|
*/ |
|
111
|
|
|
public function get_account() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->query('/account', 'json', []); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Run a search |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $output |
|
120
|
|
|
* @param array $query |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
* @throws SerpApiSearchException |
|
123
|
|
|
* @throws \RestClientException |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function search(string $output, array $query) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->query('/search', $output, $query); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param $path |
|
132
|
|
|
* @param $output |
|
133
|
|
|
* @param $query |
|
134
|
|
|
* @return mixed |
|
135
|
|
|
* @throws SerpApiSearchException |
|
136
|
|
|
* @throws \RestClientException |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function query(string $path, string $output, array $query) |
|
139
|
|
|
{ |
|
140
|
|
|
$decode_format = $output == 'json' ? 'json' : 'php'; |
|
141
|
|
|
|
|
142
|
|
|
if ($this->api_key == null) { |
|
143
|
|
|
throw new SerpApiSearchException("serp_api_key must be defined either in the constructor or by the method set_serp_api_key"); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$api = new RestClient([ |
|
147
|
|
|
'base_url' => "https://serpapi.com", |
|
148
|
|
|
'user_agent' => 'google-search-results-php/1.3.0', |
|
149
|
|
|
'curl_options' => [ |
|
150
|
|
|
CURLOPT_SSL_VERIFYHOST => 0, |
|
151
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
|
152
|
|
|
] |
|
153
|
|
|
]); |
|
154
|
|
|
|
|
155
|
|
|
$default_q = [ |
|
156
|
|
|
'output' => $output, |
|
157
|
|
|
'source' => 'php', |
|
158
|
|
|
'api_key' => $this->api_key, |
|
159
|
|
|
'engine' => $this->engine, |
|
160
|
|
|
]; |
|
161
|
|
|
$query = array_merge($default_q, $query); |
|
162
|
|
|
$result = $api->get($path, $query); |
|
163
|
|
|
|
|
164
|
|
|
// GET https://serpapi.com/search?q=Coffee&location=Portland&format=json&source=php&engine=google&serp_api_key=demo |
|
165
|
|
|
if ($result->info->http_code == 200) { |
|
166
|
|
|
// html response |
|
167
|
|
|
if ($decode_format == 'php') { |
|
168
|
|
|
return $result->response; |
|
169
|
|
|
} |
|
170
|
|
|
// json response |
|
171
|
|
|
return $result->decode_response(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($result->info->http_code == 400 && $output == 'json') { |
|
175
|
|
|
$error = $result->decode_response(); |
|
176
|
|
|
$msg = $error->error; |
|
177
|
|
|
|
|
178
|
|
|
throw new SerpApiSearchException($msg); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
throw new SerpApiSearchException("Unexpected exception: $result->response"); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths