腾讯短信服务精简版(PHP )

短信视乎已经被慢慢淡出平常的交流工具队列,但始终抹不去它的存在,短信验证码视乎从未被取代,此外在重要的信息通知的地位也是不可取的的。所以了解短信的使用是开发中很有必要的一环。

腾讯云的短信服务提供有100条内免费,所以方便开发测试。

申请短信服务并建立模板

https://console.cloud.tencent.com/sms

查看SDK

https://cloud.tencent.com/document/product/382/13410

提供了多种语言的SDK

PHP短信模板精简

实现短信模板的单次发送

 1 /**
 2 * @param string $nationCode  国家码,如 86 为中国
 3 * @param string $phoneNumber 不带国家码的手机号
 4 * @param int    $templId     模板 id
 5 * @param array  $params      模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数
 6 * @param string $sign        签名,如果填空串,系统会使用默认签名
 7 * @param string $extend      扩展码,可填空串
 8 * @param string $ext         服务端原样返回的参数,可填空串
 9 * @return string 应答json字符串,详细内容参见腾讯云协议文档
10 */
11 function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params, $sign = "", $extend = "", $ext = ""){
12         
13         $appid = 1400xxx;  //自己的短信appid
14         $appkey = "d80axxxxx"; //自己的短信appkey
15         
16         $random = rand(100000, 999999);//生成随机数
17         $curTime = time();
18         $wholeUrl = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms". "?sdkappid=" . $appid . "&random=" . $random;
19 
20         // 按照协议组织 post 包体
21         $data = new \stdClass();//创建一个没有成员方法和属性的空对象
22         $tel = new \stdClass();
23         $tel->nationcode = "".$nationCode;
24         $tel->mobile = "".$phoneNumber;
25         $data->tel = $tel;
26         $data->sig=hash("sha256", "appkey=".$appkey."&random=".$random."&time=".$curTime."&mobile=".$phoneNumber);// 生成签名
27         $data->tpl_id = $templId;
28         $data->params = $params;
29         $data->sign = $sign;
30         $data->time = $curTime;
31         $data->extend = $extend;
32         $data->ext = $ext;
33 
34         return sendCurlPost($wholeUrl, $data);
35 }
36 /**
37 * 发送请求
38 *
39 * @param string $url      请求地址
40 * @param array  $dataObj  请求内容
41 * @return string 应答json字符串
42 */
43 function sendCurlPost($url, $dataObj){
44         $curl = curl_init();
45         curl_setopt($curl, CURLOPT_URL, $url);
46         curl_setopt($curl, CURLOPT_HEADER, 0);
47         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
48         curl_setopt($curl, CURLOPT_POST, 1);
49         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
50         curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
51         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
52         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
53         $ret = curl_exec($curl);
54         if (false == $ret) {
55             // curl_exec failed
56             $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
57         } else {
58             $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
59             if (200 != $rsp) {
60                 $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp
61                         . " " . curl_error($curl) ."\"}";
62             } else {
63                 $result = $ret;
64             }
65         }
66         curl_close($curl);
67 
68         return $result;
69 }

测试代码:

 1 function xx(){
 2 $templId = 286xxx; //自己短信模板id
 3 $phoneNumber1="159xxxxx";//接受短信手机号码
 4 try {
 5 //模板占位数据
 6 $params = array("数据1","数据2");
 7 $result = sendWithParam("86", $phoneNumber1, $templId,$params, "", "", "");
 8 echo $result;//输出成功的json结果
 9 } catch(\Exception $e) {
10      echo var_dump($e);//输出异常信息
11     }
12 }
posted @ 2019-04-19 16:37  东小东  阅读(881)  评论(0编辑  收藏  举报