腾讯短信服务精简版(PHP )
2019-04-19 16:37
211 查看
短信视乎已经被慢慢淡出平常的交流工具队列,但始终抹不去它的存在,短信验证码视乎从未被取代,此外在重要的信息通知的地位也是不可取的的。所以了解短信的使用是开发中很有必要的一环。
腾讯云的短信服务提供有100条内免费,所以方便开发测试。
申请短信服务并建立模板
https://console.cloud.tencent.com/sms
查看SDK
https://cloud.tencent.com/document/product/382/13410
提供了多种语言的SDK
PHP短信模板精简
实现短信模板的单次发送
/** * @param string $nationCode 国家码,如 86 为中国 * @param string $phoneNumber 不带国家码的手机号 * @param int $templId 模板 id * @param array $params 模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数 * @param string $sign 签名,如果填空串,系统会使用默认签名 * @param string $extend 扩展码,可填空串 * @param string $ext 服务端原样返回的参数,可填空串 * @return string 应答json字符串,详细内容参见腾讯云协议文档 */ function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params, $sign = "", $extend = "", $ext = ""){ $appid = 1400xxx; //自己的短信appid $appkey = "d80axxxxx"; //自己的短信appkey $random = rand(100000, 999999);//生成随机数 $curTime = time(); $wholeUrl = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms". "?sdkappid=" . $appid . "&random=" . $random; // 按照协议组织 post 包体 $data = new \stdClass();//创建一个没有成员方法和属性的空对象 $tel = new \stdClass(); $tel->nationcode = "".$nationCode; $tel->mobile = "".$phoneNumber; $data->tel = $tel; $data->sig=hash("sha256", "appkey=".$appkey."&random=".$random."&time=".$curTime."&mobile=".$phoneNumber);// 生成签名 $data->tpl_id = $templId; $data->params = $params; $data->sign = $sign; $data->time = $curTime; $data->extend = $extend; $data->ext = $ext; return sendCurlPost($wholeUrl, $data); } /** * 发送请求 * * @param string $url 请求地址 * @param array $dataObj 请求内容 * @return string 应答json字符串 */ 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_CONNECTTIMEOUT, 60); 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 (false == $ret) { // curl_exec failed $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}"; } else { $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE); if (200 != $rsp) { $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp . " " . curl_error($curl) ."\"}"; } else { $result = $ret; } } curl_close($curl); return $result; }
测试代码:
function xx(){ $templId = 286xxx; //自己短信模板id $phoneNumber1="159xxxxx";//接受短信手机号码 try { //模板占位数据 $params = array("数据1","数据2"); $result = sendWithParam("86", $phoneNumber1, $templId,$params, "", "", ""); echo $result;//输出成功的json结果 } catch(\Exception $e) { echo var_dump($e);//输出异常信息 } }
相关文章推荐
- 阿里云PHP SMS短信服务验证码发送方法
- 阿里大鱼短信接口PHP版,精简版阿里大鱼短信SMS发送接口PHP实例
- PHP短信发送服务 youe短信企业服务
- php 阿里云短信服务及阿里大于实现短信验证码的发送
- 腾讯与阅文技术合作,微服务框架 Tars 再添PHP
- other|使用php调用aws sns服务发送短信通知
- 阿里云PHP-SMS短信服务——验证码发送教程
- 腾讯与阅文技术合作,微服务框架 Tars 再添PHP
- eoLinker-API_Shop_短信服务接口-调用示例代码,支持PHP、Python、Java等语言
- php 阿里云短信服务及阿里大鱼实现短信验证码的发送
- php使用aws的sns服务初探(主要是短信服务)
- PHP阿里云短信服务
- 阿里云PHP-SMS短信服务——验证码发送教程
- PHP开发阿里云短信服务接口
- 基于PHP的阿里云短信发送服务。防坑!!!
- aliyun短信服务,php采用示例代码报Can not find endpoint to access
- 腾讯与阅文技术合作 微服务框架Tars再添PHP
- PHP短信接口分享:适用于创蓝253平台下的短信验证码、短信服务接口
- php版-阿里大于短信验证码服务教程
- 阿里云短信服务SDK踩坑(php)