Easemob.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. <?php
  2. namespace app\api\controller;
  3. /**
  4. --------------------------------------------------
  5. 环信PHP REST示例代码
  6. --------------------------------------------------
  7. Copyright(c) 2015 环信即时通信云 www.easemob.com
  8. --------------------------------------------------
  9. Author: 神之爱 <fengpei@easemob.com>
  10. --------------------------------------------------
  11. */
  12. class Easemob{
  13. private $client_id;
  14. private $client_secret;
  15. private $org_name;
  16. private $app_name;
  17. private $url;
  18. //------------------------------------------------------用户体系
  19. /**
  20. * 初始化参数
  21. *
  22. * @param array $options
  23. * @param $options['client_id']
  24. * @param $options['client_secret']
  25. * @param $options['org_name']
  26. * @param $options['app_name']
  27. */
  28. public function __construct($options = null) {
  29. $this->client_id = isset ( $options ['client_id'] ) ? $options ['client_id'] : config('client_id');
  30. $this->client_secret = isset ( $options ['client_secret'] ) ? $options ['client_secret'] : config('client_secret');
  31. $this->org_name = isset ( $options ['org_name'] ) ? $options ['org_name'] : config('org_name');
  32. $this->app_name = isset ( $options ['app_name'] ) ? $options ['app_name'] : config('app_name');
  33. if (! empty ( $this->org_name ) && ! empty ( $this->app_name )) {
  34. $this->url = 'https://a1.easemob.com/' . $this->org_name . '/' . $this->app_name . '/';
  35. }
  36. }
  37. /**
  38. *获取token
  39. */
  40. function getToken()
  41. {
  42. $options=array(
  43. "grant_type"=>"password",
  44. "client_id"=>$this->client_id,
  45. "client_secret"=>$this->client_secret
  46. );
  47. //json_encode()函数,可将PHP数组或对象转成json字符串,使用json_decode()函数,可以将json字符串转换为PHP数组或对象
  48. $body=json_encode($options);
  49. //使用 $GLOBALS 替代 global
  50. $url=$this->url.'token';
  51. //$url=$base_url.'token';
  52. $tokenResult = $this->postCurl($url,$body,$header=array());
  53. var_dump($tokenResult);
  54. //var_dump($tokenResult['expires_in']);
  55. //return $tokenResult;
  56. return "Authorization:Bearer ".$tokenResult['access_token'];
  57. }
  58. /**
  59. 授权注册
  60. */
  61. function createUser($username,$password,$name=''){
  62. $url=$this->url.'users';
  63. $options=array(
  64. "username"=>$username,
  65. "password"=>$password,
  66. 'nickname'=>$name
  67. );
  68. $body=json_encode($options);
  69. $header=array($this->getToken());
  70. $result=$this->postCurl($url,$body,$header);
  71. return $result;
  72. }
  73. /*
  74. 批量注册用户
  75. */
  76. function createUsers($options){
  77. $url=$this->url.'users';
  78. $body=json_encode($options);
  79. $header=array($this->getToken());
  80. $result=$this->postCurl($url,$body,$header);
  81. return $result;
  82. }
  83. /*
  84. 重置用户密码
  85. */
  86. function resetPassword($username,$newpassword){
  87. $url=$this->url.'users/'.$username.'/password';
  88. $options=array(
  89. "newpassword"=>$newpassword
  90. );
  91. $body=json_encode($options);
  92. $header=array($this->getToken());
  93. $result=$this->postCurl($url,$body,$header,"PUT");
  94. return $result;
  95. }
  96. /*
  97. 获取单个用户
  98. */
  99. function getUser($username){
  100. $url=$this->url.'users/'.$username;
  101. $header=array($this->getToken());
  102. $result=$this->postCurl($url,'',$header,"GET");
  103. return $result;
  104. }
  105. /*
  106. 获取批量用户----不分页
  107. */
  108. function getUsers($limit=0){
  109. if(!empty($limit)){
  110. $url=$this->url.'users?limit='.$limit;
  111. }else{
  112. $url=$this->url.'users';
  113. }
  114. $header=array($this->getToken());
  115. $result=$this->postCurl($url,'',$header,"GET");
  116. return $result;
  117. }
  118. /*
  119. 获取批量用户---分页
  120. */
  121. function getUsersForPage($limit=0,$cursor=''){
  122. $url=$this->url.'users?limit='.$limit.'&cursor='.$cursor;
  123. $header=array($this->getToken());
  124. $result=$this->postCurl($url,'',$header,"GET");
  125. if(!empty($result["cursor"])){
  126. $cursor=$result["cursor"];
  127. $this->writeCursor("userfile.txt",$cursor);
  128. }
  129. //var_dump($GLOBALS['cursor'].'00000000000000');
  130. return $result;
  131. }
  132. //创建文件夹
  133. function mkdirs($dir, $mode = 0777)
  134. {
  135. if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
  136. if (!mkdirs(dirname($dir), $mode)) return FALSE;
  137. return @mkdir($dir, $mode);
  138. }
  139. //写入cursor
  140. function writeCursor($filename,$content){
  141. //判断文件夹是否存在,不存在的话创建
  142. if(!file_exists("resource/txtfile")){
  143. mkdirs("resource/txtfile");
  144. }
  145. $myfile=@fopen("resource/txtfile/".$filename,"w+") or die("Unable to open file!");
  146. @fwrite($myfile,$content);
  147. fclose($myfile);
  148. }
  149. //读取cursor
  150. function readCursor($filename){
  151. //判断文件夹是否存在,不存在的话创建
  152. if(!file_exists("resource/txtfile")){
  153. mkdirs("resource/txtfile");
  154. }
  155. $file="resource/txtfile/".$filename;
  156. $fp=fopen($file,"a+");//这里这设置成a+
  157. if($fp){
  158. while(!feof($fp)){
  159. //第二个参数为读取的长度
  160. $data=fread($fp,1000);
  161. }
  162. fclose($fp);
  163. }
  164. return $data;
  165. }
  166. /*
  167. 删除单个用户
  168. */
  169. function deleteUser($username){
  170. $url=$this->url.'users/'.$username;
  171. $header=array($this->getToken());
  172. $result=$this->postCurl($url,'',$header,'DELETE');
  173. return $result;
  174. }
  175. /*
  176. 删除批量用户
  177. limit:建议在100-500之间,、
  178. 注:具体删除哪些并没有指定, 可以在返回值中查看。
  179. */
  180. function deleteUsers($limit){
  181. $url=$this->url.'users?limit='.$limit;
  182. $header=array($this->getToken());
  183. $result=$this->postCurl($url,'',$header,'DELETE');
  184. return $result;
  185. }
  186. /*
  187. 修改用户昵称
  188. */
  189. function editNickname($username,$nickname){
  190. $url=$this->url.'users/'.$username;
  191. $options=array(
  192. "nickname"=>$nickname
  193. );
  194. $body=json_encode($options);
  195. $header=array($this->getToken());
  196. $result=$this->postCurl($url,$body,$header,'PUT');
  197. return $result;
  198. }
  199. /*
  200. 修改用户头像
  201. */
  202. function editAvatarurl($username,$avatarurl){
  203. $url=$this->url.'users/'.$username;
  204. $options=array(
  205. "avatarurl"=>$avatarurl
  206. );
  207. $body=json_encode($options);
  208. $header=array($this->getToken());
  209. $result=$this->postCurl($url,$body,$header,'PUT');
  210. return $result;
  211. }
  212. /*
  213. 添加好友-
  214. */
  215. function addFriend($username,$friend_name){
  216. $url=$this->url.'users/'.$username.'/contacts/users/'.$friend_name;
  217. $header=array($this->getToken(),'Content-Type:application/json');
  218. $result=$this->postCurl($url,'',$header,'POST');
  219. return $result;
  220. }
  221. /*
  222. 删除好友
  223. */
  224. function deleteFriend($username,$friend_name){
  225. $url=$this->url.'users/'.$username.'/contacts/users/'.$friend_name;
  226. $header=array($this->getToken());
  227. $result=$this->postCurl($url,'',$header,'DELETE');
  228. return $result;
  229. }
  230. /*
  231. 查看好友
  232. */
  233. function showFriends($username){
  234. $url=$this->url.'users/'.$username.'/contacts/users';
  235. $header=array($this->getToken());
  236. $result=$this->postCurl($url,'',$header,'GET');
  237. return $result;
  238. }
  239. /*
  240. 查看用户黑名单
  241. */
  242. function getBlacklist($username){
  243. $url=$this->url.'users/'.$username.'/blocks/users';
  244. $header=array($this->getToken());
  245. $result=$this->postCurl($url,'',$header,'GET');
  246. return $result;
  247. }
  248. /*
  249. 往黑名单中加人
  250. */
  251. function addUserForBlacklist($username,$usernames){
  252. $url=$this->url.'users/'.$username.'/blocks/users';
  253. $body=json_encode($usernames);
  254. $header=array($this->getToken());
  255. $result=$this->postCurl($url,$body,$header,'POST');
  256. return $result;
  257. }
  258. /*
  259. 从黑名单中减人
  260. */
  261. function deleteUserFromBlacklist($username,$blocked_name){
  262. $url=$this->url.'users/'.$username.'/blocks/users/'.$blocked_name;
  263. $header=array($this->getToken());
  264. $result=$this->postCurl($url,'',$header,'DELETE');
  265. return $result;
  266. }
  267. /*
  268. 查看用户是否在线
  269. */
  270. function isOnline($username){
  271. $url=$this->url.'users/'.$username.'/status';
  272. $header=array($this->getToken());
  273. $result=$this->postCurl($url,'',$header,'GET');
  274. return $result;
  275. }
  276. /*
  277. 查看用户离线消息数
  278. */
  279. function getOfflineMessages($username){
  280. $url=$this->url.'users/'.$username.'/offline_msg_count';
  281. $header=array($this->getToken());
  282. $result=$this->postCurl($url,'',$header,'GET');
  283. return $result;
  284. }
  285. /*
  286. 查看某条消息的离线状态
  287. ----deliverd 表示此用户的该条离线消息已经收到
  288. */
  289. function getOfflineMessageStatus($username,$msg_id){
  290. $url=$this->url.'users/'.$username.'/offline_msg_status/'.$msg_id;
  291. $header=array($this->getToken());
  292. $result=$this->postCurl($url,'',$header,'GET');
  293. return $result;
  294. }
  295. /*
  296. 禁用用户账号
  297. */
  298. function deactiveUser($username){
  299. $url=$this->url.'users/'.$username.'/deactivate';
  300. $header=array($this->getToken());
  301. $result=$this->postCurl($url,'',$header);
  302. return $result;
  303. }
  304. /*
  305. 解禁用户账号
  306. */
  307. function activeUser($username){
  308. $url=$this->url.'users/'.$username.'/activate';
  309. $header=array($this->getToken());
  310. $result=$this->postCurl($url,'',$header);
  311. return $result;
  312. }
  313. /*
  314. 强制用户下线
  315. */
  316. function disconnectUser($username){
  317. $url=$this->url.'users/'.$username.'/disconnect';
  318. $header=array($this->getToken());
  319. $result=$this->postCurl($url,'',$header,'GET');
  320. return $result;
  321. }
  322. //--------------------------------------------------------上传下载
  323. /*
  324. 上传图片或文件
  325. */
  326. function uploadFile($filePath){
  327. $url=$this->url.'chatfiles';
  328. $file=file_get_contents($filePath);
  329. $body['file']=$file;
  330. $header=array('Content-type: multipart/form-data',$this->getToken(),"restrict-access:true");
  331. $result=$this->postCurl($url,$body,$header,'XXX');
  332. return $result;
  333. }
  334. /*
  335. 下载文件或图片
  336. */
  337. function downloadFile($uuid,$shareSecret,$ext)
  338. {
  339. $url = $this->url . 'chatfiles/' . $uuid;
  340. $header = array("share-secret:" . $shareSecret, "Accept:application/octet-stream", $this->getToken(),);
  341. if ($ext=="png") {
  342. $result=$this->postCurl($url,'',$header,'GET');
  343. }else {
  344. $result = $this->getFile($url);
  345. }
  346. $filename = md5(time().mt_rand(10, 99)).".".$ext; //新图片名称
  347. if(!file_exists("resource/down")){
  348. mkdir("resource/down/");
  349. }
  350. $file = @fopen("resource/down/".$filename,"w+");//打开文件准备写入
  351. @fwrite($file,$result);//写入
  352. fclose($file);//关闭
  353. return $filename;
  354. }
  355. function getFile($url){
  356. set_time_limit(0); // unlimited max execution time
  357. $ch = curl_init($url);
  358. curl_setopt($ch, CURLOPT_TIMEOUT, 600); //max 10 minutes
  359. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  360. curl_setopt($ch, CURLOPT_HEADER, false);
  361. $result = curl_exec($ch);
  362. curl_close($ch);
  363. return $result;
  364. }
  365. /*
  366. 下载图片缩略图
  367. */
  368. function downloadThumbnail($uuid,$shareSecret){
  369. $url=$this->url.'chatfiles/'.$uuid;
  370. $header = array("share-secret:".$shareSecret,"Accept:application/octet-stream",$this->getToken(),"thumbnail:true");
  371. $result=$this->postCurl($url,'',$header,'GET');
  372. $filename = md5(time().mt_rand(10, 99))."th.png"; //新图片名称
  373. if(!file_exists("resource/down")){
  374. //mkdir("../image/down");
  375. mkdirs("resource/down/");
  376. }
  377. $file = @fopen("resource/down/".$filename,"w+");//打开文件准备写入
  378. @fwrite($file,$result);//写入
  379. fclose($file);//关闭
  380. return $filename;
  381. }
  382. //--------------------------------------------------------发送消息
  383. /*
  384. 发送文本消息
  385. */
  386. function sendText($from="admin",$target_type,$target,$content,$ext){
  387. $url=$this->url.'messages';
  388. $body['target_type']=$target_type;
  389. $body['target']=$target;
  390. $options['type']="txt";
  391. $options['msg']=$content;
  392. $body['msg']=$options;
  393. $body['from']=$from;
  394. $body['ext']=$ext;
  395. $b=json_encode($body);
  396. $header=array($this->getToken());
  397. $result=$this->postCurl($url,$b,$header);
  398. return $result;
  399. }
  400. /*
  401. 发送透传消息
  402. */
  403. function sendCmd($from="admin",$target_type,$target,$action,$ext){
  404. $url=$this->url.'messages';
  405. $body['target_type']=$target_type;
  406. $body['target']=$target;
  407. $options['type']="cmd";
  408. $options['action']=$action;
  409. $body['msg']=$options;
  410. $body['from']=$from;
  411. $body['ext']=$ext;
  412. $b=json_encode($body);
  413. $header=array($this->getToken());
  414. //$b=json_encode($body,true);
  415. $result=$this->postCurl($url,$b,$header);
  416. return $result;
  417. }
  418. /*
  419. 发图片消息
  420. */
  421. function sendImage($filePath,$from="admin",$target_type,$target,$filename,$ext){
  422. $result=$this->uploadFile($filePath);
  423. $uri=$result['uri'];
  424. $uuid=$result['entities'][0]['uuid'];
  425. $shareSecret=$result['entities'][0]['share-secret'];
  426. $url=$this->url.'messages';
  427. $body['target_type']=$target_type;
  428. $body['target']=$target;
  429. $options['type']="img";
  430. $options['url']=$uri.'/'.$uuid;
  431. $options['filename']=$filename;
  432. $options['secret']=$shareSecret;
  433. $options['size']=array(
  434. "width"=>480,
  435. "height"=>720
  436. );
  437. $body['msg']=$options;
  438. $body['from']=$from;
  439. $body['ext']=$ext;
  440. $b=json_encode($body);
  441. $header=array($this->getToken());
  442. //$b=json_encode($body,true);
  443. $result=$this->postCurl($url,$b,$header);
  444. return $result;
  445. }
  446. /*
  447. 发语音消息
  448. */
  449. function sendAudio($filePath,$from="admin",$target_type,$target,$filename,$length,$ext){
  450. $result=$this->uploadFile($filePath);
  451. $uri=$result['uri'];
  452. $uuid=$result['entities'][0]['uuid'];
  453. $shareSecret=$result['entities'][0]['share-secret'];
  454. $url=$this->url.'messages';
  455. $body['target_type']=$target_type;
  456. $body['target']=$target;
  457. $options['type']="audio";
  458. $options['url']=$uri.'/'.$uuid;
  459. $options['filename']=$filename;
  460. $options['length']=$length;
  461. $options['secret']=$shareSecret;
  462. $body['msg']=$options;
  463. $body['from']=$from;
  464. $body['ext']=$ext;
  465. $b=json_encode($body);
  466. $header=array($this->getToken());
  467. //$b=json_encode($body,true);
  468. $result=$this->postCurl($url,$b,$header);
  469. return $result;}
  470. /*
  471. 发视频消息
  472. */
  473. function sendVedio($filePath,$from="admin",$target_type,$target,$filename,$length,$thumb,$thumb_secret,$ext){
  474. $result=$this->uploadFile($filePath);
  475. $uri=$result['uri'];
  476. $uuid=$result['entities'][0]['uuid'];
  477. $shareSecret=$result['entities'][0]['share-secret'];
  478. $url=$this->url.'messages';
  479. $body['target_type']=$target_type;
  480. $body['target']=$target;
  481. $options['type']="video";
  482. $options['url']=$uri.'/'.$uuid;
  483. $options['filename']=$filename;
  484. $options['thumb']=$thumb;
  485. $options['length']=$length;
  486. $options['secret']=$shareSecret;
  487. $options['thumb_secret']=$thumb_secret;
  488. $body['msg']=$options;
  489. $body['from']=$from;
  490. $body['ext']=$ext;
  491. $b=json_encode($body);
  492. $header=array($this->getToken());
  493. //$b=json_encode($body,true);
  494. $result=$this->postCurl($url,$b,$header);
  495. return $result;
  496. }
  497. /*
  498. 发文件消息
  499. */
  500. function sendFile($filePath,$from="admin",$target_type,$target,$filename,$length,$ext){
  501. $result=$this->uploadFile($filePath);
  502. $uri=$result['uri'];
  503. $uuid=$result['entities'][0]['uuid'];
  504. $shareSecret=$result['entities'][0]['share-secret'];
  505. $url=$GLOBALS['base_url'].'messages';
  506. $body['target_type']=$target_type;
  507. $body['target']=$target;
  508. $options['type']="file";
  509. $options['url']=$uri.'/'.$uuid;
  510. $options['filename']=$filename;
  511. $options['length']=$length;
  512. $options['secret']=$shareSecret;
  513. $body['msg']=$options;
  514. $body['from']=$from;
  515. $body['ext']=$ext;
  516. $b=json_encode($body);
  517. $header=array(getToken());
  518. //$b=json_encode($body,true);
  519. $result=postCurl($url,$b,$header);
  520. return $result;
  521. }
  522. //-------------------------------------------------------------群组操作
  523. /*
  524. 获取app中的所有群组----不分页
  525. */
  526. function getGroups($limit=0){
  527. if(!empty($limit)){
  528. $url=$this->url.'chatgroups?limit='.$limit;
  529. }else{
  530. $url=$this->url.'chatgroups';
  531. }
  532. $header=array($this->getToken());
  533. $result=$this->postCurl($url,'',$header,"GET");
  534. return $result;
  535. }
  536. /*
  537. 获取app中的所有群组---分页
  538. */
  539. function getGroupsForPage($limit=0,$cursor=''){
  540. $url=$this->url.'chatgroups?limit='.$limit.'&cursor='.$cursor;
  541. $header=array($this->getToken());
  542. $result=$this->postCurl($url,'',$header,"GET");
  543. if(!empty($result["cursor"])){
  544. $cursor=$result["cursor"];
  545. $this->writeCursor("groupfile.txt",$cursor);
  546. }
  547. //var_dump($GLOBALS['cursor'].'00000000000000');
  548. return $result;
  549. }
  550. /*
  551. 获取一个或多个群组的详情
  552. */
  553. function getGroupDetail($group_ids){
  554. //$g_ids=implode(',',$group_ids);
  555. $url=$this->url.'chatgroups/'.$group_ids;
  556. $header=array($this->getToken());
  557. $result=$this->postCurl($url,'',$header,'GET');
  558. return $result;
  559. }
  560. /*
  561. 创建一个群组
  562. */
  563. function createGroup($options){
  564. $url=$this->url.'chatgroups';
  565. $header=array($this->getToken());
  566. $body=json_encode($options);
  567. $result=$this->postCurl($url,$body,$header);
  568. return $result;
  569. }
  570. /*
  571. 修改群组信息
  572. */
  573. function modifyGroupInfo($group_id,$options){
  574. $url=$this->url.'chatgroups/'.$group_id;
  575. $body=json_encode($options);
  576. $header=array($this->getToken());
  577. $result=$this->postCurl($url,$body,$header,'PUT');
  578. return $result;
  579. }
  580. /*
  581. 删除群组
  582. */
  583. function deleteGroup($group_id){
  584. $url=$this->url.'chatgroups/'.$group_id;
  585. $header=array($this->getToken());
  586. $result=$this->postCurl($url,'',$header,'DELETE');
  587. return $result;
  588. }
  589. /*
  590. 获取群组中的成员
  591. */
  592. function getGroupUsers($group_id){
  593. $url=$this->url.'chatgroups/'.$group_id.'/users';
  594. $header=array($this->getToken());
  595. $result=$this->postCurl($url,'',$header,'GET');
  596. return $result;
  597. }
  598. /*
  599. 群组单个加人
  600. */
  601. function addGroupMember($group_id,$username){
  602. $url=$this->url.'chatgroups/'.$group_id.'/users/'.$username;
  603. $header=array($this->getToken(),'Content-Type:application/json');
  604. $result=$this->postCurl($url,'',$header);
  605. return $result;
  606. }
  607. /*
  608. 群组批量加人
  609. */
  610. function addGroupMembers($group_id,$usernames){
  611. $url=$this->url.'chatgroups/'.$group_id.'/users';
  612. $body=json_encode($usernames);
  613. $header=array($this->getToken(),'Content-Type:application/json');
  614. $result=$this->postCurl($url,$body,$header);
  615. return $result;
  616. }
  617. /*
  618. 群组单个减人
  619. */
  620. function deleteGroupMember($group_id,$username){
  621. $url=$this->url.'chatgroups/'.$group_id.'/users/'.$username;
  622. $header=array($this->getToken());
  623. $result=$this->postCurl($url,'',$header,'DELETE');
  624. return $result;
  625. }
  626. /*
  627. 群组批量减人
  628. */
  629. function deleteGroupMembers($group_id,$usernames){
  630. $url=$this->url.'chatgroups/'.$group_id.'/users/'.$usernames;
  631. //$body=json_encode($usernames);
  632. $header=array($this->getToken());
  633. $result=$this->postCurl($url,'',$header,'DELETE');
  634. return $result;
  635. }
  636. /*
  637. 获取一个用户参与的所有群组
  638. */
  639. function getGroupsForUser($username){
  640. $url=$this->url.'users/'.$username.'/joined_chatgroups';
  641. $header=array($this->getToken());
  642. $result=$this->postCurl($url,'',$header,'GET');
  643. return $result;
  644. }
  645. /*
  646. 群组转让
  647. */
  648. function changeGroupOwner($group_id,$options){
  649. $url=$this->url.'chatgroups/'.$group_id;
  650. $body=json_encode($options);
  651. $header=array($this->getToken());
  652. $result=$this->postCurl($url,$body,$header,'PUT');
  653. return $result;
  654. }
  655. /*
  656. 查询一个群组黑名单用户名列表
  657. */
  658. function getGroupBlackList($group_id){
  659. $url=$this->url.'chatgroups/'.$group_id.'/blocks/users';
  660. $header=array($this->getToken());
  661. $result=$this->postCurl($url,'',$header,'GET');
  662. return $result;
  663. }
  664. /*
  665. 群组黑名单单个加人
  666. */
  667. function addGroupBlackMember($group_id,$username){
  668. $url=$this->url.'chatgroups/'.$group_id.'/blocks/users/'.$username;
  669. $header=array($this->getToken());
  670. $result=$this->postCurl($url,'',$header);
  671. return $result;
  672. }
  673. /*
  674. 群组黑名单批量加人
  675. */
  676. function addGroupBlackMembers($group_id,$usernames){
  677. $url=$this->url.'chatgroups/'.$group_id.'/blocks/users';
  678. $body=json_encode($usernames);
  679. $header=array($this->getToken());
  680. $result=$this->postCurl($url,$body,$header);
  681. return $result;
  682. }
  683. /*
  684. 群组黑名单单个减人
  685. */
  686. function deleteGroupBlackMember($group_id,$username){
  687. $url=$this->url.'chatgroups/'.$group_id.'/blocks/users/'.$username;
  688. $header=array($this->getToken());
  689. $result=$this->postCurl($url,'',$header,'DELETE');
  690. return $result;
  691. }
  692. /*
  693. 群组黑名单批量减人
  694. */
  695. function deleteGroupBlackMembers($group_id,$usernames){
  696. $url=$this->url.'chatgroups/'.$group_id.'/blocks/users';
  697. $body=json_encode($usernames);
  698. $header=array($this->getToken());
  699. $result=$this->postCurl($url,$body,$header,'DELETE');
  700. return $result;
  701. }
  702. //-------------------------------------------------------------聊天室操作
  703. /*
  704. 创建聊天室
  705. */
  706. function createChatRoom($options){
  707. $url=$this->url.'chatrooms';
  708. $header=array($this->getToken());
  709. $body=json_encode($options);
  710. $result=$this->postCurl($url,$body,$header);
  711. return $result;
  712. }
  713. /*
  714. 修改聊天室信息
  715. */
  716. function modifyChatRoom($chatroom_id,$options){
  717. $url=$this->url.'chatrooms/'.$chatroom_id;
  718. $body=json_encode($options);
  719. $result=$this->postCurl($url,$body,$header,'PUT');
  720. return $result;
  721. }
  722. /*
  723. 删除聊天室
  724. */
  725. function deleteChatRoom($chatroom_id){
  726. $url=$this->url.'chatrooms/'.$chatroom_id;
  727. $header=array($this->getToken());
  728. $result=$this->postCurl($url,'',$header,'DELETE');
  729. return $result;
  730. }
  731. /*
  732. 获取app中所有的聊天室
  733. */
  734. function getChatRooms(){
  735. $url=$this->url.'chatrooms';
  736. $header=array($this->getToken());
  737. $result=$this->postCurl($url,'',$header,"GET");
  738. return $result;
  739. }
  740. /*
  741. 获取一个聊天室的详情
  742. */
  743. function getChatRoomDetail($chatroom_id){
  744. $url=$this->url.'chatrooms/'.$chatroom_id;
  745. $header=array($this->getToken());
  746. $result=$this->postCurl($url,'',$header,'GET');
  747. return $result;
  748. }
  749. /*
  750. 获取一个用户加入的所有聊天室
  751. */
  752. function getChatRoomJoined($username){
  753. $url=$this->url.'users/'.$username.'/joined_chatrooms';
  754. $header=array($this->getToken());
  755. $result=$this->postCurl($url,'',$header,'GET');
  756. return $result;
  757. }
  758. /*
  759. 聊天室单个成员添加
  760. */
  761. function addChatRoomMember($chatroom_id,$username){
  762. $url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$username;
  763. //$header=array($this->getToken());
  764. $header=array($this->getToken(),'Content-Type:application/json');
  765. $result=$this->postCurl($url,'',$header);
  766. return $result;
  767. }
  768. /*
  769. 聊天室批量成员添加
  770. */
  771. function addChatRoomMembers($chatroom_id,$usernames){
  772. $url=$this->url.'chatrooms/'.$chatroom_id.'/users';
  773. $body=json_encode($usernames);
  774. $header=array($this->getToken());
  775. $result=$this->postCurl($url,$body,$header);
  776. return $result;
  777. }
  778. /*
  779. 聊天室单个成员删除
  780. */
  781. function deleteChatRoomMember($chatroom_id,$username){
  782. $url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$username;
  783. $header=array($this->getToken());
  784. $result=$this->postCurl($url,'',$header,'DELETE');
  785. return $result;
  786. }
  787. /*
  788. 聊天室批量成员删除
  789. */
  790. function deleteChatRoomMembers($chatroom_id,$usernames){
  791. $url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$usernames;
  792. //$body=json_encode($usernames);
  793. $header=array($this->getToken());
  794. $result=$this->postCurl($url,'',$header,'DELETE');
  795. return $result;
  796. }
  797. //-------------------------------------------------------------聊天记录
  798. /*
  799. 导出聊天记录----不分页
  800. */
  801. function getChatRecord($ql){
  802. if(!empty($ql)){
  803. $url=$this->url.'chatmessages?ql='.$ql;
  804. }else{
  805. $url=$this->url.'chatmessages';
  806. }
  807. $header=array($this->getToken());
  808. $result=$this->postCurl($url,'',$header,"GET");
  809. return $result;
  810. }
  811. /*
  812. 导出聊天记录---分页
  813. */
  814. function getChatRecordForPage($ql,$limit=0,$cursor){
  815. if(!empty($ql)){
  816. $url=$this->url.'chatmessages?ql='.$ql.'&limit='.$limit.'&cursor='.$cursor;
  817. }
  818. $header=array($this->getToken());
  819. $result=$this->postCurl($url,'',$header,"GET");
  820. $cursor=isset ( $result["cursor"] ) ? $result["cursor"] : '-1';
  821. $this->writeCursor("chatfile.txt",$cursor);
  822. //var_dump($GLOBALS['cursor'].'00000000000000');
  823. return $result;
  824. }
  825. /**
  826. *$this->postCurl方法
  827. */
  828. function postCurl($url,$body,$header,$type="POST"){
  829. //1.创建一个curl资源
  830. $ch = curl_init();
  831. //2.设置URL和相应的选项
  832. curl_setopt($ch,CURLOPT_URL,$url);//设置url
  833. //1)设置请求头
  834. //array_push($header, 'Accept:application/json');
  835. //array_push($header,'Content-Type:application/json');
  836. //array_push($header, 'http:multipart/form-data');
  837. //设置为false,只会获得响应的正文(true的话会连响应头一并获取到)
  838. curl_setopt($ch,CURLOPT_HEADER,0);
  839. // curl_setopt ( $ch, CURLOPT_TIMEOUT,5); // 设置超时限制防止死循环
  840. //设置发起连接前的等待时间,如果设置为0,则无限等待。
  841. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
  842. //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  843. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  844. //2)设备请求体
  845. if (strlen($body)>0) {
  846. //$b=json_encode($body,true);
  847. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//全部数据使用HTTP协议中的"POST"操作来发送。
  848. }
  849. //设置请求头
  850. if(count($header)>0){
  851. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  852. }
  853. //上传文件相关设置
  854. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  855. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  856. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 对认证证书来源的检查
  857. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// 从证书中检查SSL加密算
  858. //3)设置提交方式
  859. switch($type){
  860. case "GET":
  861. curl_setopt($ch,CURLOPT_HTTPGET,true);
  862. break;
  863. case "POST":
  864. curl_setopt($ch,CURLOPT_POST,true);
  865. break;
  866. case "PUT"://使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。这对于执行"DELETE" 或者其他更隐蔽的HTT
  867. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT");
  868. break;
  869. case "DELETE":
  870. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"DELETE");
  871. break;
  872. }
  873. //4)在HTTP请求中包含一个"User-Agent: "头的字符串。-----必设
  874. // curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
  875. // curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  876. curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
  877. //5)
  878. //3.抓取URL并把它传递给浏览器
  879. $res=curl_exec($ch);
  880. $result=json_decode($res,true);
  881. //4.关闭curl资源,并且释放系统资源
  882. curl_close($ch);
  883. if(empty($result))
  884. return $res;
  885. else
  886. return $result;
  887. }
  888. }
  889. ?>