1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anticaptcha; |
4
|
|
|
|
5
|
|
|
use Anticaptcha\Response\CreateTaskResponse; |
6
|
|
|
use Anticaptcha\Response\GetAppStatsResponse; |
7
|
|
|
use Anticaptcha\Response\GetBalanceResponse; |
8
|
|
|
use Anticaptcha\Response\GetQueueStatsResponse; |
9
|
|
|
use Anticaptcha\Response\GetSpendingStatsResponse; |
10
|
|
|
use Anticaptcha\Response\GetTaskResultResponse; |
11
|
|
|
use Anticaptcha\Response\PushAntiGateVariableResponse; |
12
|
|
|
use Anticaptcha\Response\ReportCorrectRecaptchaResponse; |
13
|
|
|
use Anticaptcha\Response\ReportIncorrectImageCaptchaResponse; |
14
|
|
|
use Anticaptcha\Response\ReportIncorrectRecaptchaResponse; |
15
|
|
|
use Anticaptcha\Task\AbstractTask; |
16
|
|
|
use Anticaptcha\Task\ImageToTextTask; |
17
|
|
|
use GuzzleHttp\Psr7\Request; |
18
|
|
|
use JsonException; |
19
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
20
|
|
|
use Psr\Http\Client\ClientInterface; |
21
|
|
|
use Psr\Http\Message\RequestInterface; |
22
|
|
|
|
23
|
|
|
final class Client |
24
|
|
|
{ |
25
|
|
|
private ConfigurationInterface $configuration; |
26
|
|
|
private ClientInterface $httpClient; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
ConfigurationInterface $configuration, |
30
|
|
|
ClientInterface $httpClient |
31
|
|
|
) { |
32
|
|
|
$this->httpClient = $httpClient; |
33
|
|
|
$this->configuration = $configuration; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a captcha task |
38
|
|
|
* |
39
|
|
|
* @param AbstractTask $task Task object |
40
|
|
|
* @param ?int $softId ID of your application from our Developers Center. |
41
|
|
|
* @param ?string $languagePool Sets workers' pool language. |
42
|
|
|
* en (default): English language queue |
43
|
|
|
* rn: group of countries: Russia, Ukraine, Belarus, Kazakhstan |
44
|
|
|
* @param ?string $callbackUrl Optional web address where we can send the results of captcha task processing. |
45
|
|
|
* Contents are sent by AJAX POST request and are similar to the contents of |
46
|
|
|
* getTaskResult method. |
47
|
|
|
* |
48
|
|
|
* @return CreateTaskResponse |
49
|
|
|
* |
50
|
|
|
* @throws ClientExceptionInterface |
51
|
|
|
* @throws JsonException |
52
|
|
|
* @see https://anti-captcha.com/apidoc/methods/createTask |
53
|
|
|
*/ |
54
|
|
|
public function createTask( |
55
|
|
|
AbstractTask $task, |
56
|
|
|
?int $softId = null, |
57
|
|
|
?string $languagePool = null, |
58
|
|
|
?string $callbackUrl = null |
59
|
|
|
): CreateTaskResponse { |
60
|
|
|
$request = $this->createRequest( |
61
|
|
|
$this->configuration->getApiUrl() . '/createTask', |
62
|
|
|
array_filter([ |
63
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
64
|
|
|
'softId' => $softId ?? $this->configuration->getSoftId(), |
65
|
|
|
'languagePool' => $languagePool ?? $this->configuration->getLanguagePool(), |
66
|
|
|
'task' => $task->toArray(), |
67
|
|
|
'callbackUrl' => $callbackUrl ?? $this->configuration->getCallbackUrl(), |
68
|
|
|
], fn ($value) => $value !== null) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$response = $this->httpClient->sendRequest($request); |
72
|
|
|
|
73
|
|
|
return CreateTaskResponse::fromHttpResponse($response); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Syntax sugar for self::createTask() |
78
|
|
|
* |
79
|
|
|
* @param string $base64Content Base64 encoded image content |
80
|
|
|
* @param array $taskProperties Array of ImageToTextTask attributes (attribute => value) |
81
|
|
|
* |
82
|
|
|
* @return CreateTaskResponse |
83
|
|
|
* |
84
|
|
|
* @throws ClientExceptionInterface |
85
|
|
|
* @throws JsonException |
86
|
|
|
*/ |
87
|
|
|
public function createTaskByBase64( |
88
|
|
|
string $base64Content, |
89
|
|
|
array $taskProperties = [] |
90
|
|
|
): CreateTaskResponse { |
91
|
|
|
return $this->createTask(ImageToTextTask::fromBase64($base64Content, $taskProperties)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Syntax sugar for self::createTask() |
96
|
|
|
* |
97
|
|
|
* @param string $link file_get_contents() compatible filename |
98
|
|
|
* @param array $taskProperties Array of ImageToTextTask attributes (attribute => value) |
99
|
|
|
* |
100
|
|
|
* @return CreateTaskResponse |
101
|
|
|
* |
102
|
|
|
* @throws ClientExceptionInterface |
103
|
|
|
* @throws JsonException |
104
|
|
|
*/ |
105
|
|
|
public function createTaskByImage( |
106
|
|
|
string $link, |
107
|
|
|
array $taskProperties = [] |
108
|
|
|
): CreateTaskResponse { |
109
|
|
|
return $this->createTask(ImageToTextTask::fromImage($link, $taskProperties)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Synchronous image recognition. Returns complete result with solution. |
114
|
|
|
* |
115
|
|
|
* @param string $link file_get_contents() compatible filename |
116
|
|
|
* @param array $taskProperties Array of ImageToTextTask attributes (attribute => value) |
117
|
|
|
* |
118
|
|
|
* @return GetTaskResultResponse |
119
|
|
|
* @throws ClientExceptionInterface |
120
|
|
|
* @throws JsonException |
121
|
|
|
*/ |
122
|
|
|
public function resolveImage( |
123
|
|
|
string $link, |
124
|
|
|
array $taskProperties = [] |
125
|
|
|
): GetTaskResultResponse { |
126
|
|
|
$createTaskResponse = $this->createTaskByImage($link, $taskProperties); |
127
|
|
|
$taskResult = $this->getTaskResult($createTaskResponse->getTaskId()); |
128
|
|
|
$taskResult->wait(); |
129
|
|
|
|
130
|
|
|
return $taskResult; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Synchronous image recognition. Returns complete result with solution. |
135
|
|
|
* |
136
|
|
|
* @param string $base64Content Base64 encoded image content |
137
|
|
|
* @param array $taskProperties Array of ImageToTextTask attributes (attribute => value) |
138
|
|
|
* |
139
|
|
|
* @return GetTaskResultResponse |
140
|
|
|
* @throws ClientExceptionInterface |
141
|
|
|
* @throws JsonException |
142
|
|
|
*/ |
143
|
|
|
public function resolveBase64( |
144
|
|
|
string $base64Content, |
145
|
|
|
array $taskProperties = [] |
146
|
|
|
): GetTaskResultResponse { |
147
|
|
|
$createTaskResponse = $this->createTaskByBase64($base64Content, $taskProperties); |
148
|
|
|
$taskResult = $this->getTaskResult($createTaskResponse->getTaskId()); |
149
|
|
|
$taskResult->wait(); |
150
|
|
|
|
151
|
|
|
return $taskResult; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Request a task result |
156
|
|
|
* |
157
|
|
|
* @param int $taskId |
158
|
|
|
* |
159
|
|
|
* @return GetTaskResultResponse |
160
|
|
|
* |
161
|
|
|
* @throws ClientExceptionInterface |
162
|
|
|
* @throws JsonException |
163
|
|
|
* |
164
|
|
|
* @see https://anti-captcha.com/apidoc/methods/getTaskResult |
165
|
|
|
*/ |
166
|
|
|
public function getTaskResult( |
167
|
|
|
int $taskId |
168
|
|
|
): GetTaskResultResponse { |
169
|
|
|
$httpRequest = $this->createRequest( |
170
|
|
|
$this->configuration->getApiUrl() . '/getTaskResult', |
171
|
|
|
[ |
172
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
173
|
|
|
'taskId' => $taskId |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
178
|
|
|
|
179
|
|
|
$response = GetTaskResultResponse::fromHttpResponse($httpResponse); |
180
|
|
|
|
181
|
|
|
// For GetTaskResultResponse::wait() method |
182
|
|
|
$response->setClient($this); |
183
|
|
|
$response->setTaskId($taskId); |
184
|
|
|
|
185
|
|
|
return $response; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Retrieve an account balance |
190
|
|
|
* |
191
|
|
|
* @return GetBalanceResponse |
192
|
|
|
* |
193
|
|
|
* @throws JsonException |
194
|
|
|
* @throws ClientExceptionInterface |
195
|
|
|
* |
196
|
|
|
* @see https://anti-captcha.com/apidoc/methods/getBalance |
197
|
|
|
*/ |
198
|
|
|
public function getBalance(): GetBalanceResponse |
199
|
|
|
{ |
200
|
|
|
$request = $this->createRequest( |
201
|
|
|
$this->configuration->getApiUrl() . '/getBalance', |
202
|
|
|
[ |
203
|
|
|
'clientKey' => $this->configuration->getClientKey() |
204
|
|
|
] |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$response = $this->httpClient->sendRequest($request); |
208
|
|
|
|
209
|
|
|
return GetBalanceResponse::fromHttpResponse($response); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Obtain queue load statistics |
214
|
|
|
* |
215
|
|
|
* @param int $queueId |
216
|
|
|
* |
217
|
|
|
* @return GetQueueStatsResponse |
218
|
|
|
* |
219
|
|
|
* @throws ClientExceptionInterface |
220
|
|
|
* @throws JsonException |
221
|
|
|
* |
222
|
|
|
* @see https://anti-captcha.com/apidoc/methods/getQueueStats |
223
|
|
|
*/ |
224
|
|
|
public function getQueueStats(int $queueId): GetQueueStatsResponse |
225
|
|
|
{ |
226
|
|
|
$request = $this->createRequest( |
227
|
|
|
$this->configuration->getApiUrl() . '/getQueueStats', |
228
|
|
|
[ |
229
|
|
|
'queueId' => $queueId |
230
|
|
|
] |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$response = $this->httpClient->sendRequest($request); |
234
|
|
|
|
235
|
|
|
return GetQueueStatsResponse::fromHttpResponse($response); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Send complaint on an image captcha |
240
|
|
|
* |
241
|
|
|
* @param int $taskId |
242
|
|
|
* |
243
|
|
|
* @return ReportIncorrectImageCaptchaResponse |
244
|
|
|
* |
245
|
|
|
* @throws JsonException |
246
|
|
|
* @throws ClientExceptionInterface |
247
|
|
|
* |
248
|
|
|
* @see https://anti-captcha.com/apidoc/methods/reportIncorrectImageCaptcha |
249
|
|
|
*/ |
250
|
|
|
public function reportIncorrectImageCaptcha( |
251
|
|
|
int $taskId |
252
|
|
|
): ReportIncorrectImageCaptchaResponse { |
253
|
|
|
$httpRequest = $this->createRequest( |
254
|
|
|
$this->configuration->getApiUrl() . '/reportIncorrectImageCaptcha', |
255
|
|
|
[ |
256
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
257
|
|
|
'taskId' => $taskId |
258
|
|
|
] |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
262
|
|
|
|
263
|
|
|
return ReportIncorrectImageCaptchaResponse::fromHttpResponse($httpResponse); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Send complaint on a Recaptcha token |
268
|
|
|
* @param int $taskId |
269
|
|
|
* |
270
|
|
|
* @return ReportIncorrectRecaptchaResponse |
271
|
|
|
* |
272
|
|
|
* @throws ClientExceptionInterface |
273
|
|
|
* @throws JsonException |
274
|
|
|
* |
275
|
|
|
* @see https://anti-captcha.com/apidoc/methods/reportIncorrectRecaptcha |
276
|
|
|
*/ |
277
|
|
|
public function reportIncorrectRecaptcha( |
278
|
|
|
int $taskId |
279
|
|
|
): ReportIncorrectRecaptchaResponse { |
280
|
|
|
$httpRequest = $this->createRequest( |
281
|
|
|
$this->configuration->getApiUrl() . '/reportIncorrectRecaptcha', |
282
|
|
|
[ |
283
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
284
|
|
|
'taskId' => $taskId |
285
|
|
|
] |
286
|
|
|
); |
287
|
|
|
|
288
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
289
|
|
|
|
290
|
|
|
return ReportIncorrectRecaptchaResponse::fromHttpResponse($httpResponse); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Report correctly solved Recaptcha tokens |
295
|
|
|
* |
296
|
|
|
* @param int $taskId |
297
|
|
|
* |
298
|
|
|
* @return ReportCorrectRecaptchaResponse |
299
|
|
|
* |
300
|
|
|
* @throws ClientExceptionInterface |
301
|
|
|
* @throws JsonException |
302
|
|
|
* |
303
|
|
|
* @see https://anti-captcha.com/apidoc/methods/reportCorrectRecaptcha |
304
|
|
|
*/ |
305
|
|
|
public function reportCorrectRecaptcha( |
306
|
|
|
int $taskId |
307
|
|
|
): ReportCorrectRecaptchaResponse { |
308
|
|
|
$httpRequest = $this->createRequest( |
309
|
|
|
$this->configuration->getApiUrl() . '/reportCorrectRecaptcha', |
310
|
|
|
[ |
311
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
312
|
|
|
'taskId' => $taskId |
313
|
|
|
] |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
317
|
|
|
|
318
|
|
|
return ReportCorrectRecaptchaResponse::fromHttpResponse($httpResponse); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Submit a variable value for AntiGate task. |
323
|
|
|
* |
324
|
|
|
* @param int $taskId An identifier obtained in the createTask method |
325
|
|
|
* @param string $name Name of the variable |
326
|
|
|
* @param mixed $value Value of the postponed variable |
327
|
|
|
* |
328
|
|
|
* @return PushAntiGateVariableResponse |
329
|
|
|
* |
330
|
|
|
* @throws ClientExceptionInterface |
331
|
|
|
* @throws JsonException |
332
|
|
|
* |
333
|
|
|
* @see https://anti-captcha.com/apidoc/methods/pushAntiGateVariable |
334
|
|
|
*/ |
335
|
|
|
public function pushAntigateVariable( |
336
|
|
|
int $taskId, |
337
|
|
|
string $name, |
338
|
|
|
$value |
339
|
|
|
): PushAntiGateVariableResponse { |
340
|
|
|
$httpRequest = $this->createRequest( |
341
|
|
|
$this->configuration->getApiUrl() . '/reportCorrectRecaptcha', |
342
|
|
|
[ |
343
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
344
|
|
|
'taskId' => $taskId, |
345
|
|
|
'name' => $name, |
346
|
|
|
'value' => $value |
347
|
|
|
] |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
351
|
|
|
|
352
|
|
|
return PushAntiGateVariableResponse::fromHttpResponse($httpResponse); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param ?string $date Unix timestamp of the hour from which we grab the 24 hour stats. |
357
|
|
|
* @param ?int $queue You can find the name of the queue in the AntiCaptcha statistics. If it's not |
358
|
|
|
* provided, totals are calculated for all queues. |
359
|
|
|
* Examples: |
360
|
|
|
* "English ImageToText" |
361
|
|
|
* "Recaptcha Proxyless" |
362
|
|
|
* @param ?int $softId The ID of your app from the Developer Center |
363
|
|
|
* @param ?string $ip Filter statistics by IP address you used for your API calls |
364
|
|
|
|
365
|
|
|
* @return GetSpendingStatsResponse |
366
|
|
|
* |
367
|
|
|
* @throws ClientExceptionInterface |
368
|
|
|
* @throws JsonException |
369
|
|
|
* |
370
|
|
|
* @see https://anti-captcha.com/apidoc/methods/getSpendingStats |
371
|
|
|
*/ |
372
|
|
|
public function getSpendingStats( |
373
|
|
|
?string $date = null, |
374
|
|
|
?int $queue = null, |
375
|
|
|
?int $softId = null, |
376
|
|
|
?string $ip = null |
377
|
|
|
): GetSpendingStatsResponse { |
378
|
|
|
$httpRequest = $this->createRequest( |
379
|
|
|
$this->configuration->getApiUrl() . '/getSpendingStats', |
380
|
|
|
array_filter([ |
381
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
382
|
|
|
'date' => $date, |
383
|
|
|
'queue' => $queue, |
384
|
|
|
'softId' => $softId, |
385
|
|
|
'ip' => $ip |
386
|
|
|
], fn ($value) => $value !== null) |
387
|
|
|
); |
388
|
|
|
|
389
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
390
|
|
|
|
391
|
|
|
return GetSpendingStatsResponse::fromHttpResponse($httpResponse); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Retrieve application statistics |
396
|
|
|
* |
397
|
|
|
* @param int $softId |
398
|
|
|
* @param ?string $mode |
399
|
|
|
* |
400
|
|
|
* @return GetAppStatsResponse |
401
|
|
|
* |
402
|
|
|
* @throws ClientExceptionInterface |
403
|
|
|
* @throws JsonException |
404
|
|
|
* |
405
|
|
|
* @see https://anti-captcha.com/apidoc/methods/getAppStats |
406
|
|
|
*/ |
407
|
|
|
public function getAppStats( |
408
|
|
|
int $softId, |
409
|
|
|
?string $mode = null |
410
|
|
|
): GetAppStatsResponse { |
411
|
|
|
$httpRequest = $this->createRequest( |
412
|
|
|
$this->configuration->getApiUrl() . '/getAppStats', |
413
|
|
|
array_filter([ |
414
|
|
|
'clientKey' => $this->configuration->getClientKey(), |
415
|
|
|
'softId' => $softId, |
416
|
|
|
'mode' => $mode, |
417
|
|
|
], fn ($value) => $value !== null) |
418
|
|
|
); |
419
|
|
|
|
420
|
|
|
$httpResponse = $this->httpClient->sendRequest($httpRequest); |
421
|
|
|
|
422
|
|
|
return GetAppStatsResponse::fromHttpResponse($httpResponse); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* @param string $uri |
427
|
|
|
* @param array $payload |
428
|
|
|
* |
429
|
|
|
* @return RequestInterface |
430
|
|
|
* |
431
|
|
|
* @throws JsonException |
432
|
|
|
*/ |
433
|
|
|
private function createRequest(string $uri, array $payload = []): RequestInterface |
434
|
|
|
{ |
435
|
|
|
return new Request( |
436
|
|
|
'POST', |
437
|
|
|
$uri, |
438
|
|
|
[ |
439
|
|
|
'Content-Type' => 'application/json' |
440
|
|
|
], |
441
|
|
|
json_encode($payload, JSON_THROW_ON_ERROR), |
442
|
|
|
'1.1' |
443
|
|
|
); |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|