接口说明
/**
* 初始化
* @param serverIP 必选参数 服务器地址
* @param serverPort 必选参数 服务器端口
* @param softVersion 必选参数 REST版本号
*/
REST($serverIP,$serverPort,$softVersion)
/**
* 设置主帐号
*
* @param AccountSid 主帐号
* @param AccountToken 主帐号Token
*/
function setAccount($AccountSid,$AccountToken)
/**
* 设置应用ID
*
* @param AppId 应用ID
*/
function setAppId($AppId)
/**
* 打印日志
*
* @param log 日志内容
*/
function showlog($log)
/**
* 发起HTTPS请求
* @param url 请求地址
* @param data 请求包体
* @param header 请求包头
* @param post 请求方式 默认为1 1:post,0:get
*/
function curl_post($url,$data,$header,$post=1)
/**
* 发送模板短信
* @param to 短信接收彿手机号码集合,用英文逗号分开
* @param datas 内容数据 格式为数组 例如:array('Marry','Alon'),如不需替换请填 null
* @param $tempId 模板Id,测试应用和未上线应用使用测试模板请填写1,正式应用上线后填写已申请审核通过的模板ID
*/
function sendTemplateSMS($to,$datas,$tempId)
1 <?php 2 /* 3 * Copyright (c) 2014 The CCP project authors. All Rights Reserved. 4 * 5 * Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license 6 * that can be found in the LICENSE file in the root of the web site. 7 * 8 * http://www.yuntongxun.com 9 * 10 * An additional intellectual property rights grant can be found 11 * in the file PATENTS. All contributing project authors may 12 * be found in the AUTHORS file in the root of the source tree. 13 */ 14 15 16 class REST { 17 private $AccountSid; 18 private $AccountToken; 19 private $AppId; 20 private $ServerIP; 21 private $ServerPort; 22 private $SoftVersion; 23 private $Batch; //时间戳 24 private $BodyType = "xml";//包体格式,可填值:json 、xml 25 private $enabeLog = true; //日志开关。可填值:true、 26 private $Filename="./log.txt"; //日志文件 27 private $Handle; 28 function __construct($ServerIP,$ServerPort,$SoftVersion) 29 { 30 $this->Batch = date("YmdHis"); 31 $this->ServerIP = $ServerIP; 32 $this->ServerPort = $ServerPort; 33 $this->SoftVersion = $SoftVersion; 34 $this->Handle = fopen($this->Filename, 'a'); 35 } 36 37 /** 38 * 设置主帐号 39 * 40 * @param AccountSid 主帐号 41 * @param AccountToken 主帐号Token 42 */ 43 function setAccount($AccountSid,$AccountToken){ 44 $this->AccountSid = $AccountSid; 45 $this->AccountToken = $AccountToken; 46 } 47 48 49 /** 50 * 设置应用ID 51 * 52 * @param AppId 应用ID 53 */ 54 function setAppId($AppId){ 55 $this->AppId = $AppId; 56 } 57 58 /** 59 * 打印日志 60 * 61 * @param log 日志内容 62 */ 63 function showlog($log){ 64 if($this->enabeLog){ 65 fwrite($this->Handle,$log."\n"); 66 } 67 } 68 69 /** 70 * 发起HTTPS请求 71 */ 72 function curl_post($url,$data,$header,$post=1) 73 { 74 //初始化curl 75 $ch = curl_init(); 76 //参数设置 77 $res= curl_setopt ($ch, CURLOPT_URL,$url); 78 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 79 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 80 curl_setopt ($ch, CURLOPT_HEADER, 0); 81 curl_setopt($ch, CURLOPT_POST, $post); 82 if($post) 83 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 84 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 85 curl_setopt($ch,CURLOPT_HTTPHEADER,$header); 86 $result = curl_exec ($ch); 87 //连接失败 88 if($result == FALSE){ 89 if($this->BodyType=='json'){ 90 $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}"; 91 } else { 92 $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>"; 93 } 94 } 95 96 curl_close($ch); 97 return $result; 98 } 99 100 101 102 /** 103 * 发送模板短信 104 * @param to 短信接收彿手机号码集合,用英文逗号分开 105 * @param datas 内容数据 106 * @param $tempId 模板Id 107 */ 108 function sendTemplateSMS($to,$datas,$tempId) 109 { 110 //主帐号鉴权信息验证,对必选参数进行判空。 111 $auth=$this->accAuth(); 112 if($auth!=""){ 113 return $auth; 114 } 115 // 拼接请求包体 116 if($this->BodyType=="json"){ 117 $data=""; 118 for($i=0;$i<count($datas);$i++){ 119 $data = $data. "'".$datas[$i]."',"; 120 } 121 $body= "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[".$data."]}"; 122 }else{ 123 $data=""; 124 for($i=0;$i<count($datas);$i++){ 125 $data = $data. "<data>".$datas[$i]."</data>"; 126 } 127 $body="<TemplateSMS> 128 <to>$to</to> 129 <appId>$this->AppId</appId> 130 <templateId>$tempId</templateId> 131 <datas>".$data."</datas> 132 </TemplateSMS>"; 133 } 134 $this->showlog("request body = ".$body); 135 // 大写的sig参数 136 $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch)); 137 // 生成请求URL 138 $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig"; 139 $this->showlog("request url = ".$url); 140 // 生成授权:主帐户Id + 英文冒号 + 时间戳。 141 $authen = base64_encode($this->AccountSid . ":" . $this->Batch); 142 // 生成包头 143 $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen"); 144 // 发送请求 145 $result = $this->curl_post($url,$body,$header); 146 $this->showlog("response body = ".$result); 147 if($this->BodyType=="json"){//JSON格式 148 $datas=json_decode($result); 149 }else{ //xml格式 150 $datas = simplexml_load_string(trim($result," \t\n\r")); 151 } 152 // if($datas == FALSE){ 153 // $datas = new stdClass(); 154 // $datas->statusCode = '172003'; 155 // $datas->statusMsg = '返回包体错误'; 156 // } 157 //重新装填数据 158 if($datas->statusCode==0){ 159 if($this->BodyType=="json"){ 160 $datas->TemplateSMS =$datas->templateSMS; 161 unset($datas->templateSMS); 162 } 163 } 164 165 return $datas; 166 } 167 168 /** 169 * 主帐号鉴权 170 */ 171 function accAuth() 172 { 173 if($this->ServerIP==""){ 174 $data = new stdClass(); 175 $data->statusCode = '172004'; 176 $data->statusMsg = 'IP为空'; 177 return $data; 178 } 179 if($this->ServerPort<=0){ 180 $data = new stdClass(); 181 $data->statusCode = '172005'; 182 $data->statusMsg = '端口错误(小于等于0)'; 183 return $data; 184 } 185 if($this->SoftVersion==""){ 186 $data = new stdClass(); 187 $data->statusCode = '172013'; 188 $data->statusMsg = '版本号为空'; 189 return $data; 190 } 191 if($this->AccountSid==""){ 192 $data = new stdClass(); 193 $data->statusCode = '172006'; 194 $data->statusMsg = '主帐号为空'; 195 return $data; 196 } 197 if($this->AccountToken==""){ 198 $data = new stdClass(); 199 $data->statusCode = '172007'; 200 $data->statusMsg = '主帐号令牌为空'; 201 return $data; 202 } 203 if($this->AppId==""){ 204 $data = new stdClass(); 205 $data->statusCode = '172012'; 206 $data->statusMsg = '应用ID为空'; 207 return $data; 208 } 209 } 210 } 211 ?>
1 <?php 2 /* 3 * Copyright (c) 2014 The CCP project authors. All Rights Reserved. 4 * 5 * Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license 6 * that can be found in the LICENSE file in the root of the web site. 7 * 8 * http://www.yuntongxun.com 9 * 10 * An additional intellectual property rights grant can be found 11 * in the file PATENTS. All contributing project authors may 12 * be found in the AUTHORS file in the root of the source tree. 13 */ 14 15 include_once("./CCPRestSmsSDK.php"); 16 17 //主帐号,对应开官网发者主账号下的 ACCOUNT SID 18 $accountSid= ''; 19 20 //主帐号令牌,对应官网开发者主账号下的 AUTH TOKEN 21 $accountToken= ''; 22 23 //应用Id,在官网应用列表中点击应用,对应应用详情中的APP ID 24 //在开发调试的时候,可以使用官网自动为您分配的测试Demo的APP ID 25 $appId=''; 26 27 //请求地址 28 //沙盒环境(用于应用开发调试):sandboxapp.cloopen.com 29 //生产环境(用户应用上线使用):app.cloopen.com 30 $serverIP='app.cloopen.com'; 31 32 33 //请求端口,生产环境和沙盒环境一致 34 $serverPort='8883'; 35 36 //REST版本号,在官网文档REST介绍中获得。 37 $softVersion='2013-12-26'; 38 39 40 /** 41 * 发送模板短信 42 * @param to 手机号码集合,用英文逗号分开 43 * @param datas 内容数据 格式为数组 例如:array('Marry','Alon'),如不需替换请填 null 44 * @param $tempId 模板Id,测试应用和未上线应用使用测试模板请填写1,正式应用上线后填写已申请审核通过的模板ID 45 */ 46 function sendTemplateSMS($to,$datas,$tempId) 47 { 48 // 初始化REST SDK 49 global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion; 50 $rest = new REST($serverIP,$serverPort,$softVersion); 51 $rest->setAccount($accountSid,$accountToken); 52 $rest->setAppId($appId); 53 54 // 发送模板短信 55 echo "Sending TemplateSMS to $to <br/>"; 56 $result = $rest->sendTemplateSMS($to,$datas,$tempId); 57 if($result == NULL ) { 58 echo "result error!"; 59 break; 60 } 61 if($result->statusCode!=0) { 62 echo "error code :" . $result->statusCode . "<br>"; 63 echo "error msg :" . $result->statusMsg . "<br>"; 64 //TODO 添加错误处理逻辑 65 }else{ 66 echo "Sendind TemplateSMS success!<br/>"; 67 // 获取返回信息 68 $smsmessage = $result->TemplateSMS; 69 echo "dateCreated:".$smsmessage->dateCreated."<br/>"; 70 echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>"; 71 //TODO 添加成功处理逻辑 72 } 73 } 74 75 //Demo调用 76 //**************************************举例说明*********************************************************************** 77 //*假设您用测试Demo的APP ID,则需使用默认模板ID 1,发送手机号是13800000000,传入参数为6532和5,则调用方式为 * 78 //*result = sendTemplateSMS("13800000000" ,array('6532','5'),"1"); * 79 //*则13800000000手机号收到的短信内容是:【云通讯】您使用的是云通讯短信模板,您的验证码是6532,请于5分钟内正确输入 * 80 //********************************************************************************************************************* 81 sendTemplateSMS("",array('',''),"");//手机号码,替换内容数组,模板ID 82 ?>