單獨調用騰訊云行為驗證碼
## **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、模板標簽