腾讯云短信api for php
撸一个不过瘾,一口气撸俩!等下个月发了工资,买华为云的短信包,再撸一个![大写的穷]
代码如下:
class SmsController extends SqlController {
// 发送短信验证码
public function sendVerifyCode($phone){
$random = rand(); // 本次请求的随机码
$time = time(); // 当前时间戳
$code = $this -> createCode(); // 获取验证码
$sig = $this -> sig($random, $time, $phone);
// 请求参数
$params = [
'params' => [$code], // 模板参数列表
'tel' => [
'mobile' => $phone, // 手机号
'nationcode' => 86 // 国家代码,中国为86
],
'sig' => $sig, // App凭证
'sign' => '', // 签名
'time' => $time, //时间戳
'tpl_id' => '', // 模板ID
'ext' => '' // session设置空
];
$result = $this -> post($params, $random);
// 响应
$result = json_decode($result, true);
if($result['result'] == 0) Common::response(1001, '发送成功');
else Common::response(3001, '发送失败,请稍后再试!');
}
// 生成sig
public function sig($random, $time, $phone){
$string = 'appkey='.APPKEY.'&random='.$random.'&time='.$time.'&mobile='.$phone;
$result = hash('sha256', $string);
return $result;
}
// 生成验证码
public function createCode(){
return rand(100000, 999999);
}
// 发送请求
public function post($data, $random){
$url = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid='.APPID.'&random='.$random;
$data = json_encode($data);
$config = [
'http' => [
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data,
'timeout' => 8
]
];
$config = stream_context_create($config);
$result = file_get_contents($url, false, $config);
return $result;
}
// 校验验证码 todo.
public function verify(){
$phone = $_GET['phone'];
$code = $_GET['code'];
}
}
上面代码仅提供思路,直接复制会报错!调用如下:
$SMS = new SmsController();
$SMS -> sendVerifyCode('手机号');
本文标签: 腾讯云短信