CCPRestSDK.php 33 KB

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