1818
1919/**
2020 * 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)
2921 */
3022class Client
3123{
3224 /**
3325 * ScraperAPI endpoint url
3426 */
3527 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-
5028 /**
5129 * ScraperAPI API key
5230 * @var string
5331 */
5432 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 ;
5643 /**
5744 * If true, debug statement sent to the output. Useful for debugging async requests
5845 * @var bool
5946 */
6047 private $ printDebugInfo ;
61-
6248 /**
6349 * If false, API key in debug statement will be replaced by 'API_KEY' string
6450 * @var bool
6551 */
6652 private $ showApiKey ;
67-
6853 /**
6954 * Guzzle client
7055 * @var Guzzle
7156 */
7257 private $ guzzle ;
73-
7458 /**
7559 * Optional PSR-3 logger for debug
7660 * @var LoggerInterface|null
@@ -81,19 +65,6 @@ class Client
8165 * @var mixed
8266 */
8367 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-
9768 /**
9869 * Total promises in a bunch (for debug statements)
9970 * @var int
@@ -105,13 +76,12 @@ class Client
10576 */
10677 private $ fulfilledPromises = 0 ;
10778
108- /** @var array */
109- private $ paramsList ;
110-
11179 /**
11280 * ScraperAPI documentation - https://www.scraperapi.com/documentation/
11381 *
11482 * @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
11585 * @param int $timeout Request timeout
11686 * @param int $tries Number of request attempts
11787 * @param int $delayMultiplier Delay multiplier before new request attempt in seconds
@@ -123,6 +93,8 @@ class Client
12393 */
12494 public function __construct (
12595 string $ apiKey ,
96+ ?array $ defaultApiParams = [],
97+ ?array $ defaultHeaders = [],
12698 int $ timeout = 60 ,
12799 int $ tries = 3 ,
128100 int $ delayMultiplier = 1 ,
@@ -134,13 +106,25 @@ public function __construct(
134106 )
135107 {
136108 $ this ->apiKey = $ apiKey ;
109+ $ this ->defaultApiParams = $ defaultApiParams ?? [];
110+ $ this ->defaultHeaders = $ defaultHeaders ?? [];
137111 $ this ->printDebugInfo = $ printDebugInfo ;
138112 $ this ->showApiKey = $ showApiKey ;
139113 $ this ->logger = $ logger ;
140114 $ this ->logLevel = $ logLevel ;
141115
142- $ this ->paramsList = array_flip (static ::PARAMS_LIST );
116+ $ this ->guzzle = $ this ->createGuzzleClient ($ timeout , $ tries , $ delayMultiplier , $ maxExceptionsLength );
117+ }
143118
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+ {
144128 $ handlerStack = HandlerStack::create ();
145129 $ handlerStack ->push (Middleware::retry (
146130 function (
@@ -176,113 +160,9 @@ function ($try) use ($delayMultiplier) {
176160 ));
177161 $ handlerStack ->push (Middleware::httpErrors (new BodySummarizer ($ maxExceptionsLength )), 'http_errors ' );
178162
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 ]);
200164 }
201165
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-
286166 /**
287167 * Get info about ScraperAPI account
288168 *
@@ -415,14 +295,14 @@ private function prepareQueryParams(
415295 ?array $ json
416296 ): array
417297 {
418- $ params = $ this ->params ;
298+ $ params = $ this ->defaultApiParams ;
419299 if (is_array ($ apiParams )) {
420300 foreach ($ apiParams as $ param => $ value ) {
421301 $ params [$ param ] = $ value ;
422302 }
423303 }
424304
425- $ resultHeaders = $ this ->headers ;
305+ $ resultHeaders = $ this ->defaultHeaders ;
426306 if (is_array ($ headers )) {
427307 foreach ($ headers as $ param => $ value ) {
428308 $ resultHeaders [$ param ] = $ value ;
0 commit comments