当前位置:   article > 正文

环信phpDEMO_php 获取环信用户在线状态

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

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号