腾讯云 图形验证码接入

新建图形验证应用

首先进入控制台 console.cloud.tencent.com/captcha

腾讯云 图形验证码接入

「新建验证」后,可获得 APPIDApp Secret Key

开发文档

腾讯云 图形验证码接入

云 API 密钥

腾讯云 图形验证码接入
新建密钥

腾讯云 图形验证码接入

前端接入

略过
腾讯云 图形验证码接入

服务器接入

腾讯云 图形验证码接入

腾讯云 图形验证码接入

腾讯云 图形验证码接入

  1. 点击右上方 PHP SDK 使用说明 ,下载 sdk
  2. 填入参数调试,复制自动生成的代码。

实践

比如我将 sdk 下载到了 laravel/sdk 目录

腾讯云 图形验证码接入

DEMO

<?php
namespace App\Library\Other;
require_once '../sdk/cvm/vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Captcha\V20190722\CaptchaClient;
use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest;
use Illuminate\Support\Facades\Log;
class TCaptcha {
    private $tencent_secret_id;
    private $tencent_secret_key;
    private $captcha_appid;
    private $captcha_key;
    public function __construct()
    {
        $this->tencent_secret_id = env('TSECRET_ID');
        $this->tencent_secret_key = env('TSECRET_KEY');
        $this->captcha_appid = env('CAPCHA_APPID');
        $this->captcha_key = env('CAPCHA_KEY');
    }

    public function captchaResult($ticket, $randstr)
    {
        try {
            $cred = new Credential($this->tencent_secret_id, $this->tencent_secret_key);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("captcha.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new CaptchaClient($cred, "", $clientProfile);

            $req = new DescribeCaptchaResultRequest();

            $params = array(
                "CaptchaType" => 9,
                "Ticket" => $ticket,
                "UserIp" => get_client_ip(), 
                "BusinessId" => 1, 
                "SceneId" => 1,
                "Randstr" => $randstr,
                "CaptchaAppId" => intval($this->captcha_appid),
                "AppSecretKey" => $this->captcha_key, 
                // "NeedGetCaptchaTime" => 1
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->DescribeCaptchaResult($req);    //{"CaptchaCode":1,"CaptchaMsg":"OK","EvilLevel":0,"GetCaptchaTime":0,"RequestId":"bb271767-159f-4d86-bd35-56f6812116b8"}

            $resp_json = $resp->toJsonString();
            $resp_array = json_decode($resp_json, true);

            if($resp_array['CaptchaCode'] == 1){
                return true;
            }else{
                Log::error($resp_json);
                return false;
            }

        }
        catch(TencentCloudSDKException $e) {
            Log::error($e);
            return false;
        }
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
welcome come back
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
95
粉丝
24
喜欢
156
收藏
347
排名:323
访问:2.9 万
私信
所有博文
社区赞助商