This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendCode.php
123 lines (110 loc) · 4.13 KB
/
sendCode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
header("Content-type: text/html; charset=utf-8");
$phoneNumber = $_POST['phone']; //收件人手机号
if(!is_numeric($phoneNumber) || strlen($phoneNumber) != 11){
die("手机号码不正确!");
}
$code = rand(1000,9999); //验证码
$TermOfValidity = 15; //验证码有效期
require_once 'public.php';
$pdo = new DataBase;
$db = $pdo->mysqlconn();
$sql = "INSERT INTO verification_code(phone, code, time, term) VALUES ('$phoneNumber','$code','".date("Y/m/d H:i:s")."','$TermOfValidity')";
$db->exec($sql);
try {
// appid 和 appkey
$appid = "appid";
$appkey = "appkey";
$singleSender = new SmsSingleSender($appid, $appkey);
// 指定模板单发
$params = array($code,$TermOfValidity);
$templId = "模板ID"; //模板ID
$sign = "签名"; //签名
$result = $singleSender->sendWithParam("86", $phoneNumber, $templId, $params, $sign, "", "");
$rsp = json_decode($result);
if($rsp->result){
$state = $rsp->result;
//echo "发送失败,错误代码:".$rsp->result.",原因:".$rsp->errmsg;
$JS = "发送验证码失败,错误代码:".$rsp->result.",原因:".$rsp->errmsg."!";
}else{
$state = "Success";
//echo "发送成功!";
$JS = "发送验证码成功!";
}
die($JS);
} catch (\Exception $e) {
echo var_dump($e);
die();
}
class SmsSenderUtil {
function getRandom() {
return rand(100000, 999999);
}
function calculateSigForTemplAndPhoneNumbers($appkey, $random, $curTime, $phoneNumbers) {
$phoneNumbersString = $phoneNumbers[0];
for ($i = 1; $i < count($phoneNumbers); $i++) {
$phoneNumbersString .= ("," . $phoneNumbers[$i]);
}
return hash("sha256", "appkey=".$appkey."&random=".$random
."&time=".$curTime."&mobile=".$phoneNumbersString);
}
function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber) {
$phoneNumbers = array($phoneNumber);
return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random, $curTime, $phoneNumbers);
}
function sendCurlPost($url, $dataObj) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$ret = curl_exec($curl);
if (true != $ret) {
$result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
} else {
$rsp = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
//if (200 != $rsp) {
// $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp . " " . curl_error($curl) ."\"}";
//} else {
$result = $ret;
//}
}
curl_close($curl);
return $result;
}
}
class SmsSingleSender {
var $url;
var $appid;
var $appkey;
var $util;
function __construct($appid, $appkey) {
$this->url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
$this->appid = $appid;
$this->appkey = $appkey;
$this->util = new SmsSenderUtil();
}
function sendWithParam($nationCode, $phoneNumber, $templId, $params, $sign, $extend = "", $ext = "") {
$random = $this->util->getRandom();
$curTime = time();
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
// 按照协议组织 post 包体
$data = new \stdClass();
$tel = new \stdClass();
$tel->nationcode = "".$nationCode;
$tel->mobile = "".$phoneNumber;
$data->tel = $tel;
$data->sig = $this->util->calculateSigForTempl($this->appkey, $random, $curTime, $phoneNumber);
$data->tpl_id = $templId;
$data->params = $params;
$data->sign = $sign;
$data->time = $curTime;
$data->extend = $extend;
$data->ext = $ext;
return $this->util->sendCurlPost($wholeUrl, $data);
}
}
?>