Easemob.php 26 KB

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