環信即時通信——集成客戶端

最近公司在開發一款 APP,須要使用環信即時通信來作及時聊天和直播,找了好多官方的 REST API 發現並無把直播集成服務寫完,因而本身完善了一下,與你們分享 O (∩_∩) O
話很少說上代碼php

namespace yournamespace class Easemob{ private $client_id; private $client_secret; private $org_name; private $app_name; private $url; //------------------------------------------------------用戶體系 /** * 初始化參數 * * @param array $options * @param $options['client_id'] * @param $options['client_secret'] * @param $options['org_name'] * @param $options['app_name'] */ public function __construct($options) { $this->client_id = 'XXXXXXXXXXXXXX'; $this->client_secret = 'XXXXXXXXXXXXXX'; $this->org_name = 'XXXXXXXXXXXXXX'; $this->app_name = 'XXXXXXXXXXXXXX'; if (! empty ( $this->org_name ) && ! empty ( $this->app_name )) { $this->url = 'https://a1.easemob.com/' . $this->org_name . '/' . $this->app_name . '/'; } } /** *獲取token */ function getToken() { $options=array( "grant_type"=>"client_credentials", "client_id"=>$this->client_id, "client_secret"=>$this->client_secret ); //json_encode()函數,可將PHP數組或對象轉成json字符串,使用json_decode()函數,能夠將json字符串轉換爲PHP數組或對象 $body=json_encode($options); //使用 $GLOBALS 替代 global $url=$this->url.'token'; //$url=$base_url.'token'; $tokenResult = $this->postCurl($url,$body,$header=array()); //var_dump($tokenResult['expires_in']); //return $tokenResult; return "Authorization:Bearer ".$tokenResult['access_token']; } /** 受權註冊 */ function createUser($username,$password){ $url=$this->url.'users'; $options=array( "username"=>$username, "password"=>$password ); $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header); return $result; } /* 批量註冊用戶 */ function createUsers($options){ $url=$this->url.'users'; $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header); return $result; } /* 重置用戶密碼 */ function resetPassword($username,$newpassword){ $url=$this->url.'users/'.$username.'/password'; $options=array( "newpassword"=>$newpassword ); $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header,"PUT"); return $result; } /* 獲取單個用戶 */ function getUser($username){ $url=$this->url.'users/'.$username; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); return $result; } /* 獲取批量用戶----不分頁 */ function getUsers($limit=0){ if(!empty($limit)){ $url=$this->url.'users?limit='.$limit; }else{ $url=$this->url.'users'; } $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); return $result; } /* 獲取批量用戶---分頁 */ function getUsersForPage($limit=0,$cursor=''){ $url=$this->url.'users?limit='.$limit.'&cursor='.$cursor; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); if(!empty($result["cursor"])){ $cursor=$result["cursor"]; $this->writeCursor("userfile.txt",$cursor); } //var_dump($GLOBALS['cursor'].'00000000000000'); return $result; } //建立文件夾 function mkdirs($dir, $mode = 0777) { if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE; if (!mkdirs(dirname($dir), $mode)) return FALSE; return @mkdir($dir, $mode); } //寫入cursor function writeCursor($filename,$content){ //判斷文件夾是否存在,不存在的話建立 if(!file_exists("resource/txtfile")){ mkdirs("resource/txtfile"); } $myfile=@fopen("resource/txtfile/".$filename,"w+") or die("Unable to open file!"); @fwrite($myfile,$content); fclose($myfile); } //讀取cursor function readCursor($filename){ //判斷文件夾是否存在,不存在的話建立 if(!file_exists("resource/txtfile")){ mkdirs("resource/txtfile"); } $file="resource/txtfile/".$filename; $fp=fopen($file,"a+");//這裏這設置成a+ if($fp){ while(!feof($fp)){ //第二個參數爲讀取的長度 $data=fread($fp,1000); } fclose($fp); } return $data; } /* 刪除單個用戶 */ function deleteUser($username){ $url=$this->url.'users/'.$username; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 刪除批量用戶 limit:建議在100-500之間,、 注:具體刪除哪些並無指定, 能夠在返回值中查看。 */ function deleteUsers($limit){ $url=$this->url.'users?limit='.$limit; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 修改用戶暱稱 */ function editNickname($username,$nickname){ $url=$this->url.'users/'.$username; $options=array( "nickname"=>$nickname ); $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header,'PUT'); return $result; } /* 添加好友- */ function addFriend($username,$friend_name){ $url=$this->url.'users/'.$username.'/contacts/users/'.$friend_name; $header=array($this->getToken(),'Content-Type:application/json'); $result=$this->postCurl($url,'',$header,'POST'); return $result; } /* 刪除好友 */ function deleteFriend($username,$friend_name){ $url=$this->url.'users/'.$username.'/contacts/users/'.$friend_name; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 查看好友 */ function showFriends($username){ $url=$this->url.'users/'.$username.'/contacts/users'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 查看用戶黑名單 */ function getBlacklist($username){ $url=$this->url.'users/'.$username.'/blocks/users'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 往黑名單中加人 */ function addUserForBlacklist($username,$usernames){ $url=$this->url.'users/'.$username.'/blocks/users'; $body=json_encode($usernames); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header,'POST'); return $result; } /* 從黑名單中減人 */ function deleteUserFromBlacklist($username,$blocked_name){ $url=$this->url.'users/'.$username.'/blocks/users/'.$blocked_name; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 查看用戶是否在線 */ function isOnline($username){ $url=$this->url.'users/'.$username.'/status'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 查看用戶離線消息數 */ function getOfflineMessages($username){ $url=$this->url.'users/'.$username.'/offline_msg_count'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 查看某條消息的離線狀態 ----deliverd 表示此用戶的該條離線消息已經收到 */ function getOfflineMessageStatus($username,$msg_id){ $url=$this->url.'users/'.$username.'/offline_msg_status/'.$msg_id; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 禁用用戶帳號 */ function deactiveUser($username){ $url=$this->url.'users/'.$username.'/deactivate'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header); return $result; } /* 解禁用戶帳號 */ function activeUser($username){ $url=$this->url.'users/'.$username.'/activate'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header); return $result; } /* 強制用戶下線 */ function disconnectUser($username){ $url=$this->url.'users/'.$username.'/disconnect'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } //--------------------------------------------------------上傳下載 /* 上傳圖片或文件 */ function uploadFile($filePath){ $url=$this->url.'chatfiles'; $file=file_get_contents($filePath); $body['file']=$file; $header=array('enctype:multipart/form-data',$this->getToken(),"restrict-access:true"); $result=$this->postCurl($url,$body,$header,'XXX'); return $result; } /* 下載文件或圖片 */ function downloadFile($uuid,$shareSecret){ $url=$this->url.'chatfiles/'.$uuid; $header = array("share-secret:".$shareSecret,"Accept:application/octet-stream",$this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); $filename = md5(time().mt_rand(10, 99)).".png"; //新圖片名稱 if(!file_exists("resource/down")){ //mkdir("../image/down"); mkdirs("resource/down/"); } $file = @fopen("resource/down/".$filename,"w+");//打開文件準備寫入 @fwrite($file,$result);//寫入 fclose($file);//關閉 return $filename; } /* 下載圖片縮略圖 */ function downloadThumbnail($uuid,$shareSecret){ $url=$this->url.'chatfiles/'.$uuid; $header = array("share-secret:".$shareSecret,"Accept:application/octet-stream",$this->getToken(),"thumbnail:true"); $result=$this->postCurl($url,'',$header,'GET'); $filename = md5(time().mt_rand(10, 99))."th.png"; //新圖片名稱 if(!file_exists("resource/down")){ //mkdir("../image/down"); mkdirs("resource/down/"); } $file = @fopen("resource/down/".$filename,"w+");//打開文件準備寫入 @fwrite($file,$result);//寫入 fclose($file);//關閉 return $filename; } //-------------------------------------------------------------直播間操做 /* 設置直播流地址 */ function set_tream_url($pc_pull,$mobile_push,$mobile_pull,$pc_push){ $url=$this->url.'liverooms/stream_url'; $options=array( "pc_pull"=>$pc_pull, "pc_push"=>$pc_push, "mobile_push"=>$mobile_push, "mobile_pull"=>$mobile_pull, ); $b=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$b,$header); return $result; } /* 獲取某端直播流地址 */ function getpull($types){ $url=$this->url.'liverooms/stream_url?type='.$types; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); return $result; } /* 給用戶賦予主播身份 */ function addsuperadmin($admin){ $url=$this->url.'chatrooms/super_admin'; $options=array( "superadmin"=>$admin ); $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header); return $result; } /* 建立直播聊天室 */ function addliveroom($liveroom_name,$desc,$superadmin){ $url=$this->url.'chatdemoui/liverooms'; $options=array( "title"=>$liveroom_name, "desc"=>$desc, "anchor"=>$superadmin, ); $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header); return $result; } //--------------------------------------------------------發送消息 /* 發送文本消息 */ function sendText($from="admin",$target_type,$target,$content,$ext){ $url=$this->url.'messages'; $body['target_type']=$target_type; $body['target']=$target; $options['type']="txt"; $options['msg']=$content; $body['msg']=$options; $body['from']=$from; $body['ext']=$ext; $b=json_encode($body); $header=array($this->getToken()); $result=$this->postCurl($url,$b,$header); return $result; } /* 發送透傳消息 */ function sendCmd($from="admin",$target_type,$target,$action,$ext){ $url=$this->url.'messages'; $body['target_type']=$target_type; $body['target']=$target; $options['type']="cmd"; $options['action']=$action; $body['msg']=$options; $body['from']=$from; $body['ext']=$ext; $b=json_encode($body); $header=array($this->getToken()); //$b=json_encode($body,true); $result=$this->postCurl($url,$b,$header); return $result; } /* 發圖片消息 */ function sendImage($filePath,$from="admin",$target_type,$target,$filename,$ext){ $result=$this->uploadFile($filePath); $uri=$result['uri']; $uuid=$result['entities'][0]['uuid']; $shareSecret=$result['entities'][0]['share-secret']; $url=$this->url.'messages'; $body['target_type']=$target_type; $body['target']=$target; $options['type']="img"; $options['url']=$uri.'/'.$uuid; $options['filename']=$filename; $options['secret']=$shareSecret; $options['size']=array( "width"=>480, "height"=>720 ); $body['msg']=$options; $body['from']=$from; $body['ext']=$ext; $b=json_encode($body); $header=array($this->getToken()); //$b=json_encode($body,true); $result=$this->postCurl($url,$b,$header); return $result; } /* 發語音消息 */ function sendAudio($filePath,$from="admin",$target_type,$target,$filename,$length,$ext){ $result=$this->uploadFile($filePath); $uri=$result['uri']; $uuid=$result['entities'][0]['uuid']; $shareSecret=$result['entities'][0]['share-secret']; $url=$this->url.'messages'; $body['target_type']=$target_type; $body['target']=$target; $options['type']="audio"; $options['url']=$uri.'/'.$uuid; $options['filename']=$filename; $options['length']=$length; $options['secret']=$shareSecret; $body['msg']=$options; $body['from']=$from; $body['ext']=$ext; $b=json_encode($body); $header=array($this->getToken()); //$b=json_encode($body,true); $result=$this->postCurl($url,$b,$header); return $result;} /* 發視頻消息 */ function sendVedio($filePath,$from="admin",$target_type,$target,$filename,$length,$thumb,$thumb_secret,$ext){ $result=$this->uploadFile($filePath); $uri=$result['uri']; $uuid=$result['entities'][0]['uuid']; $shareSecret=$result['entities'][0]['share-secret']; $url=$this->url.'messages'; $body['target_type']=$target_type; $body['target']=$target; $options['type']="video"; $options['url']=$uri.'/'.$uuid; $options['filename']=$filename; $options['thumb']=$thumb; $options['length']=$length; $options['secret']=$shareSecret; $options['thumb_secret']=$thumb_secret; $body['msg']=$options; $body['from']=$from; $body['ext']=$ext; $b=json_encode($body); $header=array($this->getToken()); //$b=json_encode($body,true); $result=$this->postCurl($url,$b,$header); return $result; } /* 發文件消息 */ function sendFile($filePath,$from="admin",$target_type,$target,$filename,$length,$ext){ $result=$this->uploadFile($filePath); $uri=$result['uri']; $uuid=$result['entities'][0]['uuid']; $shareSecret=$result['entities'][0]['share-secret']; $url=$GLOBALS['base_url'].'messages'; $body['target_type']=$target_type; $body['target']=$target; $options['type']="file"; $options['url']=$uri.'/'.$uuid; $options['filename']=$filename; $options['length']=$length; $options['secret']=$shareSecret; $body['msg']=$options; $body['from']=$from; $body['ext']=$ext; $b=json_encode($body); $header=array(getToken()); //$b=json_encode($body,true); $result=postCurl($url,$b,$header); return $result; } //-------------------------------------------------------------羣組操做 /* 獲取app中的全部羣組----不分頁 */ function getGroups($limit=0){ if(!empty($limit)){ $url=$this->url.'chatgroups?limit='.$limit; }else{ $url=$this->url.'chatgroups'; } $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); return $result; } /* 獲取app中的全部羣組---分頁 */ function getGroupsForPage($limit=0,$cursor=''){ $url=$this->url.'chatgroups?limit='.$limit.'&cursor='.$cursor; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); if(!empty($result["cursor"])){ $cursor=$result["cursor"]; $this->writeCursor("groupfile.txt",$cursor); } //var_dump($GLOBALS['cursor'].'00000000000000'); return $result; } /* 獲取一個或多個羣組的詳情 */ function getGroupDetail($group_ids){ $g_ids=implode(',',$group_ids); $url=$this->url.'chatgroups/'.$g_ids; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 建立一個羣組 */ function createGroup($options){ $url=$this->url.'chatgroups'; $header=array($this->getToken()); $body=json_encode($options); $result=$this->postCurl($url,$body,$header); return $result; } /* 修改羣組信息 */ function modifyGroupInfo($group_id,$options){ $url=$this->url.'chatgroups/'.$group_id; $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header,'PUT'); return $result; } /* 刪除羣組 */ function deleteGroup($group_id){ $url=$this->url.'chatgroups/'.$group_id; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 獲取羣組中的成員 */ function getGroupUsers($group_id){ $url=$this->url.'chatgroups/'.$group_id.'/users'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 羣組單個加人 */ function addGroupMember($group_id,$username){ $url=$this->url.'chatgroups/'.$group_id.'/users/'.$username; $header=array($this->getToken(),'Content-Type:application/json'); $result=$this->postCurl($url,'',$header); return $result; } /* 羣組批量加人 */ function addGroupMembers($group_id,$usernames){ $url=$this->url.'chatgroups/'.$group_id.'/users'; $body=json_encode($usernames); $header=array($this->getToken(),'Content-Type:application/json'); $result=$this->postCurl($url,$body,$header); return $result; } /* 羣組單個減人 */ function deleteGroupMember($group_id,$username){ $url=$this->url.'chatgroups/'.$group_id.'/users/'.$username; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 羣組批量減人 */ function deleteGroupMembers($group_id,$usernames){ $url=$this->url.'chatgroups/'.$group_id.'/users/'.$usernames; //$body=json_encode($usernames); $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 獲取一個用戶參與的全部羣組 */ function getGroupsForUser($username){ $url=$this->url.'users/'.$username.'/joined_chatgroups'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 羣組轉讓 */ function changeGroupOwner($group_id,$options){ $url=$this->url.'chatgroups/'.$group_id; $body=json_encode($options); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header,'PUT'); return $result; } /* 查詢一個羣組黑名單用戶名列表 */ function getGroupBlackList($group_id){ $url=$this->url.'chatgroups/'.$group_id.'/blocks/users'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 羣組黑名單單個加人 */ function addGroupBlackMember($group_id,$username){ $url=$this->url.'chatgroups/'.$group_id.'/blocks/users/'.$username; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header); return $result; } /* 羣組黑名單批量加人 */ function addGroupBlackMembers($group_id,$usernames){ $url=$this->url.'chatgroups/'.$group_id.'/blocks/users'; $body=json_encode($usernames); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header); return $result; } /* 羣組黑名單單個減人 */ function deleteGroupBlackMember($group_id,$username){ $url=$this->url.'chatgroups/'.$group_id.'/blocks/users/'.$username; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 羣組黑名單批量減人 */ function deleteGroupBlackMembers($group_id,$usernames){ $url=$this->url.'chatgroups/'.$group_id.'/blocks/users'; $body=json_encode($usernames); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header,'DELETE'); return $result; } //-------------------------------------------------------------聊天室操做 /* 建立聊天室 */ function createChatRoom($options){ $url=$this->url.'chatrooms'; $header=array($this->getToken()); $body=json_encode($options); $result=$this->postCurl($url,$body,$header); return $result; } /* 修改聊天室信息 */ function modifyChatRoom($chatroom_id,$options){ $url=$this->url.'chatrooms/'.$chatroom_id; $body=json_encode($options); $result=$this->postCurl($url,$body,$header,'PUT'); return $result; } /* 刪除聊天室 */ function deleteChatRoom($chatroom_id){ $url=$this->url.'chatrooms/'.$chatroom_id; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 獲取app中全部的聊天室 */ function getChatRooms(){ $url=$this->url.'chatrooms'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); return $result; } /* 獲取一個聊天室的詳情 */ function getChatRoomDetail($chatroom_id){ $url=$this->url.'chatrooms/'.$chatroom_id; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 獲取一個用戶加入的全部聊天室 */ function getChatRoomJoined($username){ $url=$this->url.'users/'.$username.'/joined_chatrooms'; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'GET'); return $result; } /* 聊天室單個成員添加 */ function addChatRoomMember($chatroom_id,$username){ $url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$username; //$header=array($this->getToken()); $header=array($this->getToken(),'Content-Type:application/json'); $result=$this->postCurl($url,'',$header); return $result; } /* 聊天室批量成員添加 */ function addChatRoomMembers($chatroom_id,$usernames){ $url=$this->url.'chatrooms/'.$chatroom_id.'/users'; $body=json_encode($usernames); $header=array($this->getToken()); $result=$this->postCurl($url,$body,$header); return $result; } /* 聊天室單個成員刪除 */ function deleteChatRoomMember($chatroom_id,$username){ $url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$username; $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } /* 聊天室批量成員刪除 */ function deleteChatRoomMembers($chatroom_id,$usernames){ $url=$this->url.'chatrooms/'.$chatroom_id.'/users/'.$usernames; //$body=json_encode($usernames); $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,'DELETE'); return $result; } //-------------------------------------------------------------聊天記錄 /* 導出聊天記錄----不分頁 */ function getChatRecord($ql){ if(!empty($ql)){ $url=$this->url.'chatmessages?ql='.$ql; }else{ $url=$this->url.'chatmessages'; } $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); return $result; } /* 導出聊天記錄---分頁 */ function getChatRecordForPage($ql,$limit=0,$cursor){ if(!empty($ql)){ $url=$this->url.'chatmessages?ql='.$ql.'&limit='.$limit.'&cursor='.$cursor; } $header=array($this->getToken()); $result=$this->postCurl($url,'',$header,"GET"); $cursor=$result["cursor"]; $this->writeCursor("chatfile.txt",$cursor); //var_dump($GLOBALS['cursor'].'00000000000000'); return $result; } /** *$this->postCurl方法 */ function postCurl($url,$body,$header,$type="POST"){ //1.建立一個curl資源 $ch = curl_init(); //2.設置URL和相應的選項 curl_setopt($ch,CURLOPT_URL,$url);//設置url //1)設置請求頭 //array_push($header, 'Accept:application/json'); //array_push($header,'Content-Type:application/json'); //array_push($header, 'http:multipart/form-data'); //設置爲false,只會得到響應的正文(true的話會連響應頭一併獲取到) curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt ( $ch, CURLOPT_TIMEOUT,5); // 設置超時限制防止死循環 //設置發起鏈接前的等待時間,若是設置爲0,則無限等待。 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); //將curl_exec()獲取的信息以文件流的形式返回,而不是直接輸出。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //2)設備請求體 if (count($body)>0) { //$b=json_encode($body,true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//所有數據使用HTTP協議中的"POST"操做來發送。 } //設置請求頭 if(count($header)>0){ curl_setopt($ch,CURLOPT_HTTPHEADER,$header); } //上傳文件相關設置 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 對認證證書來源的檢查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// 從證書中檢查SSL加密算 //3)設置提交方式 switch($type){ case "GET": curl_setopt($ch,CURLOPT_HTTPGET,true); break; case "POST": curl_setopt($ch,CURLOPT_POST,true); break; case "PUT"://使用一個自定義的請求信息來代替"GET"或"HEAD"做爲HTTP請 求。這對於執行"DELETE" 或者其餘更隱蔽的HTT curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT"); break; case "DELETE": curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"DELETE"); break; } //4)在HTTP請求中包含一個"User-Agent: "頭的字符串。-----必設 curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0'); curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模擬用戶使用的瀏覽器 //5) //3.抓取URL並把它傳遞給瀏覽器 $res=curl_exec($ch); $result=json_decode($res,true); //4.關閉curl資源,而且釋放系統資源 curl_close($ch); if(empty($result)) return $res; else return $result; } }

將這個 class 保存到項目路徑中,在 controller 中 use 引入
For example :json

public function registerImuser($username,$password,$nickname){ $e = new Easemob(); $result_u = $e->createUser($username,$password); $result_n = $e->editNickname($username,$nickname); return $result_n; }

使用:數組

$this->registerImuser($post_name,$post_password,$post_nickname);

注意建立直播的流程 給用戶添加主播身份 , 設置直播流 , 建立直播間(建立直播間的同時環信會自動爲咱們建立直播間對應的聊天室)
Thinks!瀏覽器

轉載:https://learnku.com/articles/6721/ring-messenger-instant-messaging-integrated-clientapp

相關文章
相關標籤/搜索