forked from AdminAnticaptcha/anticaptcha-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanticaptcha.php
253 lines (210 loc) · 7.24 KB
/
anticaptcha.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
interface AntiCaptchaTaskProtocol {
public function getPostData();
public function getTaskSolution();
}
class Anticaptcha {
private $host = "api.anti-captcha.com";
private $scheme = "https";
private $clientKey;
private $verboseMode = false;
private $errorMessage;
private $taskId;
public $taskInfo;
/**
* Submit new task and receive tracking ID
*/
public function createTask() {
$postData = array(
"clientKey" => $this->clientKey,
"task" => $this->getPostData()
);
$submitResult = $this->jsonPostRequest("createTask", $postData);
if ($submitResult == false) {
$this->debout("API error", "red");
return false;
}
if ($submitResult->errorId == 0) {
$this->taskId = $submitResult->taskId;
$this->debout("created task with ID {$this->taskId}", "yellow");
return true;
} else {
$this->debout("API error {$submitResult->errorCode} : {$submitResult->errorDescription}", "red");
$this->setErrorMessage($submitResult->errorDescription);
return false;
}
}
public function waitForResult($maxSeconds = 300, $currentSecond = 0) {
$postData = array(
"clientKey" => $this->clientKey,
"taskId" => $this->taskId
);
if ($currentSecond == 0) {
$this->debout("waiting 5 seconds..");
sleep(3);
} else {
sleep(1);
}
$this->debout("requesting task status");
$postResult = $this->jsonPostRequest("getTaskResult", $postData);
if ($postResult == false) {
$this->debout("API error", "red");
return false;
}
$this->taskInfo = $postResult;
if ($this->taskInfo->errorId == 0) {
if ($this->taskInfo->status == "processing") {
$this->debout("task is still processing");
//repeating attempt
return $this->waitForResult($maxSeconds, $currentSecond+1);
}
if ($this->taskInfo->status == "ready") {
$this->debout("task is complete", "green");
return true;
}
$this->setErrorMessage("unknown API status, update your software");
return false;
} else {
$this->debout("API error {$this->taskInfo->errorCode} : {$this->taskInfo->errorDescription}", "red");
$this->setErrorMessage($this->taskInfo->errorDescription);
return false;
}
}
public function getBalance() {
$postData = array(
"clientKey" => $this->clientKey
);
$result = $this->jsonPostRequest("getBalance", $postData);
if ($result == false) {
$this->debout("API error", "red");
return false;
}
if ($result->errorId == 0) {
return $result->balance;
} else {
return false;
}
}
public function reportIncorrectImageCaptcha() {
$result = $this->jsonPostRequest("reportIncorrectImageCaptcha", [
"clientKey" => $this->clientKey,
"taskId" => $this->taskId
]);
if ($result == false) {
$this->debout("API error", "red");
return false;
}
if ($result->errorId == 0) {
return true;
} else {
return false;
}
}
public function reportIncorrectRecaptcha() {
$result = $this->jsonPostRequest("reportIncorrectRecaptcha", [
"clientKey" => $this->clientKey,
"taskId" => $this->taskId
]);
if ($result == false) {
$this->debout("API error", "red");
return false;
}
if ($result->errorId == 0) {
return true;
} else {
return false;
}
}
public function reportCorrectRecaptcha() {
$result = $this->jsonPostRequest("reportCorrectRecaptcha", [
"clientKey" => $this->clientKey,
"taskId" => $this->taskId
]);
if ($result == false) {
$this->debout("API error", "red");
return false;
}
if ($result->errorId == 0) {
return true;
} else {
return false;
}
}
public function jsonPostRequest($methodName, $postData) {
if ($this->verboseMode) {
echo "making request to {$this->scheme}://{$this->host}/$methodName with following payload:\n";
print_r($postData);
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"{$this->scheme}://{$this->host}/$methodName");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_ENCODING,"gzip,deflate");
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, "POST");
$postDataEncoded = json_encode($postData);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postDataEncoded);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Accept: application/json',
'Content-Length: ' . strlen($postDataEncoded)
));
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30);
$result = curl_exec($ch);
if ($this->verboseMode) {
echo "API response:\n";
echo $result."\n";
}
$curlError = curl_error($ch);
if ($curlError != "") {
$this->errorMessage = "Network error: $curlError";
$this->debout("Network error: $curlError");
return false;
}
curl_close($ch);
return json_decode($result);
}
public function setVerboseMode($mode) {
$this->verboseMode = $mode;
}
public function debout($message, $color = "white") {
if (!$this->verboseMode) return false;
if ($color != "white" and $color != "") {
$CLIcolors = array(
"cyan" => "0;36",
"green" => "0;32",
"blue" => "0;34",
"red" => "0;31",
"yellow" => "1;33"
);
$CLIMsg = "\033[".$CLIcolors[$color]."m$message\033[0m";
} else {
$CLIMsg = $message;
}
echo $CLIMsg."\n";
}
public function setErrorMessage($message) {
$this->errorMessage = $message;
}
public function getErrorMessage() {
return $this->errorMessage;
}
public function getTaskId() {
return $this->taskId;
}
public function setTaskId($taskId) {
$this->taskId = $taskId;
}
public function setHost($host) {
$this->host = $host;
}
public function setScheme($scheme) {
$this->scheme = $scheme;
}
/**
* Set client access key, must be 32 bytes long
* @param string $key
*/
public function setKey($key) {
$this->clientKey = $key;
}
}