ThinkPHP整合短信通知功能
1.使用的“云之讯”云通讯的接口,注册,登录。
2.
3.
4.
5.按规范与实际需求,填写相应的信息,注意要审核通过!
--------------------------------------------------------------------------------------------------------------------------------------------------------
6.核心代码:整合到Vender.如:
7.代码:
<?php
class Ucpaas
{ /**
* 云之讯REST API版本号。当前版本号为:2014-06-30
*/
const SoftVersion = "2014-06-30";
/**
* API请求地址
*/
const BaseUrl = "https://api.ucpaas.com/";
/**
* @var string
* 开发者账号ID。由32个英文字母和阿拉伯数字组成的开发者账号唯一标识符。
*/
private $accountSid;
/**
* @var string
* 开发者账号TOKEN
*/
private $token;
/**
* @var string
* 时间戳
*/
private $timestamp; /**
* @param $options 数组参数必填
* $options = array(
*
* )
* @throws Exception
*/
public function __construct($options)
{
if (is_array($options) && !empty($options)) {
$this->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 = '<?xml version="1.0" encoding="utf-8"?>
<client><appId>'.$appId.'</appId>
<clientType>'.$clientType.'</clientType>
<charge>'.$charge.'</charge>
<friendlyName>'.$friendlyName.'</friendlyName>
<mobile>'.$mobile.'</mobile>
</client>';
$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 = '<?xml version="1.0" encoding="utf-8"?>
<client>
<clientNumber>'.$clientNumber.'</clientNumber>
<appId>'.$appId.'</appId >
</client>';
$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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<client>
<appId>'.$appId.'</appId>
<start>'.$start.'</start>
<limit>'.$limit.'</limit>
</client>';
$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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<appBill>
<appId>'.$appId.'</appId>
<date>'.$date.'</date>
</appBill>';
$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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<client>
<clientNumber>'.$clientNumber.'</clientNumber>
<chargeType>'.$chargeType.'</chargeType>
<charge>'.$charge.'</charge>
<appId>'.$appId.'</appId>
</client>';
$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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<callback>
<fromClient>'.$fromClient.'</clientNumber>
<fromSerNum>'.$fromSerNum.'</chargeType>
<to>'.$to.'</charge>
<toSerNum>'.$toSerNum.'</toSerNum>
<appId>'.$appId.'</appId>
</callback>';
$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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<voiceCode>
<verifyCode>'.$verifyCode.'</clientNumber>
<to>'.$to.'</charge>
<appId>'.$appId.'</appId>
</voiceCode>';
$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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<templateSMS>
<templateId>'.$templateId.'</templateId>
<to>'.$to.'</to>
<param>'.$param.'</param>
<appId>'.$appId.'</appId>
</templateSMS>';
$body = trim($body_xml);
}else {
throw new Exception("只能json或xml,默认为json");
}
$data = $this->getResult($url, $body, $type,'post');
return $data;
}
}
8.在config配置文件中做相关的配置;
9.使用与应用
10.测试,成功!
ThinkPHP整合短信通知功能的更多相关文章
- Thinkphp整合各个功能
thinkphp整合Auth权限管理.支付宝.微信支付.阿里oss.友盟推送.融云即时通讯.云通讯短信.Email.Excel.PDF等等: 基于thinkphp扩展了大量的功能:而不改动thinkp ...
- ThinkPHP整合支付宝担保交易
ThinkPHP整合支付宝担保交易本代码参考大神 http://www.thinkphp.cn/code/240.html 的思路 1.登陆支付宝后台,下载担保交易的集成包. 2.下载完成后的文件说明 ...
- ThinkPHP 整合Bootstrap Ajax分页
ThinkPHP Ajax分页代码 publicfunction index() { $where=array(); $name = I('name'); if(!empty($name)){ $wh ...
- thinkphp整合系列之支付宝RSA加密方式
thinkphp整合系列之支付宝RSA加密方式上篇博客写的是MD5加密方式:thinkphp整合系列之支付宝MD5加密方式扫码支付http://baijunyao.com/article/75 但是呢 ...
- ThinkPHP整合百度Ueditor
文章来源:http://www.thinkphp.cn/code/267.html ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如: ...
- ThinkPHP整合百度Ueditor图文教程
ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...
- thinkphp整合系列之phpexcel生成生成excel文件
在后台管理中会经常需要将数据生成excel表格的: php生成excel有两种方案: 一种是通过phpexcel生成xls格式的表格文件: 另一种则直接通过逗号换行生成csv格式的表格文件: 这里先讲 ...
- thinkphp整合系列之极验滑动验证码
对于建站的筒子们来说:垃圾广告真是让人深恶痛绝:为了清净:搞个难以识别的验证码吧:又被用户各种吐槽:直到后来出现了极验这个滑动的验证码:这真是一个体验好安全高的方案:官网:http://www.gee ...
- YII2与Thinkphp整合阿里云OSS
前言: 如果上传的文件都和网站程序源代码放在一起:那是有相当多的弊端的: 1:静态文件会占用大量带宽: 2:服务器的成本略高: 常规的做法是把php源代码放到一台服务器上:图片等静态文件放在另一台服务 ...
随机推荐
- Python 遍历set
遍历set 由于 set 也是一个集合,所以,遍历 set 和遍历 list 类似,都可以通过 for 循环实现. 直接使用 for 循环可以遍历 set 的元素: weekdays = set([' ...
- JavaScript数组方法说明
JavaScript的数组方法有: http://www.w3school.com.cn/jsref/jsref_obj_array.asp 其中:concat.join和slice方法都不会修改原数 ...
- Android SDK镜像的介绍使用【转发】
由于一些原因,Google相关很多服务都无法访问,所以在很多时候我们SDK也无法升级,当然通过技术手段肯定可以解决,但是比较麻烦,而且下载速度也不怎么样. 这里笔者介绍一个国内的Android镜像站, ...
- 【APP接口开发】chrome浏览器DHC工具安装使用(亲测有效)
1.DHC文件获取地址:http://chromecj.com/web-development/2015-08/549/download.html 2.chrome安装DHC插件教程和步骤:http: ...
- 对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
一.对象语义与值语义 1.值语义是指对象的拷贝与原对象无关.拷贝之后就与原对象脱离关系,彼此独立互不影响(深拷贝).比如说int,C++中的内置类型都是值语义,前面学过的三个标准库类型string,v ...
- SEH, SAFESEH相关
SEH, SAFESEH相关 1,触发seh异常让目标程序Read/Write无效地址,如果和栈底相邻的内存只读,尝试覆盖超出栈底 2,如何找到(显示)要覆盖的SEHod语法:dd fs:[0]sof ...
- C#中Out和Ref參数修饰符
在编程过程中对于函数之间的參数的传递一般分为两种:传值和传地址. 以下为大家分析一下. 传值 比方你又一份文档,假设採用传值的话.相当于我复制了一份,因此我对我这份文档的改动都不会影响到你的那份.假设 ...
- 解决 scrapy 爬虫出现Forbidden by robots.txt
我们在爬取网站的时候,scrapy 默认的是遵循 robots.txt 协议,怎么破解这个文件 操作很简单,找到setting 文件 直接改成
- atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc
atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc ...
- cocos2d-x 之 CCProgressTimer
--绕圆心转动的进度动画 local function SpriteProgressToRadial() local leftProgress = CCProgressTimer:create(CCS ...