18
18
19
19
/**
20
20
* ScraperAPI documentation - https://www.scraperapi.com/documentation/
21
- *
22
- * @method Client setCountryCode($value) Activate country geotargetting ('us' for example)
23
- * @method Client setRender($value) Activate javascript rendering (true/false)
24
- * @method Client setPremium($value) Activate premium residential and mobile IPs (true/false)
25
- * @method Client setSessionNumber($value) Reuse the same proxy (123 for example)
26
- * @method Client setKeepHeaders($value) Use your own custom headers (true/false)
27
- * @method Client setDeviceType($value) Set your requests to use mobile or desktop user agents (desktop/mobile)
28
- * @method Client setAutoparse($value) Activate auto parsing for select websites (true/false)
29
21
*/
30
22
class Client
31
23
{
32
24
/**
33
25
* ScraperAPI endpoint url
34
26
*/
35
27
private const API = 'https://api.scraperapi.com ' ;
36
-
37
- /**
38
- * ScraperAPI available query params
39
- */
40
- private const PARAMS_LIST = [
41
- 'country_code ' ,
42
- 'render ' ,
43
- 'premium ' ,
44
- 'session_number ' ,
45
- 'keep_headers ' ,
46
- 'device_type ' ,
47
- 'autoparse '
48
- ];
49
-
50
28
/**
51
29
* ScraperAPI API key
52
30
* @var string
53
31
*/
54
32
private $ apiKey ;
55
-
33
+ /**
34
+ * ScraperAPI default request params
35
+ * @var array
36
+ */
37
+ private $ defaultApiParams ;
38
+ /**
39
+ * Default headers
40
+ * @var array
41
+ */
42
+ private $ defaultHeaders ;
56
43
/**
57
44
* If true, debug statement sent to the output. Useful for debugging async requests
58
45
* @var bool
59
46
*/
60
47
private $ printDebugInfo ;
61
-
62
48
/**
63
49
* If false, API key in debug statement will be replaced by 'API_KEY' string
64
50
* @var bool
65
51
*/
66
52
private $ showApiKey ;
67
-
68
53
/**
69
54
* Guzzle client
70
55
* @var Guzzle
71
56
*/
72
57
private $ guzzle ;
73
-
74
58
/**
75
59
* Optional PSR-3 logger for debug
76
60
* @var LoggerInterface|null
@@ -81,19 +65,6 @@ class Client
81
65
* @var mixed
82
66
*/
83
67
private $ logLevel ;
84
-
85
- /**
86
- * ScraperAPI default request params
87
- * @var array
88
- */
89
- private $ params = [];
90
-
91
- /**
92
- * Default headers
93
- * @var array
94
- */
95
- private $ headers = [];
96
-
97
68
/**
98
69
* Total promises in a bunch (for debug statements)
99
70
* @var int
@@ -105,13 +76,12 @@ class Client
105
76
*/
106
77
private $ fulfilledPromises = 0 ;
107
78
108
- /** @var array */
109
- private $ paramsList ;
110
-
111
79
/**
112
80
* ScraperAPI documentation - https://www.scraperapi.com/documentation/
113
81
*
114
82
* @param string $apiKey ScraperAPI API key
83
+ * @param array|null $defaultApiParams Default api parameters for requests
84
+ * @param array|null $defaultHeaders Default headers for requests
115
85
* @param int $timeout Request timeout
116
86
* @param int $tries Number of request attempts
117
87
* @param int $delayMultiplier Delay multiplier before new request attempt in seconds
@@ -123,6 +93,8 @@ class Client
123
93
*/
124
94
public function __construct (
125
95
string $ apiKey ,
96
+ ?array $ defaultApiParams = [],
97
+ ?array $ defaultHeaders = [],
126
98
int $ timeout = 60 ,
127
99
int $ tries = 3 ,
128
100
int $ delayMultiplier = 1 ,
@@ -134,13 +106,25 @@ public function __construct(
134
106
)
135
107
{
136
108
$ this ->apiKey = $ apiKey ;
109
+ $ this ->defaultApiParams = $ defaultApiParams ?? [];
110
+ $ this ->defaultHeaders = $ defaultHeaders ?? [];
137
111
$ this ->printDebugInfo = $ printDebugInfo ;
138
112
$ this ->showApiKey = $ showApiKey ;
139
113
$ this ->logger = $ logger ;
140
114
$ this ->logLevel = $ logLevel ;
141
115
142
- $ this ->paramsList = array_flip (static ::PARAMS_LIST );
116
+ $ this ->guzzle = $ this ->createGuzzleClient ($ timeout , $ tries , $ delayMultiplier , $ maxExceptionsLength );
117
+ }
143
118
119
+ /**
120
+ * @param int $timeout
121
+ * @param int $tries
122
+ * @param int $delayMultiplier
123
+ * @param int $maxExceptionsLength
124
+ * @return Guzzle
125
+ */
126
+ private function createGuzzleClient (int $ timeout , int $ tries , int $ delayMultiplier , int $ maxExceptionsLength ): Guzzle
127
+ {
144
128
$ handlerStack = HandlerStack::create ();
145
129
$ handlerStack ->push (Middleware::retry (
146
130
function (
@@ -176,113 +160,9 @@ function ($try) use ($delayMultiplier) {
176
160
));
177
161
$ handlerStack ->push (Middleware::httpErrors (new BodySummarizer ($ maxExceptionsLength )), 'http_errors ' );
178
162
179
- $ this ->guzzle = new Guzzle (['base_uri ' => static ::API , 'timeout ' => $ timeout , 'handler ' => $ handlerStack ]);
180
- }
181
-
182
-
183
- /**
184
- * Caller for Client default query params setters
185
- *
186
- * @param string $name
187
- * @param array $arguments
188
- * @return $this
189
- * @throws Exception
190
- */
191
- public function __call (string $ name , array $ arguments )
192
- {
193
-
194
- if (strpos ($ name , 'set ' ) === 0 ) {
195
- $ this ->setter ($ name , $ arguments );
196
- return $ this ;
197
- }
198
-
199
- throw new Exception ("Unknown method ' $ name'. " );
163
+ return new Guzzle (['base_uri ' => static ::API , 'timeout ' => $ timeout , 'handler ' => $ handlerStack ]);
200
164
}
201
165
202
- /**
203
- * @param string $name
204
- * @param array $arguments
205
- */
206
- private function setter (string $ name , array $ arguments ): void
207
- {
208
- $ paramName = $ this ->parseParamName ($ name );
209
- if (array_key_exists ($ paramName , $ this ->paramsList )) {
210
- $ this ->setParam ($ paramName , $ arguments );
211
- }
212
- }
213
-
214
- /**
215
- * @param string $name
216
- * @param array $arguments
217
- */
218
- private function setParam (string $ name , array $ arguments ): void
219
- {
220
- $ paramValue = $ arguments [0 ] ?? null ;
221
- if ((!is_string ($ paramValue ) && !is_bool ($ paramValue ))) {
222
- $ paramValue = (string )$ paramValue ;
223
- }
224
- if (is_bool ($ paramValue )) {
225
- $ paramValue = $ paramValue ? 'true ' : 'false ' ;
226
- }
227
- $ this ->params [$ name ] = $ paramValue ;
228
- }
229
-
230
- /**
231
- * @param string $name
232
- * @return string
233
- */
234
- private function parseParamName (string $ name ): string
235
- {
236
- $ words = preg_split ('/(?=[A-Z])/ ' , substr ($ name , 3 ));
237
- if (!$ words ) {
238
- return '' ;
239
- }
240
-
241
- return (string )array_reduce ($ words , static function ($ carry , $ item ) {
242
- if ($ item === '' ) {
243
- return $ carry ;
244
- }
245
- return ($ carry === '' || $ carry === null ) ? strtolower ($ item ) : "{$ carry }_ " . strtolower ($ item );
246
- });
247
-
248
- }
249
-
250
- /**
251
- * Set Client default query params in one step from array, replacing existing ones
252
- *
253
- * @param array $params
254
- * @return Client
255
- */
256
- public function setParams (array $ params ): Client
257
- {
258
- $ this ->params = $ params ;
259
- return $ this ;
260
- }
261
-
262
- /**
263
- * Add default header to Client
264
- * @param string $header
265
- * @param array|string $value
266
- * @return $this
267
- */
268
- public function addHeader (string $ header , $ value ): Client
269
- {
270
- $ this ->headers [$ header ] = $ value ;
271
- return $ this ;
272
- }
273
-
274
- /**
275
- * Set default Client headers in one step from array, replacing existing ones
276
- * @param array $headers
277
- * @return $this
278
- */
279
- public function setHeaders (array $ headers ): Client
280
- {
281
- $ this ->headers = $ headers ;
282
- return $ this ;
283
- }
284
-
285
-
286
166
/**
287
167
* Get info about ScraperAPI account
288
168
*
@@ -415,14 +295,14 @@ private function prepareQueryParams(
415
295
?array $ json
416
296
): array
417
297
{
418
- $ params = $ this ->params ;
298
+ $ params = $ this ->defaultApiParams ;
419
299
if (is_array ($ apiParams )) {
420
300
foreach ($ apiParams as $ param => $ value ) {
421
301
$ params [$ param ] = $ value ;
422
302
}
423
303
}
424
304
425
- $ resultHeaders = $ this ->headers ;
305
+ $ resultHeaders = $ this ->defaultHeaders ;
426
306
if (is_array ($ headers )) {
427
307
foreach ($ headers as $ param => $ value ) {
428
308
$ resultHeaders [$ param ] = $ value ;
0 commit comments