1
|
|
|
<?php |
2
|
|
|
namespace LaravelAnticaptcha\Anticaptcha; |
3
|
|
|
|
4
|
|
|
use LaravelAnticaptcha\Anticaptcha\Exceptions\AnticaptchaException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Anticaptcha { |
8
|
|
|
|
9
|
|
|
private $host = "api.anti-captcha.com"; |
10
|
|
|
private $scheme = "https"; |
11
|
|
|
private $clientKey; |
12
|
|
|
private $verboseMode = false; |
13
|
|
|
private $errorMessage; |
14
|
|
|
private $taskId; |
15
|
|
|
public $taskInfo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Submit new task and receive tracking ID |
19
|
|
|
* @return bool |
20
|
|
|
* @throws AnticaptchaException |
21
|
|
|
*/ |
22
|
|
|
public function createTask() { |
23
|
|
|
|
24
|
|
|
$postData = array( |
25
|
|
|
"clientKey" => $this->clientKey, |
26
|
|
|
"task" => $this->getPostData() |
|
|
|
|
27
|
|
|
); |
28
|
|
|
$submitResult = $this->jsonPostRequest("createTask", $postData); |
29
|
|
|
|
30
|
|
|
if ($submitResult == false) { |
31
|
|
|
throw new AnticaptchaException('API error'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($submitResult->errorId == 0) { |
35
|
|
|
$this->taskId = $submitResult->taskId; |
36
|
|
|
$this->debout("created task with ID {$this->taskId}", "yellow"); |
37
|
|
|
return true; |
38
|
|
|
} else { |
39
|
|
|
$this->setErrorMessage($submitResult->errorDescription); |
40
|
|
|
throw new AnticaptchaException("API error {$submitResult->errorCode} : {$submitResult->errorDescription}"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $maxSeconds |
47
|
|
|
* @param int $currentSecond |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
* @throws AnticaptchaException |
51
|
|
|
*/ |
52
|
|
|
public function waitForResult($maxSeconds = 300, $currentSecond = 0) { |
53
|
|
|
$postData = array( |
54
|
|
|
"clientKey" => $this->clientKey, |
55
|
|
|
"taskId" => $this->taskId |
56
|
|
|
); |
57
|
|
|
if ($currentSecond == 0) { |
58
|
|
|
$this->debout("waiting 5 seconds.."); |
59
|
|
|
sleep(3); |
60
|
|
|
} else { |
61
|
|
|
sleep(1); |
62
|
|
|
} |
63
|
|
|
$this->debout("requesting task status"); |
64
|
|
|
$postResult = $this->jsonPostRequest("getTaskResult", $postData); |
65
|
|
|
|
66
|
|
|
if ($postResult == false) { |
67
|
|
|
throw new AnticaptchaException('API error'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->taskInfo = $postResult; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($this->taskInfo->errorId == 0) { |
74
|
|
|
if ($this->taskInfo->status == "processing") { |
75
|
|
|
|
76
|
|
|
$this->debout("task is still processing"); |
77
|
|
|
//repeating attempt |
78
|
|
|
return $this->waitForResult($maxSeconds, $currentSecond+1); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
if ($this->taskInfo->status == "ready") { |
82
|
|
|
$this->debout("task is complete", "green"); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
$this->setErrorMessage("unknown API status, update your software"); |
86
|
|
|
throw new AnticaptchaException("unknown API status, update your software"); |
87
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
$this->setErrorMessage($this->taskInfo->errorDescription); |
90
|
|
|
throw new AnticaptchaException("API error {$this->taskInfo->errorCode} : {$this->taskInfo->errorDescription}"); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
* @throws AnticaptchaException |
97
|
|
|
*/ |
98
|
|
|
public function getBalance() { |
99
|
|
|
$postData = array( |
100
|
|
|
"clientKey" => $this->clientKey |
101
|
|
|
); |
102
|
|
|
$result = $this->jsonPostRequest("getBalance", $postData); |
103
|
|
|
if ($result == false) { |
104
|
|
|
throw new AnticaptchaException('API error'); |
105
|
|
|
} |
106
|
|
|
if ($result->errorId == 0) { |
107
|
|
|
return $result->balance; |
108
|
|
|
} else { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $methodName |
115
|
|
|
* @param $postData |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
* @throws AnticaptchaException |
119
|
|
|
*/ |
120
|
|
|
public function jsonPostRequest($methodName, $postData) { |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
if ($this->verboseMode) { |
124
|
|
|
echo "making request to {$this->scheme}://{$this->host}/$methodName with following payload:\n"; |
125
|
|
|
print_r($postData); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
$ch = curl_init(); |
130
|
|
|
curl_setopt($ch, CURLOPT_URL, "{$this->scheme}://{$this->host}/$methodName"); |
131
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
132
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); |
133
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
134
|
|
|
$postDataEncoded = json_encode($postData); |
135
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataEncoded); |
136
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
137
|
|
|
'Content-Type: application/json; charset=utf-8', |
138
|
|
|
'Accept: application/json', |
139
|
|
|
'Content-Length: '.strlen($postDataEncoded) |
140
|
|
|
)); |
141
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
142
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); |
143
|
|
|
$result = curl_exec($ch); |
144
|
|
|
$curlError = curl_error($ch); |
145
|
|
|
|
146
|
|
|
if ($curlError != "") { |
147
|
|
|
$this->debout("Network error: $curlError"); |
148
|
|
|
throw new AnticaptchaException("Network error: $curlError"); |
149
|
|
|
} |
150
|
|
|
curl_close($ch); |
151
|
|
|
return json_decode($result); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setVerboseMode($mode) { |
155
|
|
|
$this->verboseMode = $mode; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function debout($message, $color = "white") { |
159
|
|
|
if (!$this->verboseMode) { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
if ($color != "white" and $color != "") { |
163
|
|
|
$CLIcolors = array( |
164
|
|
|
"cyan" => "0;36", |
165
|
|
|
"green" => "0;32", |
166
|
|
|
"blue" => "0;34", |
167
|
|
|
"red" => "0;31", |
168
|
|
|
"yellow" => "1;33" |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$CLIMsg = "\033[".$CLIcolors[$color]."m$message\033[0m"; |
172
|
|
|
|
173
|
|
|
} else { |
174
|
|
|
$CLIMsg = $message; |
175
|
|
|
} |
176
|
|
|
echo $CLIMsg."\n"; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $message |
181
|
|
|
*/ |
182
|
|
|
public function setErrorMessage($message) { |
183
|
|
|
$this->errorMessage = $message; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function getErrorMessage() { |
190
|
|
|
return $this->errorMessage; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return mixed |
195
|
|
|
*/ |
196
|
|
|
public function getTaskId() { |
197
|
|
|
return $this->taskId; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $taskId |
202
|
|
|
*/ |
203
|
|
|
public function setTaskId($taskId) { |
204
|
|
|
$this->taskId = $taskId; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function setHost($host) { |
208
|
|
|
$this->host = $host; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function setScheme($scheme) { |
212
|
|
|
$this->scheme = $scheme; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set client access key, must be 32 bytes long |
217
|
|
|
* @param string $key |
218
|
|
|
*/ |
219
|
|
|
public function setKey($key) { |
220
|
|
|
$this->clientKey = $key; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|