Skip to content

Commit

Permalink
Fix Loggers errors, when debug=false
Browse files Browse the repository at this point in the history
Update Excemples
m-tymchyk committed Dec 2, 2016
2 parents 90db8f1 + 9b1ab95 commit bf60ca4
Showing 6 changed files with 109 additions and 38 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Anticaptcha PHP SDK

[PHP Anticaptcha on Packages.org](https://packagist.org/packages/reilag/php-anticaptcha)

PHP client for Anticaptcha services:

* [anti-captcha.com](http://anti-captcha.com) (recommend)
@@ -27,26 +29,27 @@ require 'vendor/autoload.php';

### Recognize captcha
```php
use Anticaptcha\Anticaptcha;
use AntiCaptcha\AntiCaptcha;

// Get file content
$image = file_get_contents(realpath(dirname(__FILE__)) . '/images/image.jpg');

// Your API key
$apiKey = '*********** API_KEY **************';

$antiCaptchaClient = new Anticaptcha('rucaptcha', ['api_key' => $apiKey, 'debug' => true]);
$antiCaptchaClient = new AntiCaptcha(AntiCaptcha::SERVICE_ANTICAPTCHA, ['api_key' => $apiKey, 'debug' => true]);
echo $antiCaptchaClient->recognize($image, null, ['phrase' => 0, 'numeric' => 0]);
```

### Get balance
```php
use Anticaptcha\Anticaptcha;
use AntiCaptcha\AntiCaptcha;

$apiKey = '*********** API_KEY **************';

$service = new \Anticaptcha\Service\Antigate($apiKey);
$antiCaptchaClient = new \Anticaptcha\Anticaptcha($service);
$service = new \AntiCaptcha\Service\AntiCaptcha($apiKey);
$antiCaptchaClient = new \AntiCaptcha\AntiCaptcha($service);

echo "Your Balance is: " . $antiCaptchaClient->balance() . "\n";

echo $antiCaptchaClient->balance();
```
```
6 changes: 3 additions & 3 deletions example/balance.php
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@

$apiKey = '*********** API_KEY **************';

$service = new \Anticaptcha\Service\Antigate($apiKey);
$service = new \AntiCaptcha\Service\AntiCaptcha($apiKey);

$ac = new \Anticaptcha\Anticaptcha($service);
$ac = new \AntiCaptcha\AntiCaptcha($service);

echo $ac->balance();
echo "Your Balance is: " . $ac->balance() . "\n";
4 changes: 2 additions & 2 deletions example/recognize.php
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@

require_once realpath(dirname(dirname(__FILE__))) . '/vendor/autoload.php';

use Anticaptcha\Anticaptcha;
use AntiCaptcha\AntiCaptcha;


$image = file_get_contents(realpath(dirname(__FILE__)) . '/images/image.jpg');

$apiKey = '*********** API_KEY **************';

$ac = new Anticaptcha('rucaptcha', ['api_key' => $apiKey, 'debug' => true]);
$ac = new AntiCaptcha(AntiCaptcha::SERVICE_ANTICAPTCHA, ['api_key' => $apiKey, 'debug' => true]);

echo $ac->recognize($image, null, ['phrase' => 0, 'numeric' => 0]);
84 changes: 60 additions & 24 deletions src/AntiCaptcha.php
Original file line number Diff line number Diff line change
@@ -3,13 +3,15 @@
use AntiCaptcha\Exception\AntiCaptchaException;
use AntiCaptcha\Exception\InvalidAntiCaptchaServiceException;
use AntiCaptcha\Service\AbstractService;
use Psr\Log\AbstractLogger;

use GuzzleHttp\Client;

use Psr\Log\AbstractLogger;


/**
* Class AntiCaptcha
* @package Anticaptcha
* @package AntiCaptcha
*/
class AntiCaptcha
{
@@ -26,9 +28,15 @@ class AntiCaptcha

/**
* @var AbstractLogger $logger
* @deprecated
*/
protected $logger;

/**
* @var bool
*/
protected $debugMod = false;

/**
* @var array
*/
@@ -41,10 +49,10 @@ class AntiCaptcha
/**
* Constants list
*/
const SERVICE_ANTICAPTCHA = 'anti-captcha';
const SERVICE_ANTIGATE = 'antigate';
const SERVICE_CAPTCHABOT = 'captchabot';
const SERVICE_RUCAPTCHA = 'rucaptcha';
const SERVICE_ANTICAPTCHA = 'anti-captcha';
const SERVICE_ANTIGATE = 'antigate';
const SERVICE_CAPTCHABOT = 'captchabot';
const SERVICE_RUCAPTCHA = 'rucaptcha';

/**
* Captcha service list
@@ -61,7 +69,7 @@ class AntiCaptcha

/**
* AntiCaptcha constructor.
* @param null $service
* @param null|string|AbstractService $service
* @param array $options
*
* @throws AntiCaptchaException
@@ -103,7 +111,7 @@ public function __construct($service = null, array $options = [])

if (!empty($options['debug']))
{
// set Logger
$this->debugMod = true;
$this->setLogger(new Logger);
}

@@ -170,10 +178,17 @@ public function setClient($client)
*/
public function balance()
{
$this->logger->debug("check ballans ...");
if( $this->debugMod )
{
$this->logger->debug("check ballans ...");
}

$url = $this->getService()->getApiUrl() . '/res.php';
$this->logger->debug('connect to: ' . $url);

if( $this->debugMod )
{
$this->logger->debug('connect to: ' . $url);
}

$request = $this->client->request('GET', $url, [
'query' =>
@@ -185,7 +200,11 @@ public function balance()

$body = $request->getBody();

$this->logger->debug('result: ' . $body);
if( $this->debugMod )
{
$this->logger->debug('result: ' . $body);
}


if (strpos($body, 'ERROR') !== false)
{
@@ -220,7 +239,8 @@ public function recognize($image, $url = null, $params = [])

$captcha_id = $this->sendImage($image);

if (!empty($captcha_id)) {
if (!empty($captcha_id))
{
return $this->getResult($captcha_id);
}
}
@@ -245,7 +265,8 @@ protected function sendImage($image)
]
];

foreach ($this->getService()->getParams() as $key => $val) {
foreach ($this->getService()->getParams() as $key => $val)
{
$postfields['form_params'][$key] = (string)$val;
}

@@ -284,16 +305,22 @@ protected function sendImage($image)
*/
protected function getResult($captcha_id)
{
$this->logger->debug('captcha sent, got captcha ID: ' . $captcha_id);
if( $this->debugMod )
{
$this->logger->debug('captcha sent, got captcha ID: ' . $captcha_id);
}

// задержка перед первым опросом результата каптчи
$this->logger->debug('waiting for 10 seconds');
// Delay, before first captcha check
if( $this->debugMod )
{
$this->logger->debug('waiting for 10 seconds');
}
sleep(10);

// максимальное время опроса результата каптчи
$waittime = 0;
$waitTime = 0;

while (true) {
while (true)
{
$request = $this->client->request('GET', $this->getService()->getApiUrl() . '/res.php', [
'query' =>
[
@@ -312,17 +339,26 @@ protected function getResult($captcha_id)

if ($body == "CAPCHA_NOT_READY")
{
$this->logger->debug('captcha is not ready yet');
if( $this->debugMod )
{
$this->logger->debug('captcha is not ready yet');
}

$waittime += $this->options['timeout_ready'];
$waitTime += $this->options['timeout_ready'];

if ($waittime > $this->options['timeout_max'])
if ($waitTime > $this->options['timeout_max'])
{
$this->logger->debug('timelimit (' . $this->options['timeout_max'] . ') hit');
if( $this->debugMod )
{
$this->logger->debug('timelimit (' . $this->options['timeout_max'] . ') hit');
}
break;
}

$this->logger->debug('waiting for ' . $this->options['timeout_ready'] . ' seconds');
if( $this->debugMod )
{
$this->logger->debug('waiting for ' . $this->options['timeout_ready'] . ' seconds');
}
sleep($this->options['timeout_ready']);
}
else
19 changes: 18 additions & 1 deletion src/Service/AntiCaptcha.php
Original file line number Diff line number Diff line change
@@ -18,4 +18,21 @@ class AntiCaptcha extends AbstractService
* @var string
*/
protected $api_url = 'http://anti-captcha.com';
}

/**
* Method getParams description.
*
* @return array
*/
public function getParams()
{
return array_merge(
$this->params,
[
'soft_id' => 791
]
);
}

}

17 changes: 16 additions & 1 deletion src/Service/Rucaptcha.php
Original file line number Diff line number Diff line change
@@ -11,4 +11,19 @@ class Rucaptcha extends AbstractService
* @var string
*/
protected $api_url = 'http://rucaptcha.com';
}

/**
* Method getParams description.
*
* @return array
*/
public function getParams()
{
return array_merge(
$this->params,
[
'soft_id' => 1528
]
);
}
}

0 comments on commit bf60ca4

Please sign in to comment.