博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThinkPHP整合短信通知功能
阅读量:5124 次
发布时间:2019-06-13

本文共 14661 字,大约阅读时间需要 48 分钟。

1.使用的“云之讯”云通讯的接口,注册,登录。

地址:

2.

3.

 

 4.

5.按规范与实际需求,填写相应的信息,注意要审核通过!

--------------------------------------------------------------------------------------------------------------------------------------------------------

6.核心代码:整合到Vender.如:

7.代码:

accountSid = isset($options['accountsid']) ? $options['accountsid'] : ''; $this->token = isset($options['token']) ? $options['token'] : ''; $this->timestamp = date("YmdHis") + 7200; } else { throw new Exception("非法参数"); } } /** * @return string * 包头验证信息,使用Base64编码(账户Id:时间戳) */ private function getAuthorization() { $data = $this->accountSid . ":" . $this->timestamp; return trim(base64_encode($data)); } /** * @return string * 验证参数,URL后必须带有sig参数,sig= MD5(账户Id + 账户授权令牌 + 时间戳,共32位)(注:转成大写) */ private function getSigParameter() { $sig = $this->accountSid . $this->token . $this->timestamp; return strtoupper(md5($sig)); } /** * @param $url * @param string $type * @return mixed|string */ private function getResult($url, $body = null, $type = 'json',$method) { $data = $this->connection($url,$body,$type,$method); if (isset($data) && !empty($data)) { $result = $data; } else { $result = '没有返回数据'; } return $result; } /** * @param $url * @param $type * @param $body post数据 * @param $method post或get * @return mixed|string */ private function connection($url, $body, $type,$method) { if ($type == 'json') { $mine = 'application/json'; } else { $mine = 'application/xml'; } if (function_exists("curl_init")) { $header = array( 'Accept:' . $mine, 'Content-Type:' . $mine . ';charset=utf-8', 'Authorization:' . $this->getAuthorization(), ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); if($method == 'post'){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$body); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close($ch); } else { $opts = array(); $opts['http'] = array(); $headers = array( "method" => strtoupper($method), ); $headers[]= 'Accept:'.$mine; $headers['header'] = array(); $headers['header'][] = "Authorization: ".$this->getAuthorization(); $headers['header'][]= 'Content-Type:'.$mine.';charset=utf-8'; if(!empty($body)) { $headers['header'][]= 'Content-Length:'.strlen($body); $headers['content']= $body; } $opts['http'] = $headers; $result = file_get_contents($url, false, stream_context_create($opts)); } return $result; } /** * @param string $type 默认json,也可指定xml,否则抛出异常 * @return mixed|string 返回指定$type格式的数据 * @throws Exception */ public function getDevinfo($type = 'json') { if ($type == 'json') { $type = 'json'; } elseif ($type == 'xml') { $type = 'xml'; } else { throw new Exception("只能json或xml,默认为json"); } $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '?sig=' . $this->getSigParameter(); $data = $this->getResult($url,null,$type,'get'); return $data; } /** * @param $appId 应用ID * @param $clientType 计费方式。0 开发者计费;1 云平台计费。默认为0. * @param $charge 充值的金额 * @param $friendlyName 昵称 * @param $mobile 手机号码 * @return json/xml */ public function applyClient($appId, $clientType, $charge, $friendlyName, $mobile, $type = 'json') { $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Clients?sig=' . $this->getSigParameter(); if ($type == 'json') { $body_json = array(); $body_json['client'] = array(); $body_json['client']['appId'] = $appId; $body_json['client']['clientType'] = $clientType; $body_json['client']['charge'] = $charge; $body_json['client']['friendlyName'] = $friendlyName; $body_json['client']['mobile'] = $mobile; $body = json_encode($body_json); } elseif ($type == 'xml') { $body_xml = '
'.$appId.'
'.$clientType.'
'.$charge.'
'.$friendlyName.'
'.$mobile.'
'; $body = trim($body_xml); } else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $clientNumber * @param $appId * @param string $type * @return mixed|string * @throws Exception */ public function releaseClient($clientNumber,$appId,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/dropClient?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array(); $body_json['client'] = array(); $body_json['client']['clientNumber'] = $clientNumber; $body_json['client']['appId'] = $appId; $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$clientNumber.'
'.$appId.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $appId * @param $start * @param $limit * @param string $type * @return mixed|string * @throws Exception */ public function getClientList($appId,$start,$limit,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/clientList?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array('client'=>array( 'appId'=>$appId, 'start'=>$start, 'limit'=>$limit )); $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$appId.'
'.$start.'
'.$limit.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $appId * @param $clientNumber * @param string $type * @return mixed|string * @throws Exception */ public function getClientInfo($appId,$clientNumber,$type = 'json'){ if ($type == 'json') { $type = 'json'; } elseif ($type == 'xml') { $type = 'xml'; } else { throw new Exception("只能json或xml,默认为json"); } $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '?sig=' . $this->getSigParameter(). '&clientNumber='.$clientNumber.'&appId='.$appId; $data = $this->getResult($url,null,$type,'get'); return $data; } /** * @param $appId * @param $mobile * @param string $type * @return mixed|string * @throws Exception */ public function getClientInfoByMobile($appId,$mobile,$type = 'json'){ if ($type == 'json') { $type = 'json'; } elseif ($type == 'xml') { $type = 'xml'; } else { throw new Exception("只能json或xml,默认为json"); } $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/ClientsByMobile?sig=' . $this->getSigParameter(). '&mobile='.$mobile.'&appId='.$appId; $data = $this->getResult($url,null,$type,'get'); return $data; } /** * @param $appId * @param $date * @param string $type * @return mixed|string * @throws Exception */ public function getBillList($appId,$date,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/billList?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array('appBill'=>array( 'appId'=>$appId, 'date'=>$date, )); $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$appId.'
'.$date.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $appId * @param $clientNumber * @param $chargeType * @param $charge * @param string $type * @return mixed|string * @throws Exception */ public function chargeClient($appId,$clientNumber,$chargeType,$charge,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/chargeClient?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array('client'=>array( 'appId'=>$appId, 'clientNumber'=>$clientNumber, 'chargeType'=>$chargeType, 'charge'=>$charge )); $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$clientNumber.'
'.$chargeType.'
'.$charge.'
'.$appId.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $appId * @param $fromClient * @param $to * @param null $fromSerNum * @param null $toSerNum * @param string $type * @return mixed|string * @throws Exception */ public function callBack($appId,$fromClient,$to,$fromSerNum=null,$toSerNum=null,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/callBack?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array('callback'=>array( 'appId'=>$appId, 'fromClient'=>$fromClient, 'fromSerNum'=>$fromSerNum, 'to'=>$to, 'toSerNum'=>$toSerNum )); $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$fromClient.'
'.$fromSerNum.'
'.$to.'
'.$toSerNum.'
'.$appId.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $appId * @param $verifyCode * @param $to * @param string $type * @return mixed|string * @throws Exception */ public function voiceCode($appId,$verifyCode,$to,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/voiceCode?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array('voiceCode'=>array( 'appId'=>$appId, 'verifyCode'=>$verifyCode, 'to'=>$to )); $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$verifyCode.'
'.$to.'
'.$appId.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; } /** * @param $appId * @param $to * @param $templateId * @param null $param * @param string $type * @return mixed|string * @throws Exception */ public function templateSMS($appId,$to,$templateId,$param=null,$type = 'json'){ $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Messages/templateSMS?sig=' . $this->getSigParameter(); if($type == 'json'){ $body_json = array('templateSMS'=>array( 'appId'=>$appId, 'templateId'=>$templateId, 'to'=>$to, 'param'=>$param )); $body = json_encode($body_json); }elseif($type == 'xml'){ $body_xml = '
'.$templateId.'
'.$to.'
'.$param.'
'.$appId.'
'; $body = trim($body_xml); }else { throw new Exception("只能json或xml,默认为json"); } $data = $this->getResult($url, $body, $type,'post'); return $data; }}

8.在config配置文件中做相关的配置;

9.使用与应用

10.测试,成功!

转载于:https://www.cnblogs.com/wuheng1991/p/6635716.html

你可能感兴趣的文章
uva 387 A Puzzling Problem (回溯)
查看>>
12.2日常
查看>>
同步代码时忽略maven项目 target目录
查看>>
Oracle中包的创建
查看>>
团队开发之个人博客八(4月27)
查看>>
发布功能完成
查看>>
【原】小程序常见问题整理
查看>>
C# ITextSharp pdf 自动打印
查看>>
【Java】synchronized与lock的区别
查看>>
django高级应用(分页功能)
查看>>
【转】Linux之printf命令
查看>>
关于PHP会话:session和cookie
查看>>
STM32F10x_RTC秒中断
查看>>
display:none和visiblity:hidden区别
查看>>
C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现。
查看>>
牛的障碍Cow Steeplechase
查看>>
Zookeeper选举算法原理
查看>>
3月29日AM
查看>>
利用IP地址查询接口来查询IP归属地
查看>>
HTML元素定义 ID,Class,Style的优先级
查看>>