Ucm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chendeben
  5. * Date: 2017/12/6
  6. * Time: 11:55
  7. */
  8. namespace PhpLife\Library;
  9. class Ucm
  10. {
  11. private $host = "http://10.0.11.3";
  12. private $token = null;
  13. public static $instance;
  14. public function __construct($is_pre = FALSE)
  15. {
  16. $this->token = self::getToken();
  17. }
  18. public static function instance()
  19. {
  20. if (!isset(self::$instance)) {
  21. $instance = new self();
  22. self::$instance = $instance;
  23. } else {
  24. $instance = self::$instance;
  25. }
  26. return $instance;
  27. }
  28. /**
  29. * 添加用户
  30. *
  31. * @param $data JSON "name":账号;<br>
  32. "password":密码,需要SHA1先hash一下;<br>
  33. "displayName":名字;<br>
  34. "email":邮箱;<br>
  35. "telephone":座机;<br>
  36. "cellphone":手机;<br>
  37. " description ":描述信息;<br>
  38. " sipUserName ":SIP号码,可通过“获取空闲SIP”获取;<br>
  39. " sipPassword ":SIP密码;<br>
  40. " company ":分组id,-1表示默认分组;<br>
  41. " deviceType ":平台类型,目前咱们UCM2.1默认是WEB;<br>
  42. " status ":用户状态,0表示激活,1表示停用,2表示锁定;<br>
  43. * @return JSON {
  44. "id":132,
  45. "name":"lisi",
  46. "password":" d033e22ae348aeb5660fc2140aec35850c4da997",
  47. "displayName":"普通用户",
  48. "email":"lisi@126.com",
  49. "telephone":"",
  50. "cellphone":"13288054696",
  51. "description":"miaoshu",
  52. "sipUserName":"1040",
  53. "sipPassword":"182116",
  54. "company":-1,
  55. "systemManager":false,
  56. "status":0
  57. }
  58. */
  59. public function addUser($data)
  60. {
  61. $url = $this->host . '/api/rest/v2.0/users?token=' . $this->token;
  62. return $result = $this->post($url, $data);
  63. }
  64. /**
  65. *
  66. * 更新用户
  67. * @param $userid
  68. * @param $data JSON 参考addUser
  69. * @return mixed
  70. */
  71. public function updateUser($userid, $data)
  72. {
  73. $url = $this->host . '/api/rest/v2.0/users/' . $userid . '?token=' . $this->token;
  74. return $result = $this->put($url, $data);
  75. }
  76. /**
  77. * 删除用户
  78. * @param $userid
  79. * @return mixed
  80. */
  81. public function deleteUser($userid){
  82. $url = $this->host . '/api/rest/v2.0/users/' . $userid . '?token=' . $this->token;
  83. return $result = $this->delete($url);
  84. }
  85. /**
  86. * 获取用户列表
  87. * @param null $userName 不传userName则获取全部的用户
  88. * @return mixed
  89. */
  90. public function getUserList($userName = null)
  91. {
  92. $url = $this->host . '/api/rest/v2.0/users?token=' . $this->token;
  93. if ($userName) {
  94. $url .= "&userName=" . $userName;
  95. }
  96. return $this->get($url);
  97. }
  98. /**
  99. * 查看用户
  100. * @param $userid
  101. * @return mixed {
  102. "id":132,
  103. "name":"lisi",
  104. "password":" 7c4a8d09ca3762af61e59520943dc26494f8941b",
  105. "displayName":"普通用户",
  106. "email":"lisi@126.com",
  107. "telephone":"",
  108. "cellphone":"13288054696",
  109. "description":"miaoshu",
  110. "sipUserName":"1040",
  111. "sipPassword":"182116",
  112. "company":-1,
  113. "systemManager":false,
  114. "status":0
  115. }
  116. */
  117. public function getUser($userid){
  118. $url = $this->host . '/api/rest/v2.0/users/' . $userid . '?token=' . $this->token;
  119. return $result = $this->get($url);
  120. }
  121. /**
  122. * 添加终端
  123. *
  124. * @param $data JSON
  125. "systemName":系统名称;<br>
  126. " endpointType ":终端型号,支持的类型:M18,M16,OTHER<br>
  127. "callType":呼叫协议,支持的协议类型:SIP_URL,SIP_IP,H323_IP,H323_E164;<br>
  128. 注意:当callType为SIP_URL时,需要输入对应的sipUrl和sipPassword的值。当callType为SIP_IP时,需要输入对应的ip的值。当callType为H323_IP时,需要输入对应的ip的值。当callType为H323_E164时,需要输入对应的e164的值。<br>
  129. "sipUrl":SIP号码,可通过“获取空闲SIP”获取;<br>
  130. " sipPassword ":SIP密码;<br>
  131. " e164":E.164号;<br>
  132. "ip":IP地址;<br>
  133. " deviceSN ":序列号;<br>
  134. "description":描述;<br>
  135. "callSpeed":呼叫速率,支持0,64,96,128,192,256,320,384,512,768,832,1024,1152,1280,1472,1536,1728,1920,2048,2560,3072,3584,4096,6144;<br><br>
  136. " netType ":网络类型,其中intranet为内网,extranet为外网;<br>
  137. " cascadeRoleType ":级联,其中None表示无级联;<br>
  138. " dialDirection ":呼叫方向,支持的类型:Dial-in,Dial-out;<br>
  139. * @return JSON {
  140. "id":184,
  141. "name":"longriver",
  142. "endpointType":"M16",
  143. " callType ":" SIP_URL ",
  144. "sipUrl":"1020",
  145. "sipPassword":"124601",
  146. "deviceSN":"4556662112",
  147. "description":"00000000",
  148. "callSpeed":128,
  149. "netType":"intranet",
  150. "cascadeRoleType":"None",
  151. "dialDirection":"Dial-in"
  152. }
  153. */
  154. public function addEndPoints($data)
  155. {
  156. $url = $this->host . '/api/rest/v2.0/endpoints?token=' . $this->token;
  157. return $result = $this->post($url, $data);
  158. }
  159. /**
  160. *
  161. * 更新用户
  162. * @param $endpointId
  163. * @param $data JSON 参考addEndPoints()
  164. * @return mixed
  165. */
  166. public function updateEndPoints($endpointId, $data)
  167. {
  168. $url = $this->host . '/api/rest/v2.0/endpoints/' . $endpointId . '?token=' . $this->token;
  169. return $result = $this->put($url, $data);
  170. }
  171. /**
  172. * 删除用户
  173. * @param $endpointId
  174. * @return mixed
  175. */
  176. public function deleteEndPoints($endpointId){
  177. $url = $this->host . '/api/rest/v2.0/endpoints/' . $endpointId . '?token=' . $this->token;
  178. return $result = $this->delete($url);
  179. }
  180. /**
  181. * 获取终端列表
  182. * @param $query array 查询条件,不指定条件则获取所有<br>
  183. * token 已登陆令牌<br>
  184. startTime 时间段起点,取值0即可。<br>
  185. endTime 时间段终点,系统当前时间即可。<br>
  186. isIncludeViseeTerminal 结果里是否包含app注册的终端,web端默认是true<br>
  187. endpointStatue 指定终端的状态(0:离线,1:在线,5:通话)<br>
  188. endpointType 指定终端类型(M18,M16,OTHER,HEXMEET)<br>
  189. endpointText 根据终端名称,序列号,IP地址,SIP号码或E.164查询匹配终端<br>
  190. * @return mixed
  191. */
  192. public function getEndPointsList($query=array())
  193. {
  194. $url = $this->host . '/api/rest/v2.0/endpoints?token=' . $this->token;
  195. if ($query) {
  196. $url .= '&'.http_build_query($query);
  197. }
  198. return $this->get($url);
  199. }
  200. /**
  201. * 查看用户
  202. * @param $endpointId
  203. * @return mixed {
  204. "id":184,
  205. "name":"longriver",
  206. "endpointType":"M16",
  207. " callType ":" SIP_URL ",
  208. "sipUrl":"1020",
  209. "sipPassword":"124601",
  210. "deviceSN":"4556662112",
  211. "description":"0000000012",
  212. "callSpeed":128,
  213. "netType":"intranet",
  214. "cascadeRoleType":"None",
  215. "dialDirection":"Dial-in",
  216. " available ":"true
  217. }
  218. */
  219. public function getEndPoints($endpointId){
  220. $url = $this->host . '/api/rest/v2.0/endpoints/' . $endpointId . '?token=' . $this->token;
  221. return $result = $this->get($url);
  222. }
  223. /**
  224. *
  225. * 预约会议
  226. * @param $data String "startTime":会议开始时间,从1970-01-01 00:00:00开始的毫秒数;<br>
  227. "duration":以毫秒计数的会议时长;<br>
  228. " name ":会议名称;<br>
  229. " confPassword ":会议密码;<br>
  230. "confDefinition":会议类型,支持FHD,HD,SD,HDRECORDING,WEBCAST,BROADCAST;<br>
  231. " remarks ":备注信息;<br>
  232. "layout":会议分屏,可以为:0X0,1x1,1x2,2x1,2x2,1and5,3x3,1x2Ver,1x2Hor,1and2Hor,1and2Ver,1and3Hor,1and3Ver,1and4Hor,1and4Ver,1and8Central,1and8Upper,1and2HorUpper,1and3HorUpper,1and4HorUpper,1and8Lower,1and7,1x1Qcif,4x4,2and8,1and12;<br><br>
  233. " userIds ":预约用户列表;<br>
  234. " confType ":入会方式,ucm2.1默认与会方式TRADITIONAL;<br>
  235. " maxBandwidth ":最大宽带;<br>
  236. " autoRedialing ":自动重拨,true和false;<br>
  237. " endpointIds ":预约终端列表信息,可由获取终端列表获取。<br>
  238. " masterEndpointId ":主会场;<br>
  239. * @return mixed
  240. */
  241. public function meetings($data){
  242. $url = $this->host . '/api/rest/v2.0/meetings?token=' . $this->token;
  243. if (is_array($data)){
  244. $data=json_encode($data);
  245. }
  246. return $this->post($url, $data);
  247. }
  248. /**
  249. * 获取空闲sip号码
  250. * @return bool|string
  251. */
  252. public function getFreeSip()
  253. {
  254. $url = $this->host . '/api/rest/v2.0/idleSipNumber?token=' . $this->token;
  255. $result = $this->get($url);
  256. return $result['sipUrl'];
  257. }
  258. /**
  259. * 获取token
  260. * @return string
  261. */
  262. private function getToken()
  263. {
  264. $url = $this->host . "/api/rest/v2.0/login";
  265. $data['account'] = "admin";
  266. $data['password'] = sha1("meitutestCOM");
  267. $data['deviceType'] = "WEB";
  268. $post = json_encode($data);
  269. $result = $this->put($url, $post);
  270. return $result['token'];
  271. }
  272. /**
  273. * put
  274. * @param $url
  275. * @param $data
  276. * @return mixed
  277. */
  278. private function put($url, $data)
  279. {
  280. if (is_array($data)){
  281. $data=json_encode($data);
  282. }
  283. $ch = curl_init(); //初始化CURL句柄
  284. curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
  285. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
  286. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
  287. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式
  288. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
  289. $output = curl_exec($ch);
  290. curl_close($ch);
  291. return json_decode($output, true);
  292. }
  293. /**
  294. *
  295. * post
  296. * @param $url
  297. * @param $data
  298. * @return mixed
  299. */
  300. private function post($url, $data)
  301. {
  302. if (is_array($data)){
  303. $data=json_encode($data);
  304. }
  305. $ch = curl_init();
  306. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
  307. curl_setopt($ch, CURLOPT_URL, $url); // 设置链接
  308. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置是否返回信息
  309. curl_setopt($ch, CURLOPT_POST, 1); // 设置为POST方式
  310. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  311. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  312. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  313. $response = curl_exec($ch); // 接收返回信息
  314. $error = curl_error($ch);
  315. if ($error) {
  316. print_r($error);
  317. }
  318. $result = json_decode($response, true);
  319. curl_close($ch);
  320. return $result;
  321. }
  322. /**
  323. * @param $url
  324. * @return mixed
  325. */
  326. private function get($url)
  327. {
  328. $headerArray = array("Content-type:application/json;", "Accept:application/json");
  329. $ch = curl_init();
  330. curl_setopt($ch, CURLOPT_URL, $url);
  331. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  332. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  333. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  334. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  335. $output = curl_exec($ch);
  336. curl_close($ch);
  337. $output = json_decode($output, true);
  338. return $output;
  339. }
  340. /**
  341. * delete
  342. * @param $url
  343. * @return mixed
  344. */
  345. private function delete($url)
  346. {
  347. $ch = curl_init();
  348. curl_setopt($ch, CURLOPT_URL, $url);
  349. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
  350. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  351. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  352. $output = curl_exec($ch);
  353. curl_close($ch);
  354. $output = json_decode($output, true);
  355. return $output;
  356. }
  357. }