CCPRestSDK.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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 $SubAccountSid;
  19. private $SubAccountToken;
  20. private $VoIPAccount;
  21. private $VoIPPassword;
  22. private $ServerIP;
  23. private $ServerPort;
  24. private $SoftVersion;
  25. private $Batch; //时间sh
  26. private $BodyType = "xml";//包体格式,可填值:json 、xml
  27. private $enabeLog = false; //日志开关。可填值:true、
  28. private $Filename="../log.txt"; //日志文件
  29. private $Handle;
  30. function __construct($ServerIP,$ServerPort,$SoftVersion)
  31. {
  32. $this->Batch = date("YmdHis");
  33. $this->ServerIP = $ServerIP;
  34. $this->ServerPort = $ServerPort;
  35. $this->SoftVersion = $SoftVersion;
  36. $this->Handle = fopen($this->Filename, 'a');
  37. }
  38. /**
  39. * 设置主帐号
  40. *
  41. * @param AccountSid 主帐号
  42. * @param AccountToken 主帐号Token
  43. */
  44. function setAccount($AccountSid,$AccountToken){
  45. $this->AccountSid = $AccountSid;
  46. $this->AccountToken = $AccountToken;
  47. }
  48. /**
  49. * 设置子帐号
  50. *
  51. * @param SubAccountSid 子帐号
  52. * @param SubAccountToken 子帐号Token
  53. * @param VoIPAccount VoIP帐号
  54. * @param VoIPPassword VoIP密码
  55. */
  56. function setSubAccount($SubAccountSid,$SubAccountToken,$VoIPAccount,$VoIPPassword){
  57. $this->SubAccountSid = $SubAccountSid;
  58. $this->SubAccountToken = $SubAccountToken;
  59. $this->VoIPAccount = $VoIPAccount;
  60. $this->VoIPPassword = $VoIPPassword;
  61. }
  62. /**
  63. * 设置应用ID
  64. *
  65. * @param AppId 应用ID
  66. */
  67. function setAppId($AppId){
  68. $this->AppId = $AppId;
  69. }
  70. /**
  71. * 打印日志
  72. *
  73. * @param log 日志内容
  74. */
  75. function showlog($log){
  76. if($this->enabeLog){
  77. fwrite($this->Handle,$log."\n");
  78. }
  79. }
  80. /**
  81. * 发起HTTPS请求
  82. */
  83. function curl_post($url, $data, $header, $post = 1)
  84. {
  85. Log::record("curl_post = {$url}.",Log::DEBUG);
  86. //初始化curl
  87. $ch = curl_init();
  88. //参数设置
  89. curl_setopt($ch, CURLOPT_URL, $url);
  90. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  92. curl_setopt($ch, CURLOPT_HEADER, 0);
  93. curl_setopt($ch, CURLOPT_POST, $post);
  94. if ($post) {
  95. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  96. }
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  98. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  99. $result = curl_exec($ch);
  100. //连接失败
  101. if ($result == FALSE) {
  102. if ($this->BodyType == 'json') {
  103. $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}";
  104. } else {
  105. $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>";
  106. }
  107. }
  108. curl_close($ch);
  109. return $result;
  110. }
  111. /**
  112. * 创建子帐号
  113. * @param friendlyName 子帐号名称
  114. */
  115. function createSubAccount($friendlyName)
  116. {
  117. //主帐号鉴权信息验证,对必选参数进行判空。
  118. $auth=$this->accAuth();
  119. if($auth!=""){
  120. return $auth;
  121. }
  122. // 拼接请求包体
  123. if($this->BodyType=="json"){
  124. $body= "{'appId':'$this->AppId','friendlyName':'$friendlyName'}";
  125. }else{
  126. $body="<SubAccount>
  127. <appId>$this->AppId</appId>
  128. <friendlyName>$friendlyName</friendlyName>
  129. </SubAccount>";
  130. }
  131. $this->showlog("request body = ".$body);
  132. // 大写的sig参数
  133. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  134. // 生成请求URL
  135. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SubAccounts?sig=$sig";
  136. $this->showlog("request url = ".$url);
  137. // 生成授权:主帐号Id + 英文冒号 + 时间戳
  138. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  139. // 生成包头
  140. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  141. // 发请求
  142. $result = $this->curl_post($url,$body,$header);
  143. $this->showlog("response body = ".$result);
  144. if($this->BodyType=="json"){//JSON格式
  145. $datas=json_decode($result);
  146. }else{ //xml格式
  147. $datas = simplexml_load_string(trim($result," \t\n\r"));
  148. }
  149. // if($datas == FALSE){
  150. // $datas = new stdClass();
  151. // $datas->statusCode = '172003';
  152. // $datas->statusMsg = '返回包体错误';
  153. // }
  154. return $datas;
  155. }
  156. /**
  157. * 获取子帐号
  158. * @param startNo 开始的序号,默认从0开始
  159. * @param offset 一次查询的最大条数,最小是1条,最大是100条
  160. */
  161. function getSubAccounts($startNo,$offset)
  162. {
  163. //主帐号鉴权信息验证,对必选参数进行判空。
  164. $auth=$this->accAuth();
  165. if($auth!=""){
  166. return $auth;
  167. }
  168. // 拼接请求包体
  169. $body="
  170. <SubAccount>
  171. <appId>$this->AppId</appId>
  172. <startNo>$startNo</startNo>
  173. <offset>$offset</offset>
  174. </SubAccount>";
  175. if($this->BodyType=="json"){
  176. $body= "{'appId':'$this->AppId','startNo':'$startNo','offset':'$offset'}";
  177. }else{
  178. $body="
  179. <SubAccount>
  180. <appId>$this->AppId</appId>
  181. <startNo>$startNo</startNo>
  182. <offset>$offset</offset>
  183. </SubAccount>";
  184. }
  185. $this->showlog("request body = ".$body);
  186. // 大写的sig参数
  187. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  188. // 生成请求URL
  189. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/GetSubAccounts?sig=$sig";
  190. $this->showlog("request url = ".$url);
  191. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  192. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  193. // 生成包头
  194. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  195. // 发送请求
  196. $result = $this->curl_post($url,$body,$header);
  197. $this->showlog("response body = ".$result);
  198. if($this->BodyType=="json"){//JSON格式
  199. $datas=json_decode($result);
  200. }else{ //xml格式
  201. $datas = simplexml_load_string(trim($result," \t\n\r"));
  202. }
  203. // if($datas == FALSE){
  204. // $datas = new stdClass();
  205. // $datas->statusCode = '172003';
  206. // $datas->statusMsg = '返回包体错误';
  207. // }
  208. return $datas;
  209. }
  210. /**
  211. * 子帐号信息查询
  212. * @param friendlyName 子帐号名称
  213. */
  214. function querySubAccount($friendlyName)
  215. {
  216. //主帐号鉴权信息验证,对必选参数进行判空。
  217. $auth=$this->accAuth();
  218. if($auth!=""){
  219. return $auth;
  220. }
  221. // 拼接请求包体
  222. if($this->BodyType=="json"){
  223. $body= "{'appId':'$this->AppId','friendlyName':'$friendlyName'}";
  224. }else{
  225. $body="
  226. <SubAccount>
  227. <appId>$this->AppId</appId>
  228. <friendlyName>$friendlyName</friendlyName>
  229. </SubAccount>";
  230. }
  231. $this->showlog("request body = ".$body);
  232. // 大写的sig参数
  233. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  234. // 生成请求URL
  235. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/QuerySubAccountByName?sig=$sig";
  236. $this->showlog("request url = ".$url);
  237. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  238. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  239. // 生成包头
  240. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  241. // 发送请求
  242. $result = $this->curl_post($url,$body,$header);
  243. $this->showlog("response body = ".$result);
  244. if($this->BodyType=="json"){//JSON格式
  245. $datas=json_decode($result);
  246. }else{ //xml格式
  247. $datas = simplexml_load_string(trim($result," \t\n\r"));
  248. }
  249. // if($datas == FALSE) {
  250. // $datas = new stdClass();
  251. // $datas->statusCode = '172003';
  252. // $datas->statusMsg = '返回包体错误';
  253. // }
  254. return $datas;
  255. }
  256. /**
  257. * 发送模板短信
  258. * @param to 短信接收彿手机号码集合,用英文逗号分开
  259. * @param datas 内容数据
  260. * @param $tempId 模板Id
  261. */
  262. function sendTemplateSMS($to,$datas,$tempId)
  263. {
  264. //主帐号鉴权信息验证,对必选参数进行判空。
  265. $auth=$this->accAuth();
  266. if($auth!=""){
  267. return $auth;
  268. }
  269. // 拼接请求包体
  270. if($this->BodyType=="json"){
  271. $data="";
  272. for($i=0;$i<count($datas);$i++){
  273. $data = $data. "'".$datas[$i]."',";
  274. }
  275. $body= "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[".$data."]}";
  276. }else{
  277. $data="";
  278. for($i=0;$i<count($datas);$i++){
  279. $data = $data. "<data>".$datas[$i]."</data>";
  280. }
  281. $body="<TemplateSMS>
  282. <to>$to</to>
  283. <appId>$this->AppId</appId>
  284. <templateId>$tempId</templateId>
  285. <datas>".$data."</datas>
  286. </TemplateSMS>";
  287. }
  288. $this->showlog("request body = ".$body);
  289. // 大写的sig参数
  290. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  291. // 生成请求URL
  292. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
  293. $this->showlog("request url = ".$url);
  294. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  295. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  296. // 生成包头
  297. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  298. // 发送请求
  299. $result = $this->curl_post($url,$body,$header);
  300. $this->showlog("response body = ".$result);
  301. if($this->BodyType=="json"){//JSON格式
  302. $datas=json_decode($result);
  303. }else{ //xml格式
  304. $datas = simplexml_load_string(trim($result," \t\n\r"));
  305. }
  306. // if($datas == FALSE){
  307. // $datas = new stdClass();
  308. // $datas->statusCode = '172003';
  309. // $datas->statusMsg = '返回包体错误';
  310. // }
  311. //重新装填数据
  312. if($datas->statusCode==0){
  313. if($this->BodyType=="json"){
  314. $datas->TemplateSMS =$datas->templateSMS;
  315. unset($datas->templateSMS);
  316. }
  317. }
  318. return $datas;
  319. }
  320. /**
  321. * 双向回呼
  322. * @param from 主叫电话号码
  323. * @param to 被叫电话号码
  324. * @param customerSerNum 被叫侧显示的客服号码
  325. * @param fromSerNum 主叫侧显示的号码
  326. * @param promptTone 自定义回拨提示音
  327. * @param userData 第三方私有数据
  328. * @param maxCallTime 最大通话时长
  329. * @param hangupCdrUrl 实时话单通知地址
  330. * @param alwaysPlay 是否一直播放提示音
  331. * @param terminalDtmf 用于终止播放promptTone参数定义的提示音
  332. * @param needBothCdr 是否给主被叫发送话单
  333. * @param needRecord 是否录音
  334. * @param countDownTime 设置倒计时时间
  335. * @param countDownPrompt 倒计时时间到后播放的提示音
  336. */
  337. function callBack($from,$to,$customerSerNum,$fromSerNum,$promptTone,$alwaysPlay,$terminalDtmf,$userData,$maxCallTime,$hangupCdrUrl,$needBothCdr,$needRecord,$countDownTime,$countDownPrompt)
  338. {
  339. //子帐号鉴权信息验证,对必选参数进行判空。
  340. $auth=$this->subAuth();
  341. if($auth!=""){
  342. return $auth;
  343. }
  344. // 拼接请求包体
  345. if($this->BodyType=="json"){
  346. $body= "{'from':'$from','to':'$to','customerSerNum':'$customerSerNum','fromSerNum':'$fromSerNum','promptTone':'$promptTone','userData':'$userData','maxCallTime':'$maxCallTime','hangupCdrUrl':'$hangupCdrUrl',
  347. 'alwaysPlay':'$alwaysPlay','terminalDtmf':'$terminalDtmf','needBothCdr':'$needBothCdr',
  348. 'needRecord':'$needRecord','countDownTime':'$$countDownTime','countDownPrompt':'$countDownPrompt'}";
  349. }else{
  350. $body= "<CallBack>
  351. <from>$from</from>
  352. <to>$to</to>
  353. <customerSerNum>$customerSerNum</customerSerNum>
  354. <fromSerNum>$fromSerNum</fromSerNum>
  355. <promptTone>$promptTone</promptTone>
  356. <userData>$userData</userData>
  357. <maxCallTime>$maxCallTime</maxCallTime>
  358. <hangupCdrUrl>$hangupCdrUrl</hangupCdrUrl>
  359. <alwaysPlay>$alwaysPlay</alwaysPlay>
  360. <terminalDtmf>$terminalDtmf</terminalDtmf>
  361. <needBothCdr>$needBothCdr</needBothCdr>
  362. <needRecord>$needRecord</needRecord>
  363. <countDownTime>$countDownTime</countDownTime>
  364. <countDownPrompt>$countDownPrompt</countDownPrompt>
  365. </CallBack>";
  366. }
  367. $this->showlog("request body = ".$body);
  368. // 大写的sig参数
  369. $sig = strtoupper(md5($this->SubAccountSid . $this->SubAccountToken . $this->Batch));
  370. // 生成请求URL
  371. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/SubAccounts/$this->SubAccountSid/Calls/Callback?sig=$sig";
  372. $this->showlog("request url = ".$url);
  373. // 生成授权:子帐号Id + 英文冒号 + 时间戳
  374. $authen=base64_encode($this->SubAccountSid . ":" . $this->Batch);
  375. // 生成包头
  376. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  377. // 发请求
  378. $result = $this->curl_post($url,$body,$header);
  379. $this->showlog("response body = ".$result);
  380. if($this->BodyType=="json"){//JSON格式
  381. $datas=json_decode($result);
  382. }else{ //xml格式
  383. $datas = simplexml_load_string(trim($result," \t\n\r"));
  384. }
  385. // if($datas == FALSE){
  386. // $datas = new stdClass();
  387. // $datas->statusCode = '172003';
  388. // $datas->statusMsg = '返回包体错误';
  389. // }
  390. return $datas;
  391. }
  392. /**
  393. * 外呼通知
  394. * @param to 被叫号码
  395. * @param mediaName 语音文件名称,格式 wav。与mediaTxt不能同时为空。当不为空时mediaTxt属性失效。
  396. * @param mediaTxt 文本内容
  397. * @param displayNum 显示的主叫号码
  398. * @param playTimes 循环播放次数,1-3次,默认播放1次。
  399. * @param respUrl 外呼通知状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知。
  400. * @param userData 用户私有数据
  401. * @param maxCallTime 最大通话时长
  402. * @param speed 发音速度
  403. * @param volume 音量
  404. * @param pitch 音调
  405. * @param bgsound 背景音编号
  406. */
  407. function landingCall($to,$mediaName,$mediaTxt,$displayNum,$playTimes,$respUrl,$userData,$maxCallTime,$speed,$volume,$pitch,$bgsound)
  408. {
  409. //主帐号鉴权信息验证,对必选参数进行判空。
  410. $auth=$this->accAuth();
  411. if($auth!=""){
  412. return $auth;
  413. }
  414. // 拼接请求包体
  415. if($this->BodyType=="json"){
  416. $body= "{'playTimes':'$playTimes','mediaTxt':'$mediaTxt','mediaName':'$mediaName','to':'$to','appId':'$this->AppId','displayNum':'$displayNum','respUrl':'$respUrl',
  417. 'userData':'$userData','maxCallTime':'$maxCallTime','speed':'$speed','volume':'$volume','pitch':'$pitch','bgsound':'$bgsound'}";
  418. }else{
  419. $body="<LandingCall>
  420. <to>$to</to>
  421. <mediaName>$mediaName</mediaName>
  422. <mediaTxt>$mediaTxt</mediaTxt>
  423. <appId>$this->AppId</appId>
  424. <displayNum>$displayNum</displayNum>
  425. <playTimes>$playTimes</playTimes>
  426. <respUrl>$respUrl</respUrl>
  427. <userData>$userData</userData>
  428. <maxCallTime>$maxCallTime</maxCallTime>
  429. <speed>$speed</speed>
  430. <volume>$volume</volume>
  431. <pitch>$pitch</pitch>
  432. <bgsound>$bgsound</bgsound>
  433. </LandingCall>";
  434. }
  435. $this->showlog("request body = ".$body);
  436. // 大写的sig参数
  437. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  438. // 生成请求URL
  439. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/Calls/LandingCalls?sig=$sig";
  440. $this->showlog("request url = ".$url);
  441. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  442. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  443. // 生成包头
  444. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  445. // 发送请求
  446. $result = $this->curl_post($url,$body,$header);
  447. $this->showlog("response body = ".$result);
  448. if($this->BodyType=="json"){//JSON格式
  449. $datas=json_decode($result);
  450. }else{ //xml格式
  451. $datas = simplexml_load_string(trim($result," \t\n\r"));
  452. }
  453. // if($datas == FALSE){
  454. // $datas = new stdClass();
  455. // $datas->statusCode = '172003';
  456. // $datas->statusMsg = '返回包体错误';
  457. // }
  458. return $datas;
  459. }
  460. /**
  461. * 语音验证码
  462. * @param verifyCode 验证码内容,为数字和英文字母,不区分大小写,长度4-8位
  463. * @param playTimes 播放次数,1-3次
  464. * @param to 接收号码
  465. * @param displayNum 显示的主叫号码
  466. * @param respUrl 语音验证码状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知
  467. * @param lang 语言类型
  468. * @param userData 第三方私有数据
  469. */
  470. function voiceVerify($verifyCode,$playTimes,$to,$displayNum,$respUrl,$lang,$userData)
  471. {
  472. //主帐号鉴权信息验证,对必选参数进行判空。
  473. $auth=$this->accAuth();
  474. if($auth!=""){
  475. return $auth;
  476. }
  477. // 拼接请求包体
  478. if($this->BodyType=="json"){
  479. $body= "{'appId':'$this->AppId','verifyCode':'$verifyCode','playTimes':'$playTimes','to':'$to','respUrl':'$respUrl','displayNum':'$displayNum',
  480. 'lang':'$lang','userData':'$userData'}";
  481. }else{
  482. $body="<VoiceVerify>
  483. <appId>$this->AppId</appId>
  484. <verifyCode>$verifyCode</verifyCode>
  485. <playTimes>$playTimes</playTimes>
  486. <to>$to</to>
  487. <respUrl>$respUrl</respUrl>
  488. <displayNum>$displayNum</displayNum>
  489. <lang>$lang</lang>
  490. <userData>$userData</userData>
  491. </VoiceVerify>";
  492. }
  493. $this->showlog("request body = ".$body);
  494. // 大写的sig参数
  495. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  496. // 生成请求URL
  497. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/Calls/VoiceVerify?sig=$sig";
  498. $this->showlog("request url = ".$url);
  499. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  500. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  501. // 生成包头
  502. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  503. // 发送请求
  504. $result = $this->curl_post($url,$body,$header);
  505. $this->showlog("response body = ".$result);
  506. if($this->BodyType=="json"){//JSON格式
  507. $datas=json_decode($result);
  508. }else{ //xml格式
  509. $datas = simplexml_load_string(trim($result," \t\n\r"));
  510. }
  511. // if($datas == FALSE){
  512. // $datas = new stdClass();
  513. // $datas->statusCode = '172003';
  514. // $datas->statusMsg = '返回包体错误';
  515. // }
  516. return $datas;
  517. }
  518. /**
  519. * IVR外呼
  520. * @param number 待呼叫号码,为Dial节点的属性
  521. * @param userdata 用户数据,在<startservice>通知中返回,只允许填写数字字符,为Dial节点的属性
  522. * @param record 是否录音,可填项为true和false,默认值为false不录音,为Dial节点的属性
  523. */
  524. function ivrDial($number,$userdata,$record)
  525. {
  526. //主帐号鉴权信息验证,对必选参数进行判空。
  527. $auth=$this->accAuth();
  528. if($auth!=""){
  529. return $auth;
  530. }
  531. // 拼接请求包体
  532. $body=" <Request>
  533. <Appid>$this->AppId</Appid>
  534. <Dial number='$number' userdata='$userdata' record='$record'></Dial>
  535. </Request>";
  536. $this->showlog("request body = ".$body);
  537. // 大写的sig参数
  538. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  539. // 生成请求URL
  540. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/ivr/dial?sig=$sig";
  541. $this->showlog("request url = ".$url);
  542. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  543. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  544. // 生成包头
  545. $header = array("Accept:application/xml","Content-Type:application/xml;charset=utf-8","Authorization:$authen");
  546. // 发送请求
  547. $result = $this->curl_post($url,$body,$header);
  548. $this->showlog("response body = ".$result);
  549. $datas = simplexml_load_string(trim($result," \t\n\r"));
  550. // if($datas == FALSE){
  551. // $datas = new stdClass();
  552. // $datas->statusCode = '172003';
  553. // $datas->statusMsg = '返回包体错误';
  554. // }
  555. return $datas;
  556. }
  557. /**
  558. * 话单下载
  559. * @param date day 代表前一天的数据(从00:00 – 23:59)
  560. * @param keywords 客户的查询条件,由客户自行定义并提供给云通讯平台。默认不填忽略此参数
  561. */
  562. function billRecords($date,$keywords)
  563. {
  564. //主帐号鉴权信息验证,对必选参数进行判空。
  565. $auth=$this->accAuth();
  566. if($auth!=""){
  567. return $auth;
  568. }
  569. // 拼接请求包体
  570. if($this->BodyType=="json"){
  571. $body= "{'appId':'$this->AppId','date':'$date','keywords':'$keywords'}";
  572. }else{
  573. $body="<BillRecords>
  574. <appId>$this->AppId</appId>
  575. <date>$date</date>
  576. <keywords>$keywords</keywords>
  577. </BillRecords>";
  578. }
  579. $this->showlog("request body = ".$body);
  580. // 大写的sig参数
  581. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  582. // 生成请求URL
  583. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/BillRecords?sig=$sig";
  584. $this->showlog("request url = ".$url);
  585. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  586. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  587. // 生成包头
  588. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  589. // 发送请求
  590. $result = $this->curl_post($url,$body,$header);
  591. $this->showlog("response body = ".$result);
  592. if($this->BodyType=="json"){//JSON格式
  593. $datas=json_decode($result);
  594. }else{ //xml格式
  595. $datas = simplexml_load_string(trim($result," \t\n\r"));
  596. }
  597. // if($datas == FALSE){
  598. // $datas = new stdClass();
  599. // $datas->statusCode = '172003';
  600. // $datas->statusMsg = '返回包体错误';
  601. // }
  602. return $datas;
  603. }
  604. /**
  605. * 主帐号信息查询
  606. */
  607. function queryAccountInfo()
  608. {
  609. //主帐号鉴权信息验证,对必选参数进行判空。
  610. $auth=$this->accAuth();
  611. if($auth!=""){
  612. return $auth;
  613. }
  614. // 大写的sig参数
  615. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  616. // 生成请求URL
  617. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/AccountInfo?sig=$sig";
  618. $this->showlog("request url = ".$url);
  619. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  620. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  621. // 生成包头
  622. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  623. // 发送请求
  624. $result = $this->curl_post($url,"",$header,0);
  625. $this->showlog("response body = ".$result);
  626. if($this->BodyType=="json"){//JSON格式
  627. $datas=json_decode($result);
  628. }else{ //xml格式
  629. $datas = simplexml_load_string(trim($result," \t\n\r"));
  630. }
  631. // if($datas == FALSE){
  632. // $datas = new stdClass();
  633. // $datas->statusCode = '172003';
  634. // $datas->statusMsg = '返回包体错误';
  635. // }
  636. return $datas;
  637. }
  638. /**
  639. * 短信模板查询
  640. * @param date templateId 模板ID
  641. */
  642. function QuerySMSTemplate($templateId)
  643. {
  644. //主帐号鉴权信息验证,对必选参数进行判空。
  645. $auth=$this->accAuth();
  646. if($auth!=""){
  647. return $auth;
  648. }
  649. // 拼接请求包体
  650. if($this->BodyType=="json"){
  651. $body= "{'appId':'$this->AppId','templateId':'$templateId'}";
  652. }else{
  653. $body="<Request>
  654. <appId>$this->AppId</appId>
  655. <templateId>$templateId</templateId>
  656. </Request>";
  657. }
  658. $this->showlog("request body = ".$body);
  659. // 大写的sig参数
  660. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  661. // 生成请求URL
  662. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/QuerySMSTemplate?sig=$sig";
  663. $this->showlog("request url = ".$url);
  664. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  665. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  666. // 生成包头
  667. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  668. // 发送请求
  669. $result = $this->curl_post($url,$body,$header);
  670. $this->showlog("response body = ".$result);
  671. if($this->BodyType=="json"){//JSON格式
  672. $datas=json_decode($result);
  673. }else{ //xml格式
  674. $datas = simplexml_load_string(trim($result," \t\n\r"));
  675. }
  676. // if($datas == FALSE){
  677. // $datas = new stdClass();
  678. // $datas->statusCode = '172003';
  679. // $datas->statusMsg = '返回包体错误';
  680. // }
  681. return $datas;
  682. }
  683. /**
  684. * 取消回拨
  685. * @param callSid 一个由32个字符组成的电话唯一标识符
  686. * @param type 0: 任意时间都可以挂断电话;1 :被叫应答前可以挂断电话,其他时段返回错误代码;2: 主叫应答前可以挂断电话,其他时段返回错误代码;默认值为0。
  687. */
  688. function CallCancel($callSid,$type)
  689. {
  690. //主帐号鉴权信息验证,对必选参数进行判空。
  691. $auth=$this->subAuth();
  692. if($auth!=""){
  693. return $auth;
  694. }
  695. // 拼接请求包体
  696. if($this->BodyType=="json"){
  697. $body= "{'appId':'$this->AppId','callSid':'$callSid','type':'$type'}";
  698. }else{
  699. $body="<CallCancel>
  700. <appId>$this->AppId</appId>
  701. <callSid>$callSid</callSid>
  702. <type>$type</type>
  703. </CallCancel>";
  704. }
  705. $this->showlog("request body = ".$body);
  706. // 大写的sig参数
  707. $sig = strtoupper(md5($this->SubAccountSid . $this->SubAccountToken . $this->Batch));
  708. // 生成请求URL
  709. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/SubAccounts/$this->SubAccountSid/Calls/CallCancel?sig=$sig";
  710. $this->showlog("request url = ".$url);
  711. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  712. $authen = base64_encode($this->SubAccountSid . ":" . $this->Batch);
  713. // 生成包头
  714. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  715. // 发送请求
  716. $result = $this->curl_post($url,$body,$header);
  717. $this->showlog("response body = ".$result);
  718. if($this->BodyType=="json"){//JSON格式
  719. $datas=json_decode($result);
  720. }else{ //xml格式
  721. $datas = simplexml_load_string(trim($result," \t\n\r"));
  722. }
  723. // if($datas == FALSE){
  724. // $datas = new stdClass();
  725. // $datas->statusCode = '172003';
  726. // $datas->statusMsg = '返回包体错误';
  727. // }
  728. return $datas;
  729. }
  730. /**
  731. * 呼叫状态查询
  732. * @param callid 呼叫Id
  733. * @param action 查询结果通知的回调url地址
  734. */
  735. function QueryCallState($callid,$action)
  736. {
  737. //主帐号鉴权信息验证,对必选参数进行判空。
  738. $auth=$this->accAuth();
  739. if($auth!=""){
  740. return $auth;
  741. }
  742. // 拼接请求包体
  743. if($this->BodyType=="json"){
  744. $body= "{'Appid':'$this->AppId','QueryCallState':{'callid':'$callid','action':'$action'}}";
  745. }else{
  746. $body="<Request>
  747. <Appid>$this->AppId</Appid>
  748. <QueryCallState callid ='$callid' action='$action'/>
  749. </Request>";
  750. }
  751. $this->showlog("request body = ".$body);
  752. // 大写的sig参数
  753. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  754. // 生成请求URL
  755. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/ivr/call?sig=$sig&callid=$callid";
  756. $this->showlog("request url = ".$url);
  757. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  758. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  759. // 生成包头
  760. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  761. // 发送请求
  762. $result = $this->curl_post($url,$body,$header);
  763. $this->showlog("response body = ".$result);
  764. if($this->BodyType=="json"){//JSON格式
  765. $datas=json_decode($result);
  766. }else{ //xml格式
  767. $datas = simplexml_load_string(trim($result," \t\n\r"));
  768. }
  769. // if($datas == FALSE){
  770. // $datas = new stdClass();
  771. // $datas->statusCode = '172003';
  772. // $datas->statusMsg = '返回包体错误';
  773. // }
  774. return $datas;
  775. }
  776. /**
  777. * 呼叫结果查询
  778. * @param callSid 呼叫Id
  779. */
  780. function CallResult($callSid)
  781. {
  782. //主帐号鉴权信息验证,对必选参数进行判空。
  783. $auth=$this->accAuth();
  784. if($auth!=""){
  785. return $auth;
  786. }
  787. // 大写的sig参数
  788. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  789. // 生成请求URL
  790. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/CallResult?sig=$sig&callsid=$callSid";
  791. $this->showlog("request url = ".$url);
  792. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  793. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  794. // 生成包头
  795. $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
  796. // 发送请求
  797. $result = $this->curl_post($url,"",$header,0);
  798. $this->showlog("response body = ".$result);
  799. if($this->BodyType=="json"){//JSON格式
  800. $datas=json_decode($result);
  801. }else{ //xml格式
  802. $datas = simplexml_load_string(trim($result," \t\n\r"));
  803. }
  804. // if($datas == FALSE){
  805. // $datas = new stdClass();
  806. // $datas->statusCode = '172003';
  807. // $datas->statusMsg = '返回包体错误';
  808. // }
  809. return $datas;
  810. }
  811. /**
  812. * 语音文件上传
  813. * @param filename 文件名
  814. * @param body 二进制串
  815. */
  816. function MediaFileUpload($filename,$body)
  817. {
  818. //主帐号鉴权信息验证,对必选参数进行判空。
  819. $auth=$this->accAuth();
  820. if($auth!=""){
  821. return $auth;
  822. }
  823. // 拼接请求包体
  824. $this->showlog("request body = ".$body);
  825. // 大写的sig参数
  826. $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
  827. // 生成请求URL
  828. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/Calls/MediaFileUpload?sig=$sig&appid=$this->AppId&filename=$filename";
  829. $this->showlog("request url = ".$url);
  830. // 生成授权:主帐户Id + 英文冒号 + 时间戳。
  831. $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
  832. // 生成包头
  833. $header = array("Accept:application/$this->BodyType","Content-Type:application/octet-stream","Authorization:$authen");
  834. // 发送请求
  835. $result = $this->curl_post($url,$body,$header);
  836. $this->showlog("response body = ".$result);
  837. if($this->BodyType=="json"){//JSON格式
  838. $datas=json_decode($result);
  839. }else{ //xml格式
  840. $datas = simplexml_load_string(trim($result," \t\n\r"));
  841. }
  842. // if($datas == FALSE){
  843. // $datas = new stdClass();
  844. // $datas->statusCode = '172003';
  845. // $datas->statusMsg = '返回包体错误';
  846. // }
  847. return $datas;
  848. }
  849. /**
  850. * 子帐号鉴权
  851. */
  852. function subAuth()
  853. {
  854. if($this->ServerIP==""){
  855. $data = new stdClass();
  856. $data->statusCode = '172004';
  857. $data->statusMsg = 'IP为空';
  858. return $data;
  859. }
  860. if($this->ServerPort<=0){
  861. $data = new stdClass();
  862. $data->statusCode = '172005';
  863. $data->statusMsg = '端口错误(小于等于0)';
  864. return $data;
  865. }
  866. if($this->SoftVersion==""){
  867. $data = new stdClass();
  868. $data->statusCode = '172013';
  869. $data->statusMsg = '版本号为空';
  870. return $data;
  871. }
  872. if($this->SubAccountSid==""){
  873. $data = new stdClass();
  874. $data->statusCode = '172008';
  875. $data->statusMsg = '子帐号为空';
  876. return $data;
  877. }
  878. if($this->SubAccountToken==""){
  879. $data = new stdClass();
  880. $data->statusCode = '172009';
  881. $data->statusMsg = '子帐号令牌为空';
  882. return $data;
  883. }
  884. if($this->AppId==""){
  885. $data = new stdClass();
  886. $data->statusCode = '172012';
  887. $data->statusMsg = '应用ID为空';
  888. return $data;
  889. }
  890. }
  891. /**
  892. * 主帐号鉴权
  893. */
  894. function accAuth()
  895. {
  896. if($this->ServerIP==""){
  897. $data = new stdClass();
  898. $data->statusCode = '172004';
  899. $data->statusMsg = 'IP为空';
  900. return $data;
  901. }
  902. if($this->ServerPort<=0){
  903. $data = new stdClass();
  904. $data->statusCode = '172005';
  905. $data->statusMsg = '端口错误(小于等于0)';
  906. return $data;
  907. }
  908. if($this->SoftVersion==""){
  909. $data = new stdClass();
  910. $data->statusCode = '172013';
  911. $data->statusMsg = '版本号为空';
  912. return $data;
  913. }
  914. if($this->AccountSid==""){
  915. $data = new stdClass();
  916. $data->statusCode = '172006';
  917. $data->statusMsg = '主帐号为空';
  918. return $data;
  919. }
  920. if($this->AccountToken==""){
  921. $data = new stdClass();
  922. $data->statusCode = '172007';
  923. $data->statusMsg = '主帐号令牌为空';
  924. return $data;
  925. }
  926. if($this->AppId==""){
  927. $data = new stdClass();
  928. $data->statusCode = '172012';
  929. $data->statusMsg = '应用ID为空';
  930. return $data;
  931. }
  932. }
  933. }
  934. ?>