redpic/antigate

antigate.com API Laravel 5.1 包

0.1 2015-08-04 18:39 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:38:33 UTC


README

antigate.com API Laravel 5.1 包

说明

安装包: composer require redpic/antigate

config/app.php 中添加服务提供者

'providers' => [
    // ...
    Redpic\Antigate\AntigateServiceProvider::class,
];

发布配置: php artisan vendor:publish 并在其中写入访问令牌: config/antigate.php

创建两个事件监听器

php artisan make:listener test --event CaptchaWasRecognized
php artisan make:listener test --event CaptchaWasNotRecognized

将它们转换为

app/Listeners/CaptchaWasRecognizedListener.php:

<?php

namespace App\Listeners;

use Redpic\Antigate\Events\CaptchaWasRecognized;
use Redpic\Antigate\Jobs\RecognizeCaptcha;

class CaptchaWasRecognizedListener
{
    public function handle(CaptchaWasRecognized $event)
    {
        $event->captcha->getKey(); // Тут текст разгаданной капчи
    }
}

app/Listeners/CaptchaWasNotRecognizedListener.php:

<?php

namespace App\Listeners;

use Redpic\Antigate\Events\CaptchaWasNotRecognized;
use Redpic\Antigate\Jobs\RecognizeCaptcha;

class CaptchaWasNotRecognizedListener
{
    public function handle(CaptchaWasNotRecognized $event)
    {
        $event->captcha; // Не разгаданная капча
        $event->exception; // Исключение вызванное во время работы

        /*
        Если в этом месте вызвать какое то исключение, 
        то задание по разгадываю этой капчи снова добавится в очередь
        */
    }
}

app/Providers/EventServiceProvider.php 中注册监听器

protected $listen = [
    //...
    'Redpic\Antigate\Events\CaptchaWasRecognized' => [
        'App\Listeners\CaptchaWasRecognizedListener',
    ],
    'Redpic\Antigate\Events\CaptchaWasNotRecognized' => [
        'App\Listeners\CaptchaWasNotRecognizedListener',
    ],
];

从控制器添加任务看起来大约是这样的

$captcha = (new Captcha)->setImageByUrl('http://ПУТЬ_К_КАПЧЕ');
$this->dispatch(new RecognizeCaptcha($captcha));