Easemob.php 26 KB

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