PHP code example of phpcfdi / image-captcha-resolver
1. Go to this page and download the library: Download phpcfdi/image-captcha-resolver library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpcfdi / image-captcha-resolver example snippets
declare(strict_types=1);
use PhpCfdi\ImageCaptchaResolver\CaptchaImage;
use PhpCfdi\ImageCaptchaResolver\CaptchaResolverInterface;
use PhpCfdi\ImageCaptchaResolver\UnableToResolveCaptchaException;
/**
* @var string $theImgElementSrcAtributte
* @var CaptchaResolverInterface $resolver
*/
$image = CaptchaImage::newFromInlineHtml($theImgElementSrcAtributte);
try {
$answer = $resolver->resolve($image);
} catch (UnableToResolveCaptchaException $exception) {
echo 'No se pudo resolver el captcha', PHP_EOL;
return;
}
echo "Respuesta del captcha: {$answer->getValue()}", PHP_EOL;
declare(strict_types=1);
use PhpCfdi\ImageCaptchaResolver\Resolvers\AntiCaptchaResolver;
use PhpCfdi\ImageCaptchaResolver\Timer\Timer;
/**
* @var string $clientKey La clave de cliente para conectarse a anti-captcha
*/
// Se puede crear el resolvedor usando los parámetros predefinidos
$resolverDefault = AntiCaptchaResolver::create($clientKey);
// O especificando parámetros funcionales
$resolverWithSettings = AntiCaptchaResolver::create(
$clientKey,
5, // segundos antes de intentar leer la respuesta
60, // segundos antes de considerar que el captcha no tiene solución
500 // milisegundos antes de reintentar obtener la respuesta
);
// O directamente creando los objetos
$resolverConstructed = new AntiCaptchaResolver(
new AntiCaptchaResolver\AntiCaptchaConnector($clientKey),
new Timer(4, 60, 500)
);
declare(strict_types=1);
use PhpCfdi\ImageCaptchaResolver\Resolvers\CaptchaLocalResolver;
use PhpCfdi\ImageCaptchaResolver\Timer\Timer;
/**
* @var string $baseUrl La dirección donde está corriendo el servicio, como http://localhost:9095
*/
// Se puede crear el resolvedor usando los parámetros predefinidos
$resolverDefault = CaptchaLocalResolver::create($baseUrl);
// O especificando parámetros funcionales
$resolverWithSettings = CaptchaLocalResolver::create(
$baseUrl,
5, // segundos antes de intentar leer la respuesta
60, // segundos antes de considerar que el captcha no tiene solución
500 // milisegundos antes de reintentar obtener la respuesta
);
// O directamente creando los objetos
$resolverConstructed = new CaptchaLocalResolver(
new CaptchaLocalResolver\CaptchaLocalResolverConnector($baseUrl),
new Timer(4, 60, 500)
);
declare(strict_types=1);
use PhpCfdi\ImageCaptchaResolver\CaptchaImageInterface;
use PhpCfdi\ImageCaptchaResolver\Resolvers\CommandLineResolver;
use PhpCfdi\ImageCaptchaResolver\UnableToResolveCaptchaException;
/**
* @var CaptchaImageInterface $image
*/
$resolver = CommandLineResolver::create(explode(' ', 'my-captcha-breaker --in {file} --stdout'));
try {
$answer = $resolver->resolve($image);
} catch (UnableToResolveCaptchaException $exception) {
echo 'No se pudo resolver el captcha: ', $exception->getMessage(), PHP_EOL;
return;
}
echo $answer, PHP_EOL;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.