PHP code example of godruoyi / laravel-tencent007-captcha
1. Go to this page and download the library: Download godruoyi/laravel-tencent007-captcha 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/ */
godruoyi / laravel-tencent007-captcha example snippets
php artisan vendor:publish --provider="Godruoyi\Tencent007\ServiceProvider"
class UserRegisterController extends Controller
{
public function __invoke(Request $request)
{
$response = app('007')->ticketVerify($request->ticket, $request->randstr);
if (!$response->success()) {
// 不可信用户
}
}
}
// bool 是否验证成功
$response->success();
// string 验证错误信息
$response->message();
// array|null 请求返回的原始数据
$response->getOriginal();
// int 用户恶意等级
$response->level();
protected $routeMiddleware = [
// ...
'throttle.007' => \Godruoyi\Tencent007\Middlewares\ThrottleRequests::class,
];
Route::post('users/register', 'UserRegisterController')->middleware('throttle.007:60,1');
use Symfony\Component\HttpKernel\Exception\HttpException;
class NeedCaptchaAuthException extends HttpException
{
public function __construct($statusCode = 429, $message = 'Need tencent captcha certification.')
{
parent::__construct($statusCode, $message);
}
}
namespace App\Exceptions;
use Exception;
use Godruoyi\Tencent007\Exceptions\NeedCaptchaAuthException;
use Godruoyi\Tencent007\Exceptions\RequestNotPassedException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
public function render($request, Exception $exception)
{
if ($exception instanceof NeedCaptchaAuthException) {
return response()->json([
'code' => -429,
'msg' => '请先通过滑块验证'
]);
} elseif ($exception instanceof RequestNotPassedException) {
return response()->json([
'code' => -403,
'msg' => '非法用户'
]);
}
return parent::render($request, $exception);
}
}