腾讯云 图形验证码接入
新建图形验证应用#
首先进入控制台 console.cloud.tencent.com/captcha
「新建验证」后,可获得 APPID
、App Secret Key
开发文档#
云 API 密钥#
新建密钥
前端接入#
略过
服务器接入#
- 点击右上方
PHP SDK 使用说明
, 下载sdk
。 - 填入参数调试,复制自动生成的代码。
实践#
比如我将 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 协议》,转载必须注明作者和本文链接
推荐文章: