单独调用腾讯云行为验证码
## **html前端**
~~~
<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
~~~
~~~
<script>
var captcha1 = new TencentCaptcha('你的appId', function (res) {
if (res.ret === 0) {
$.post("{:url('login/doCaptcha')}", {
aid: res.appid,
randstr: res.randstr,
ticket: res.ticket
}, function (res2) {
//验证成功的逻辑
});
}
captcha1.show(); // 显示验证码
</script>
~~~
## **PHP后端**
~~~
//腾讯验证码验证
public function doCaptcha()
{
$params = $this->request->param();
// 进行在线验证
$host = 'captcha.tencentcloudapi.com';
$params = [
'CaptchaType' => 9,
'Ticket' => $params['ticket'],
'UserIp' => request()->ip(0, true),
'Randstr' => $params['randstr'],
'CaptchaAppId' => (int)$params['aid'],
'AppSecretKey' => 'aaaaaaaaaaaaaaaaaaaaaaa**'
];
$headers = $this->makeHeader($host, $params);
try {
$result = self::curl_post('https://'.$host, json_encode($params), $headers);
} catch (Exception $e) {
$this->error($e->getCode(), ['code' => '']);
}
$result = json_decode($result, true);
if (empty($result) || ! isset($result['Response']) || ! isset($result['Response']['CaptchaCode']) || $result['Response']['CaptchaCode'] != 1) {
$this->error(isset($result['Response']) && isset($result['Response']['CaptchaMsg']) && ! empty($result['Response']['CaptchaMsg']) ? $result['Response']['CaptchaMsg'] : "验证失败,请重试",
['code' => '']);
}
}
private function makeHeader($host, $params)
{
$headers = [
'X-TC-Action' => 'DescribeCaptchaResult',
'X-TC-Timestamp' => time(),
'X-TC-Version' => '2019-07-22',
'Content-Type' => 'application/json',
'Host' => $host
];
$algo = "TC3-HMAC-SHA256";
$date = gmdate("Y-m-d", $headers["X-TC-Timestamp"]);
$service = 'captcha';
$canonicalUri = '/';
$reqmethod = 'POST';
$canonicalQueryString = '';
$canonicalHeaders = "content-type:".$headers["Content-Type"]."\n"."host:".$headers["Host"]."\n";
$signedHeaders = "content-type;host";
$payloadHash = hash("SHA256", json_encode($params));
$canonicalRequest = $reqmethod."\n".$canonicalUri."\n".$canonicalQueryString."\n".$canonicalHeaders."\n".$signedHeaders."\n".$payloadHash;
$credentialScope = $date."/".$service."/tc3_request";
$hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
$str2sign = $algo."\n".$headers["X-TC-Timestamp"]."\n".$credentialScope."\n".$hashedCanonicalRequest;
$secretKey = 'ccccccccccccccccccccccc';
$signature = $this->signTC3($secretKey, $date, $service, $str2sign);
$sid = 'bbbbbbbbbbbbbbbbbbbb';
$auth = $algo." Credential=".$sid."/".$credentialScope.", SignedHeaders=content-type;host, Signature=".$signature;
$headers["Authorization"] = $auth;
$temp = [];
foreach ($headers as $key => $value) {
array_push($temp, $key.':'.$value);
}
return $temp;
}
protected function signTC3($skey, $date, $service, $str2sign)
{
$dateKey = hash_hmac("SHA256", $date, "TC3".$skey, true);
$serviceKey = hash_hmac("SHA256", $service, $dateKey, true);
$reqKey = hash_hmac("SHA256", "tc3_request", $serviceKey, true);
return hash_hmac("SHA256", $str2sign, $reqKey);
}
protected function curl_post($post_url, $post_data, $header = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
~~~
- thinkphp
- thinkphp笔记
- 后台登陆退出
- config配置
- 隐藏后台模块
- 单独调用腾讯云行为验证码
- api接口跨域问题
- api接口创建案例代码
- 使用gateway worker
- 使用swoole代码笔记
- 使用队列 think-queue笔记
- 后台布局
- MySQL
- 1、关于lnmp mysql的一个坑
- 2、mysql实现group by后取各分组的最新一条
- 其他
- 搞笑的注释代码
- 分页类
- nodejs 打包网址为exe
- 免费天气预报API接口
- Ajax
- 简单的ajax分页1
- 通用ajax-post提交
- 引用的类库文件
- Auth.php
- Auth.php权限控制对应的数据库表结构
- Layui.php
- Pinyin.php
- Random.php
- Tree.php
- Tree2.php
- Js-Jq
- Git的使用
- 3、bootstrap-datetimepicker实现两个时间范围输入
- CentOS安装SSR做梯子
- Python爬虫
- 1、安装Gerapy
- 2、安装Scrapy
- 3、Scrapy使用
- 4、Scrapy框架,爬取网站返回json数据(spider源码)
- 0、Python pip更换国内源(一句命令换源)
- 服务器运维
- 1、宝塔使用webhook更新服务器代码
- 2、搭建内网穿透
- 3、数据库主从同步
- 4、数据库复制
- hui-Shop问题
- 1、前端模板的注意事项
- 2、模板标签