腾讯云-云智信短信验证码demo(php)
注意:url一定要使用http,亲自踩坑,使用https会报下面这个错误。如果https可以用请忽略本条
上代码
<?php
$dateTime = gmdate("I d F Y H:i:s");
$SecretId = '';//appid
$SecretKey = '';//appkey
$srcStr = "date: ".$dateTime."\n"."source: "."yousali";
$Authen = 'hmac id="'.$SecretId.'", algorithm="hmac-sha1", headers="date source", signature="';
$signStr = base64_encode(hash_hmac('sha1', $srcStr, $SecretKey, true));
$Authen = $Authen.$signStr."\"";
$mobile = '';//手机号
$code = rand(1000,9999);//验证码
$url = "http://service-4xrmju6b-1255399658.ap-beijing.apigateway.myqcloud.com/release/dxsms?mobile=".$mobile."¶m=code:".$code."&tpl_id=跟客服申请的模板id";
$headers = array(
'Host:service-4xrmju6b-1255399658.ap-beijing.apigateway.myqcloud.com',
'Accept:text/html, */*; q=0.01',
'Source: yousali',
'Date: '.$dateTime,
'Authorization: '.$Authen,
'X-Requested-With: XMLHttpRequest',
'Accept-Encoding: gzip, deflate, sdch'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}
?>
成功后返回
版权声明:本文为博主辛苦编辑,转载此文章须经博主同意,并请附上原文链接, https://blog.csdn.net/weixin_42237632/article/details/81112956