CCPRestSDK.php 39 KB

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