CCPRestSmsSDK.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. class REST {
  15. private $AccountSid;
  16. private $AccountToken;
  17. private $AppId;
  18. private $ServerIP;
  19. private $ServerPort;
  20. private $SoftVersion;
  21. private $Batch; //时间戳
  22. private $BodyType = "xml";//包体格式,可填值:json 、xml
  23. private $enabeLog = true; //日志开关。可填值:true、
  24. private $Filename="./log.txt"; //日志文件
  25. private $Handle;
  26. function __construct($ServerIP,$ServerPort,$SoftVersion)
  27. {
  28. $this->Batch = date("YmdHis");
  29. $this->ServerIP = $ServerIP;
  30. $this->ServerPort = $ServerPort;
  31. $this->SoftVersion = $SoftVersion;
  32. $this->Handle = fopen($this->Filename, 'a');
  33. }
  34. /**
  35. * 设置主帐号
  36. *
  37. * @param AccountSid 主帐号
  38. * @param AccountToken 主帐号Token
  39. */
  40. function setAccount($AccountSid,$AccountToken){
  41. $this->AccountSid = $AccountSid;
  42. $this->AccountToken = $AccountToken;
  43. }
  44. /**
  45. * 设置应用ID
  46. *
  47. * @param AppId 应用ID
  48. */
  49. function setAppId($AppId){
  50. $this->AppId = $AppId;
  51. }
  52. /**
  53. * 打印日志
  54. *
  55. * @param log 日志内容
  56. */
  57. function showlog($log){
  58. return;
  59. if($this->enabeLog){
  60. fwrite($this->Handle,$log."\n");
  61. }
  62. }
  63. /**
  64. * 发起HTTPS请求
  65. */
  66. function curl_post($url,$data,$header,$post=1)
  67. {
  68. //初始化curl
  69. $ch = curl_init();
  70. //参数设置
  71. $res= curl_setopt ($ch, CURLOPT_URL,$url);
  72. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  73. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  74. curl_setopt ($ch, CURLOPT_HEADER, 0);
  75. curl_setopt($ch, CURLOPT_POST, $post);
  76. if($post)
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  78. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  79. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  80. $result = curl_exec ($ch);
  81. //连接失败
  82. if($result == FALSE){
  83. if($this->BodyType=='json'){
  84. $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}";
  85. } else {
  86. $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>";
  87. }
  88. }
  89. curl_close($ch);
  90. return $result;
  91. }
  92. /**
  93. * 发送模板短信
  94. * @param to 短信接收彿手机号码集合,用英文逗号分开
  95. * @param datas 内容数据
  96. * @param $tempId 模板Id
  97. */
  98. function sendTemplateSMS($to,$datas,$tempId)
  99. {
  100. //主帐号鉴权信息验证,对必选参数进行判空。
  101. $auth=$this->accAuth();
  102. if($auth!=""){
  103. return $auth;
  104. }
  105. // 拼接请求包体
  106. if($this->BodyType=="json"){
  107. $data="";
  108. for($i=0;$i<count($datas);$i++){
  109. $data = $data. "'".$datas[$i]."',";
  110. }
  111. $body= "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[".$data."]}";
  112. }else{
  113. $data="";
  114. for($i=0;$i<count($datas);$i++){
  115. $data = $data. "<data>".$datas[$i]."</data>";
  116. }
  117. $body="<TemplateSMS>
  118. <to>$to</to>
  119. <appId>$this->AppId</appId>
  120. <templateId>$tempId</templateId>
  121. <datas>".$data."</datas>
  122. </TemplateSMS>";
  123. }
  124. $this->showlog("request body = ".$body);
  125. // 大写的sig参数
  126. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  127. // 生成请求URL
  128. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
  129. $this->showlog("request url = ".$url);
  130. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  131. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  132. // 生成包头
  133. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  134. // 发送请求
  135. $result = $this->curl_post($url,$body,$header);
  136. $this->showlog("response body = ".$result);
  137. if($this->BodyType=="json"){//JSON格式
  138. $datas=json_decode($result);
  139. }else{ //xml格式
  140. $datas = simplexml_load_string(trim($result," \t\n\r"));
  141. }
  142. // if($datas == FALSE){
  143. // $datas = new stdClass();
  144. // $datas->statusCode = '172003';
  145. // $datas->statusMsg = '返回包体错误';
  146. // }
  147. //重新装填数据
  148. if($datas->statusCode==0){
  149. if($this->BodyType=="json"){
  150. $datas->TemplateSMS =$datas->templateSMS;
  151. unset($datas->templateSMS);
  152. }
  153. }
  154. return $datas;
  155. }
  156. /**
  157. * 主帐号鉴权
  158. */
  159. function accAuth()
  160. {
  161. if($this->ServerIP==""){
  162. $data = new stdClass();
  163. $data->statusCode = '172004';
  164. $data->statusMsg = 'IP为空';
  165. return $data;
  166. }
  167. if($this->ServerPort<=0){
  168. $data = new stdClass();
  169. $data->statusCode = '172005';
  170. $data->statusMsg = '端口错误(小于等于0)';
  171. return $data;
  172. }
  173. if($this->SoftVersion==""){
  174. $data = new stdClass();
  175. $data->statusCode = '172013';
  176. $data->statusMsg = '版本号为空';
  177. return $data;
  178. }
  179. if($this->AccountSid==""){
  180. $data = new stdClass();
  181. $data->statusCode = '172006';
  182. $data->statusMsg = '主帐号为空';
  183. return $data;
  184. }
  185. if($this->AccountToken==""){
  186. $data = new stdClass();
  187. $data->statusCode = '172007';
  188. $data->statusMsg = '主帐号令牌为空';
  189. return $data;
  190. }
  191. if($this->AppId==""){
  192. $data = new stdClass();
  193. $data->statusCode = '172012';
  194. $data->statusMsg = '应用ID为空';
  195. return $data;
  196. }
  197. }
  198. }
  199. ?>