TimRestApi.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. <?php
  2. require_once dirname(__FILE__) . '/TimRestInterface.php';
  3. class TimRestAPI extends TimRestInterface
  4. {
  5. #app基本信息
  6. protected $sdkappid = 0;
  7. protected $usersig = '';
  8. protected $identifier = '';
  9. #开放IM https接口参数, 一般不需要修改
  10. protected $http_type = 'https://';
  11. protected $method = 'post';
  12. protected $im_yun_url = 'console.tim.qq.com';
  13. protected $version = 'v4';
  14. protected $contenttype = 'json';
  15. protected $apn = '0';
  16. /**
  17. * 初始化函数
  18. * @param int $sdkappid 应用的appid
  19. * @param string $identifier 访问接口的用户
  20. */
  21. function init($sdkappid, $identifier)
  22. {
  23. $this->sdkappid = $sdkappid;
  24. $this->identifier = $identifier;
  25. }
  26. /**
  27. * 构造访问REST服务器的参数,并访问REST接口
  28. * @param string $server_name 服务名
  29. * @param string $cmd_name 命令名
  30. * @param string $identifier 用户名
  31. * @param string $usersig 用来鉴权的usersig
  32. * @param string $req_data 传递的json结构
  33. * $param bool $print_flag 是否打印请求,默认为打印
  34. * @return string $out 返回的签名字符串
  35. */
  36. public function api($service_name, $cmd_name, $identifier, $usersig, $req_data, $print_flag = false)
  37. {
  38. //$req_tmp用来做格式化输出
  39. $req_tmp = json_decode($req_data, true);
  40. # 构建HTTP请求参数,具体格式请参考 REST API接口文档 (http://avc.qcloud.com/wiki/im/)(即时通信云-数据管理REST接口)
  41. $parameter = "usersig=" . $this->usersig
  42. . "&identifier=" . $this->identifier
  43. . "&sdkappid=" . $this->sdkappid
  44. . "&contenttype=" . $this->contenttype;
  45. $url = $this->http_type . $this->im_yun_url . '/' . $this->version . '/' . $service_name . '/' .$cmd_name . '?' . $parameter;
  46. if($print_flag)
  47. {
  48. echo "Request Url:\n";
  49. echo $url;
  50. echo "\n";
  51. echo "Request Body:\n";
  52. echo json_format($req_tmp);
  53. echo "\n";
  54. }
  55. $ret = $this->http_req('https', 'post', $url, $req_data);
  56. return $ret;
  57. }
  58. /**
  59. * 构造访问REST服务器参数,并发访问REST服务器
  60. * @param string $server_name 服务名
  61. * @param string $cmd_name 命令名
  62. * @param string $identifier 用户名
  63. * @param string $usersig 用来鉴权的usersig
  64. * @param string $req_data 传递的json结构
  65. * $param bool $print_flag 是否打印请求,默认为打印
  66. * @return string $out 返回的签名字符串
  67. */
  68. public function multi_api($service_name, $cmd_name, $identifier, $usersig, $req_data, $print_flag = true)
  69. {
  70. //$req_tmp用来做格式化控制台输出,同时作为多路访问需要的数组结构
  71. $req_tmp = json_decode($req_data, true);
  72. # 构建HTTP请求参数,具体格式请参考 REST API接口文档 (http://avc.qcloud.com/wiki/im/)(即时通信云-数据管理REST接口)
  73. $parameter = "usersig=" . $this->usersig
  74. . "&identifier=" . $this->identifier
  75. . "&sdkappid=" . $this->sdkappid
  76. . "&contenttype=" . $this->contenttype;
  77. $url = $this->http_type . $this->im_yun_url . '/' . $this->version . '/' . $service_name . '/' .$cmd_name . '?' . $parameter;
  78. if($print_flag)
  79. {
  80. echo "Request Url:\n";
  81. echo $url;
  82. echo "\n";
  83. echo "Request Body:\n";
  84. echo json_format($req_tmp);
  85. echo "\n";
  86. }
  87. $ret = $this->http_req_multi('https', 'post', $url, $req_tmp);
  88. return $ret;
  89. }
  90. /**
  91. * 独立模式根据Identifier生成UserSig的方法
  92. * @param int $identifier 用户账号
  93. * @param int $expiry_after 过期时间
  94. * @param string $protected_key_path 私钥的存储路径及文件名
  95. * @return string $out 返回的签名字符串
  96. */
  97. public function generate_user_sig($identifier, $expiry_after, $protected_key_path, $tool_path)
  98. {
  99. # 这里需要写绝对路径,开发者根据自己的路径进行调整
  100. $command = escapeshellarg($tool_path)
  101. . ' '. escapeshellarg($protected_key_path)
  102. . ' ' . escapeshellarg($this->sdkappid)
  103. . ' ' .escapeshellarg($identifier);
  104. $ret = exec($command, $out, $status);
  105. if( $status == -1)
  106. {
  107. return null;
  108. }
  109. $this->usersig = $out[0];
  110. return $out;
  111. }
  112. /**
  113. * 托管模式设置用户凭证
  114. * @param string $protected_key_path 私钥的存储路径及文件名
  115. * @return bool 返回成功与否
  116. */
  117. public function set_user_sig($usr_sig)
  118. {
  119. $this->usersig = $usr_sig;
  120. return true;
  121. }
  122. /**
  123. * 向Rest服务器发送请求
  124. * @param string $http_type http类型,比如https
  125. * @param string $method 请求方式,比如POST
  126. * @param string $url 请求的url
  127. * @return string $data 请求的数据
  128. */
  129. public static function http_req($http_type, $method, $url, $data)
  130. {
  131. $ch = curl_init();
  132. if (strstr($http_type, 'https'))
  133. {
  134. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  135. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  136. }
  137. if ($method == 'post')
  138. {
  139. curl_setopt($ch, CURLOPT_POST, 1);
  140. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  141. } else
  142. {
  143. $url = $url . '?' . $data;
  144. }
  145. curl_setopt($ch, CURLOPT_URL, $url);
  146. curl_setopt($ch, CURLOPT_HEADER, 0);
  147. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  148. curl_setopt($ch, CURLOPT_TIMEOUT,100000);//超时时间
  149. try
  150. {
  151. $ret=curl_exec($ch);
  152. }catch(Exception $e)
  153. {
  154. curl_close($ch);
  155. return json_encode(array('ret'=>0,'msg'=>'failure'));
  156. }
  157. curl_close($ch);
  158. return $ret;
  159. }
  160. /**
  161. * 向Rest服务器发送多个请求(并发)
  162. * @param string $http_type http类型,比如https
  163. * @param string $method 请求方式,比如POST
  164. * @param string $url 请求的url
  165. * @return bool 是否成功
  166. */
  167. public static function http_req_multi($http_type, $method, $url, $data)
  168. {
  169. $mh = curl_multi_init();
  170. $ch_list = array();
  171. $i = -1;
  172. $req_list = array();
  173. foreach($data as $req_data)
  174. {
  175. $i++;
  176. $req_data = json_encode($req_data);
  177. $ch = curl_init();
  178. if ($http_type == 'https://')
  179. {
  180. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  181. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
  182. }
  183. if ($method == 'post')
  184. {
  185. curl_setopt($ch, CURLOPT_POST, 1);
  186. curl_setopt($ch, CURLOPT_POSTFIELDS, $req_data);
  187. } else
  188. {
  189. $url = $url . '?' . $data;
  190. }
  191. curl_setopt($ch, CURLOPT_URL, $url);
  192. curl_setopt($ch, CURLOPT_HEADER, 0);
  193. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  194. curl_setopt($ch, CURLOPT_TIMEOUT,100000);//超时时间
  195. curl_multi_add_handle($mh, $ch);
  196. $ch_list[] = $ch;
  197. $req_list[] = $req_data;
  198. }
  199. try
  200. {
  201. do{
  202. $mret = curl_multi_exec($mh, $active);
  203. }while($mret == CURLM_CALL_MULTI_PERFORM);
  204. while($active and $mret == CURLM_OK){
  205. if(curl_multi_select($mh) === -1){
  206. usleep(100);
  207. }
  208. do{
  209. $mret = curl_multi_exec($mh, $active);
  210. }while($mret == CURLM_CALL_MULTI_PERFORM);
  211. }
  212. }catch(Exception $e)
  213. {
  214. curl_close($ch);
  215. return json_encode(array('ret'=>0,'msg'=>'failure'));
  216. }
  217. for($i = 0; $i < count($ch_list); $i++)
  218. {
  219. $ret = curl_multi_getcontent($ch_list[$i]);
  220. if(strstr($ret, "URL_INFO"))
  221. {
  222. curl_multi_close($mh);
  223. return $ret;
  224. }
  225. $ret = json_decode($ret, true);
  226. echo json_format($ret);
  227. }
  228. curl_multi_close($mh);
  229. return true;
  230. }
  231. #REST API 访问接口集合
  232. #参数详情见RestInterface
  233. public function openim_send_msg($account_id, $receiver, $text_content)
  234. {
  235. #构造高级接口所需参数
  236. $msg_content = array();
  237. //创建array 所需元素
  238. $msg_content_elem = array(
  239. 'MsgType' => 'TIMTextElem', //文本类型
  240. 'MsgContent' => array(
  241. 'Text' => $text_content, //hello 为文本信息
  242. )
  243. );
  244. //将创建的元素$msg_content_elem, 加入array $msg_content
  245. array_push($msg_content, $msg_content_elem);
  246. $ret = $this->openim_send_msg2($account_id, $receiver, $msg_content);
  247. return $ret;
  248. }
  249. /**
  250. * 设置全局禁言 https://www.qcloud.com/doc/product/269/4230
  251. * @param int time 禁言时长,默认最长 4294967295 表示永久禁言,0 取消禁言
  252. */
  253. public function set_no_speaking($user_id, $time = 4294967295)
  254. {
  255. $user_id = (string)$user_id;
  256. $req_data = json_encode(array(
  257. 'Set_Account' => $user_id,
  258. 'C2CmsgNospeakingTime' => $time,
  259. 'GroupmsgNospeakingTime' => $time,
  260. ));
  261. $ret = $this->api("openconfigsvr", "setnospeaking", $this->identifier, $this->usersig, $req_data, false);
  262. return json_decode($ret,true);
  263. }
  264. public function openpic_pic_upload($account_id, $receiver, $pic_path, $busi_type)
  265. {
  266. #获取长度和md5值
  267. $pic_data = file_get_contents($pic_path);
  268. $md5 = md5($pic_data);
  269. $pic_size = filesize($pic_path);
  270. #进行base64处理
  271. $fp = fopen($pic_path, "r");
  272. $pic_data = fread($fp, $pic_size);
  273. $slice_data = array();
  274. $slice_size = array();
  275. $SLICE_SIZE = 32*4096;
  276. //对文件进行分片
  277. for($i = 0; $i < $pic_size; $i = $i + $SLICE_SIZE)
  278. {
  279. if($i + $SLICE_SIZE > $pic_size)
  280. {
  281. break;
  282. }
  283. $slice_tmp = substr($pic_data, $i, $SLICE_SIZE);
  284. $slice_tmp = chunk_split(base64_encode($slice_tmp));
  285. $slice_tmp = str_replace("\r\n", '', $slice_tmp);
  286. $slice_size[] = $SLICE_SIZE;
  287. $slice_data[] = $slice_tmp;
  288. }
  289. //最后一个分片
  290. if($i - $SLICE_SIZE < $pic_size)
  291. {
  292. $slice_size[] = $pic_size-$i;
  293. $tmp = substr($pic_data, $i, $pic_size-$i);
  294. $slice_size[] = strlen($tmp);
  295. $tmp = chunk_split(base64_encode($tmp));
  296. $tmp = str_replace("\r\n", '', $tmp);
  297. $slice_data[] = $tmp;
  298. }
  299. $pic_rand = rand(1, 65535);
  300. $time_stamp = time();
  301. $req_data_list = array();
  302. $sentOut = 0;
  303. printf("handle %d segments\n", count($slice_data)-1);
  304. for($i = 0; $i < count($slice_data)-1; $i++)
  305. {
  306. #构造消息
  307. $msg = array(
  308. "From_Account" => $account_id, //发送者
  309. "To_Account" => $receiver, //接收者
  310. "App_Version" => 1.4, //应用版本号
  311. "Seq" => $i+1, //同一个分片需要保持一致
  312. "Timestamp" => $time_stamp, //同一张图片的不同分片需要保持一致
  313. "Random" => $pic_rand, //同一张图片的不同分片需要保持一致
  314. "File_Str_Md5" => $md5, //图片MD5,验证图片的完整性
  315. "File_Size" => $pic_size, //图片原始大小
  316. "Busi_Id" => $busi_type, //群消息:1 c2c消息:2 个人头像:3 群头像:4
  317. "PkgFlag" => 1, //同一张图片要保持一致: 0表示图片数据没有被处理 ;1-表示图片经过base64编码,固定为1
  318. "Slice_Offset" => $i*$SLICE_SIZE, //必须是4K的整数倍
  319. "Slice_Size" => $slice_size[$i], //必须是4K的整数倍,除最后一个分片列外
  320. "Slice_Data" => $slice_data[$i] //PkgFlag=1时,为base64编码
  321. );
  322. array_push($req_data_list, $msg);
  323. $sentOut = 0;
  324. if ($i != 0 && ($i+1) % 4 == 0)
  325. {
  326. //将消息序列化为json串
  327. $req_data_list = json_encode($req_data_list);
  328. printf("\ni = %d, call multi_api once\n", $i);
  329. $ret = $this->multi_api("openpic", "pic_up", $this->identifier, $this->usersig, $req_data_list, false);
  330. if(gettype($ret) == "string")
  331. {
  332. $ret = json_decode($ret, true);
  333. return $ret;
  334. }
  335. $req_data_list = array();
  336. $sentOut = 1;
  337. }
  338. }
  339. if ($sentOut == 0)
  340. {
  341. $req_data_list = json_encode($req_data_list);
  342. printf("\ni = %d, call multi_api once\n", $i);
  343. $this->multi_api("openpic", "pic_up", $this->identifier, $this->usersig, $req_data_list, false);
  344. }
  345. #最后一个分片
  346. $msg = array(
  347. "From_Account" => $account_id, //发送者
  348. "To_Account" => $receiver, //接收者
  349. "App_Version" => 1.4, //应用版本号
  350. "Seq" => $i+1, //同一个分片需要保持一致
  351. "Timestamp" => $time_stamp, //同一张图片的不同分片需要保持一致
  352. "Random" => $pic_rand, //同一张图片的不同分片需要保持一致
  353. "File_Str_Md5" => $md5, //图片MD5,验证图片的完整性
  354. "File_Size" => $pic_size, //图片原始大小
  355. "Busi_Id" => $busi_type, //群消息:1 c2c消息:2 个人头像:3 群头像:4
  356. "PkgFlag" => 1, //同一张图片要保持一致: 0表示图片数据没有被处理 ;1-表示图片经过base64编码,固定为1
  357. "Slice_Offset" => $i*$SLICE_SIZE, //必须是4K的整数倍
  358. "Slice_Size" => $slice_size[count($slice_data)-1], //必须是4K的整数倍,除最后一个分片列外
  359. "Slice_Data" => $slice_data[count($slice_data)-1] //PkgFlag=1时,为base64编码
  360. );
  361. $req_data = json_encode($msg);
  362. $ret = $this->api("openpic", "pic_up", $this->identifier, $this->usersig, $req_data, false);
  363. $ret = json_decode($ret, true);
  364. echo json_format($ret);
  365. return $ret;
  366. }
  367. public function openim_send_msg_pic($account_id, $receiver, $pic_path)
  368. {
  369. #构造高级接口所需参数
  370. //上传图片并获取url
  371. $busi_type = 2; //表示C2C消息
  372. $ret = $this->openpic_pic_upload($account_id, $receiver, $pic_path, $busi_type);
  373. $tmp = $ret["URL_INFO"];
  374. $uuid = $ret["File_UUID"];
  375. $pic_url = $tmp[0]["DownUrl"];
  376. $img_info = array();
  377. $img_tmp = $ret["URL_INFO"][0];
  378. if($img_tmp["PIC_TYPE"] == 4){
  379. $img_tmp["PIC_TYPE"] = 3;
  380. }
  381. $img_info_elem1 = array(
  382. "URL" => $img_tmp["DownUrl"],
  383. "Height" => $img_tmp["PIC_Height"],
  384. "Size" => $img_tmp["PIC_Size"],
  385. "Type" => $img_tmp["PIC_TYPE"],
  386. "Width" => $img_tmp["PIC_Width"]
  387. );
  388. $img_tmp = $ret["URL_INFO"][1];
  389. if($img_tmp["PIC_TYPE"] == 4){
  390. $img_tmp["PIC_TYPE"] = 3;
  391. }
  392. $img_info_elem2 = array(
  393. "URL" => $img_tmp["DownUrl"],
  394. "Height" => $img_tmp["PIC_Height"],
  395. "Size" => $img_tmp["PIC_Size"],
  396. "Type" => $img_tmp["PIC_TYPE"],
  397. "Width" => $img_tmp["PIC_Width"]
  398. );
  399. $img_tmp = $ret["URL_INFO"][2];
  400. if($img_tmp["PIC_TYPE"] == 4){
  401. $img_tmp["PIC_TYPE"] = 3;
  402. }
  403. $img_info_elem3 = array(
  404. "URL" => $img_tmp["DownUrl"],
  405. "Height" => $img_tmp["PIC_Height"],
  406. "Size" => $img_tmp["PIC_Size"],
  407. "Type" => $img_tmp["PIC_TYPE"],
  408. "Width" => $img_tmp["PIC_Width"]
  409. );
  410. array_push($img_info, $img_info_elem1);
  411. array_push($img_info, $img_info_elem2);
  412. array_push($img_info, $img_info_elem3);
  413. $msg_content = array();
  414. //创建array 所需元素
  415. $msg_content_elem = array(
  416. 'MsgType' => 'TIMImageElem', //文本类型
  417. 'MsgContent' => array(
  418. 'UUID' => $uuid,
  419. 'ImageInfoArray' => $img_info,
  420. )
  421. );
  422. //将创建的元素$msg_content_elem, 加入array $msg_content
  423. array_push($msg_content, $msg_content_elem);
  424. $ret = $this->openim_send_msg2($account_id, $receiver, $msg_content);
  425. return $ret;
  426. }
  427. public function openim_send_msg2($account_id, $receiver, $msg_content)
  428. {
  429. #构造新消息
  430. $msg = array(
  431. 'To_Account' => $receiver,
  432. 'MsgSeq' => rand(1, 65535),
  433. 'MsgRandom' => rand(1, 65535),
  434. 'MsgTimeStamp' => time(),
  435. 'MsgBody' => $msg_content,
  436. 'From_Account' => $account_id
  437. );
  438. #将消息序列化为json串
  439. $req_data = json_encode($msg);
  440. $ret = $this->api("openim", "sendmsg", $this->identifier, $this->usersig, $req_data);
  441. $ret = json_decode($ret, true);
  442. return $ret;
  443. }
  444. public function openim_batch_sendmsg($account_list, $text_content)
  445. {
  446. #构造高级接口所需参数
  447. $msg_content = array();
  448. //创建array 所需元素
  449. $msg_content_elem = array(
  450. 'MsgType' => 'TIMTextElem', //文本类型
  451. 'MsgContent' => array(
  452. 'Text' => $text_content, //hello 为文本信息
  453. )
  454. );
  455. //将创建的元素$msg_content_elem, 加入array $msg_content
  456. array_push($msg_content, $msg_content_elem);
  457. $ret = $this->openim_batch_sendmsg2($account_list, $msg_content);
  458. return $ret;
  459. }
  460. public function openim_batch_sendmsg_pic($account_list, $pic_path)
  461. {
  462. #构造高级接口所需参数
  463. //上传图片并获取url
  464. $busi_type = 2; //表示C2C消息
  465. $ret = $this->openpic_pic_upload($this->identifier, $account_list[0], $pic_path, $busi_type);
  466. $tmp = $ret["URL_INFO"];
  467. $uuid = $ret["File_UUID"];
  468. $pic_url = $tmp[0]["DownUrl"];
  469. $img_info = array();
  470. $img_tmp = $ret["URL_INFO"][0];
  471. if($img_tmp["PIC_TYPE"] == 4){
  472. $img_tmp["PIC_TYPE"] = 3;
  473. }
  474. $img_info_elem1 = array(
  475. "URL" => $img_tmp["DownUrl"],
  476. "Height" => $img_tmp["PIC_Height"],
  477. "Size" => $img_tmp["PIC_Size"],
  478. "Type" => $img_tmp["PIC_TYPE"],
  479. "Width" => $img_tmp["PIC_Width"]
  480. );
  481. $img_tmp = $ret["URL_INFO"][1];
  482. if($img_tmp["PIC_TYPE"] == 4){
  483. $img_tmp["PIC_TYPE"] = 3;
  484. }
  485. $img_info_elem2 = array(
  486. "URL" => $img_tmp["DownUrl"],
  487. "Height" => $img_tmp["PIC_Height"],
  488. "Size" => $img_tmp["PIC_Size"],
  489. "Type" => $img_tmp["PIC_TYPE"],
  490. "Width" => $img_tmp["PIC_Width"]
  491. );
  492. $img_tmp = $ret["URL_INFO"][2];
  493. if($img_tmp["PIC_TYPE"] == 4){
  494. $img_tmp["PIC_TYPE"] = 3;
  495. }
  496. $img_info_elem3 = array(
  497. "URL" => $img_tmp["DownUrl"],
  498. "Height" => $img_tmp["PIC_Height"],
  499. "Size" => $img_tmp["PIC_Size"],
  500. "Type" => $img_tmp["PIC_TYPE"],
  501. "Width" => $img_tmp["PIC_Width"]
  502. );
  503. array_push($img_info, $img_info_elem1);
  504. array_push($img_info, $img_info_elem2);
  505. array_push($img_info, $img_info_elem3);
  506. $msg_content = array();
  507. //创建array 所需元素
  508. $msg_content_elem = array(
  509. 'MsgType' => 'TIMImageElem', //文本类型
  510. 'MsgContent' => array(
  511. 'UUID' => $uuid,
  512. 'ImageInfoArray' => $img_info,
  513. )
  514. );
  515. //将创建的元素$msg_content_elem, 加入array $msg_content
  516. array_push($msg_content, $msg_content_elem);
  517. $ret = $this->openim_batch_sendmsg2($account_list, $msg_content);
  518. return $ret;
  519. }
  520. public function openim_batch_sendmsg2($account_list, $msg_content)
  521. {
  522. #构造新消息
  523. $msg = array(
  524. 'To_Account' => $account_list,
  525. 'MsgRandom' => rand(1, 65535),
  526. 'MsgBody' => $msg_content,
  527. );
  528. #将消息序列化为json串
  529. $req_data = json_encode($msg);
  530. $ret = $this->api("openim", "batchsendmsg", $this->identifier, $this->usersig, $req_data);
  531. $ret = json_decode($ret, true);
  532. return $ret;
  533. }
  534. public function account_import($identifier, $nick, $face_url)
  535. {
  536. #构造新消息
  537. $msg = array(
  538. 'Identifier' => $identifier,
  539. 'Nick' => $nick,
  540. 'FaceUrl' => $face_url,
  541. );
  542. #将消息序列化为json串
  543. $req_data = json_encode($msg);
  544. $ret = $this->api("im_open_login_svc", "account_import", $this->identifier, $this->usersig, $req_data);
  545. $ret = json_decode($ret, true);
  546. return $ret;
  547. }
  548. public function register_account($identifier, $identifierType, $password)
  549. {
  550. #构造新消息
  551. $msg = array(
  552. 'Identifier' => $identifier,
  553. 'IdentifierType' => $identifierType,
  554. 'Password' => $password,
  555. );
  556. #将消息序列化为json串
  557. $req_data = json_encode($msg);
  558. $ret = $this->api("registration_service", "register_account_v1", $this->identifier, $this->usersig, $req_data);
  559. $ret = json_decode($ret, true);
  560. return $ret;
  561. }
  562. public function profile_portrait_get($account_id)
  563. {
  564. #构造高级接口所需参数
  565. $account_list = array();
  566. array_push($account_list, $account_id);
  567. $tag_list = array(
  568. "Tag_Profile_IM_Nick",
  569. "Tag_Profile_IM_AllowType"
  570. );
  571. $ret = $this->profile_portrait_get2($account_list, $tag_list);
  572. return $ret;
  573. }
  574. public function profile_portrait_get2($account_list, $tag_list)
  575. {
  576. #构造高级接口所需参数
  577. $msg = array(
  578. 'From_Account' => $this->identifier,
  579. 'To_Account' => $account_list,
  580. 'TagList' => $tag_list,
  581. 'LastStandardSequence' => 0
  582. );
  583. #将消息序列化为json串
  584. $req_data = json_encode($msg);
  585. $ret = $this->api("profile", "portrait_get", $this->identifier, $this->usersig, $req_data);
  586. $ret = json_decode($ret, true);
  587. return $ret;
  588. }
  589. public function profile_portrait_set($account_id, $new_name)
  590. {
  591. #构造高级接口所需参数
  592. $profile_list = array();
  593. $profile_nick = array(
  594. "Tag" => "Tag_Profile_IM_Nick",
  595. "Value" => $new_name
  596. );
  597. //加好友验证方式
  598. $profile_allow = array(
  599. "Tag" => "Tag_Profile_IM_AllowType",
  600. "Value" => "NeedPermission"
  601. );
  602. array_push($profile_list, $profile_nick);
  603. //array_push($profile_list, $profile_allow);
  604. $ret = $this->profile_portrait_set2($account_id, $profile_list);
  605. return $ret;
  606. }
  607. public function profile_portrait_set2($account_id, $profile_list)
  608. {
  609. #构造新消息
  610. $msg = array(
  611. 'From_Account' => $account_id,
  612. 'ProfileItem' => $profile_list
  613. );
  614. #将消息序列化为json串
  615. $req_data = json_encode($msg);
  616. $ret = $this->api("profile", "portrait_set", $this->identifier, $this->usersig, $req_data);
  617. $ret = json_decode($ret, true);
  618. return $ret;
  619. }
  620. public function sns_friend_import($account_id, $receiver)
  621. {
  622. #构造新消息
  623. $msg = array(
  624. 'From_Account' => $account_id,
  625. 'AddFriendItem' => array()
  626. );
  627. $receiver_arr = array(
  628. 'To_Account' => $receiver,
  629. 'Remark' => "",
  630. 'AddSource' => "AddSource_Type_Unknow",
  631. 'AddWording' => ""
  632. );
  633. array_push($msg['AddFriendItem'], $receiver_arr);
  634. #将消息序列化为json串
  635. $req_data = json_encode($msg);
  636. $ret = $this->api("sns", "friend_import", $this->identifier, $this->usersig, $req_data);
  637. $ret = json_decode($ret, true);
  638. return $ret;
  639. }
  640. public function sns_friend_delete($account_id, $frd_id)
  641. {
  642. #构造新消息
  643. $frd_list = array();
  644. //要添加的好友用户
  645. array_push($frd_list, $frd_id);
  646. $msg = array(
  647. 'From_Account' => $account_id,
  648. 'To_Account' => $frd_list,
  649. 'DeleteType' => "Delete_Type_Both"
  650. );
  651. #将消息序列化为json串
  652. $req_data = json_encode($msg);
  653. $ret = $this->api("sns", "friend_delete", $this->identifier, $this->usersig, $req_data);
  654. $ret = json_decode($ret, true);
  655. return $ret;
  656. }
  657. public function sns_friend_delete_all($account_id)
  658. {
  659. #构造新消息
  660. $msg = array(
  661. 'From_Account' => $account_id,
  662. );
  663. #将消息序列化为json串
  664. $req_data = json_encode($msg);
  665. $ret = $this->api("sns", "friend_delete_all", $this->identifier, $this->usersig, $req_data);
  666. $ret = json_decode($ret, true);
  667. return $ret;
  668. }
  669. public function sns_friend_check($account_id, $to_account)
  670. {
  671. #构造高级接口所需参数
  672. $to_account_list = array();
  673. //要添加的好友用户
  674. array_push($to_account_list, $to_account);
  675. $msg = array(
  676. 'From_Account' => $account_id,
  677. 'To_Account' => $to_account_list,
  678. );
  679. $ret = $this->sns_friend_check2($account_id, $to_account_list, "CheckResult_Type_Both");
  680. return $ret;
  681. }
  682. public function sns_friend_check2($account_id, $to_account_list, $check_type)
  683. {
  684. #构造新消息
  685. $msg = array(
  686. 'From_Account' => $account_id,
  687. 'To_Account' => $to_account_list,
  688. 'CheckType' => $check_type
  689. );
  690. #将消息序列化为json串
  691. $req_data = json_encode($msg);
  692. $ret = $this->api("sns", "friend_check", $this->identifier, $this->usersig, $req_data);
  693. $ret = json_decode($ret, true);
  694. return $ret;
  695. }
  696. function sns_friend_get_all($account_id)
  697. {
  698. #构造高级接口所需参数
  699. $tag_list = array(
  700. "Tag_Profile_IM_Nick",
  701. "Tag_SNS_IM_Remark"
  702. );
  703. $ret = $this->sns_friend_get_all2($account_id, $tag_list);
  704. return $ret;
  705. }
  706. function sns_friend_get_all2($account_id, $tag_list)
  707. {
  708. #构造新消息
  709. $msg = array(
  710. 'From_Account' => $account_id,
  711. 'TimeStamp' => 0,
  712. 'TagList' => $tag_list,
  713. 'LastStandardSequence' => 1,
  714. );
  715. #将消息序列化为json串
  716. $req_data = json_encode($msg);
  717. $ret = $this->api("sns", "friend_get_all", $this->identifier, $this->usersig, $req_data);
  718. $ret = json_decode($ret, true);
  719. return $ret;
  720. }
  721. function sns_friend_get_list($account_id, $frd_id)
  722. {
  723. #构造高级接口所需参数
  724. $frd_list = array();
  725. array_push($frd_list, $frd_id);
  726. $tag_list = array(
  727. "Tag_Profile_IM_Nick",
  728. "Tag_SNS_IM_Remark"
  729. );
  730. $ret = $this->sns_friend_get_list2($account_id, $frd_list, $tag_list);
  731. return $ret;
  732. }
  733. function sns_friend_get_list2($account_id, $frd_list, $tag_list)
  734. {
  735. #构造新消息
  736. $msg = array(
  737. 'From_Account' => $account_id,
  738. 'To_Account' => $frd_list,
  739. 'TagList' => $tag_list,
  740. );
  741. #将消息序列化为json串
  742. $req_data = json_encode($msg);
  743. $ret = $this->api("sns", "friend_get_list", $this->identifier, $this->usersig, $req_data);
  744. $ret = json_decode($ret, true);
  745. return $ret;
  746. }
  747. function group_get_appid_group_list()
  748. {
  749. #构造高级接口所需参数
  750. $ret = $this->group_get_appid_group_list2(50, null, null);
  751. return $ret;
  752. }
  753. function group_get_appid_group_list2($limit, $offset, $group_type)
  754. {
  755. #构造新消息
  756. $msg = array(
  757. 'Limit' => $limit,
  758. 'Offset' => $offset,
  759. 'GroupType' => $group_type
  760. );
  761. #将消息序列化为json串
  762. $req_data = json_encode($msg);
  763. $ret = $this->api("group_open_http_svc", "get_appid_group_list", $this->identifier, $this->usersig, $req_data);
  764. $ret = json_decode($ret, true);
  765. return $ret;
  766. }
  767. //创建全员广播大群
  768. function full_group_create($group_id,$aes_key){
  769. #构造新消息
  770. $Introduction = '';
  771. if($aes_key!=''){
  772. $Introduction = $aes_key;
  773. }
  774. $msg = array(
  775. 'Owner_Account' => $this->identifier,
  776. 'Type' => 'BChatRoom',
  777. 'GroupId' => $group_id,
  778. 'Name' => 'FullGroup',
  779. 'Introduction' => $Introduction,
  780. );
  781. #将消息序列化为json串
  782. $req_data = json_encode($msg);
  783. $ret = $this->api("group_open_http_svc", "create_group", $this->identifier, $this->usersig, $req_data);
  784. $ret = json_decode($ret, true);
  785. return $ret;
  786. }
  787. function group_create_group($group_type, $group_name, $owner_id,$group_id=null)
  788. {
  789. #构造高级接口所需参数
  790. $info_set = array(
  791. 'group_id' => $group_id,
  792. 'introduction' => null,
  793. 'notification' => null,
  794. 'face_url' => null,
  795. 'max_member_num' => 500,
  796. );
  797. $mem_list = array();
  798. $ret = $this->group_create_group2($group_type, $group_name, $owner_id, $info_set, $mem_list);
  799. return $ret;
  800. }
  801. function group_create_group2($group_type, $group_name, $owner_id, $info_set, $mem_list)
  802. {
  803. #构造新消息
  804. $msg = array(
  805. 'Type' => $group_type,
  806. 'Name' => $group_name,
  807. 'Owner_Account' => $owner_id,
  808. 'GroupId' => $info_set['group_id'],
  809. 'Introduction' => $info_set['introduction'],
  810. 'Notification' => $info_set['notification'],
  811. 'FaceUrl' => $info_set['face_url'],
  812. 'MaxMemberCount' => $info_set['max_member_num'],
  813. // 'ApplyJoinOption' => $info_set['apply_join'],
  814. 'MemberList' => $mem_list
  815. );
  816. #将消息序列化为json串
  817. $req_data = json_encode($msg);
  818. $ret = $this->api("group_open_http_svc", "create_group", $this->identifier, $this->usersig, $req_data);
  819. $ret = json_decode($ret, true);
  820. return $ret;
  821. }
  822. function group_change_group_owner($group_id, $new_owner)
  823. {
  824. #构造新消息
  825. $msg = array(
  826. 'GroupId' => $group_id,
  827. 'NewOwner_Account' => $new_owner
  828. );
  829. #将消息序列化为json串
  830. $req_data = json_encode($msg);
  831. $ret = $this->api("group_open_http_svc", "change_group_owner", $this->identifier, $this->usersig, $req_data);
  832. $ret = json_decode($ret, true);
  833. return $ret;
  834. }
  835. function group_get_group_info($group_id)
  836. {
  837. #构造高级接口所需参数
  838. $group_list = array();
  839. array_push($group_list, $group_id);
  840. $base_info_filter = array(
  841. "Type", //群类型(包括Public(公开群), Private(私密群), ChatRoom(聊天室))
  842. "Name", //群名称
  843. "Introduction", //群简介
  844. "Notification", //群公告
  845. "FaceUrl", //群头像url地址
  846. "CreateTime", //群组创建时间
  847. "Owner_Account", //群主id
  848. "LastInfoTime", //最后一次系统通知时间
  849. "LastMsgTime", //最后一次消息发送时间
  850. "MemberNum", //群组当前成员数目
  851. "MaxMemberNum", //群组内最大成员数目
  852. "ApplyJoinOption" //加群处理方式(比如FreeAccess 自由加入)
  853. );
  854. $member_info_filter = array(
  855. "Account", // 成员ID
  856. "Role", // 成员身份
  857. "JoinTime", // 成员加入时间
  858. "LastSendMsgTime", // 该成员最后一次发送消息时间
  859. "ShutUpUntil" // 该成员被禁言直到某时间
  860. );
  861. $app_define_filter = array(
  862. "GroupTestData1", //自定义数据
  863. );
  864. $ret = $this->group_get_group_info2($group_list, $base_info_filter, $member_info_filter, $app_define_filter);
  865. return $ret;
  866. }
  867. function group_get_group_info2($group_list, $base_info_filter, $member_info_filter, $app_define_filter)
  868. {
  869. #构造新消息
  870. $filter = new Filter();
  871. $filter->GroupBaseInfoFilter = $base_info_filter;
  872. $filter->MemberInfoFilter = $member_info_filter;
  873. $filter->AppDefinedDataFilter_Group = $app_define_filter;
  874. $msg = array(
  875. 'GroupIdList' => $group_list,
  876. 'ResponseFilter' => $filter
  877. );
  878. #将消息序列化为json串
  879. $req_data = json_encode($msg);
  880. $ret = $this->api("group_open_http_svc", "get_group_info", $this->identifier, $this->usersig, $req_data);
  881. $ret = json_decode($ret, true);
  882. return $ret;
  883. }
  884. function group_get_group_member_info($group_id, $limit, $offset)
  885. {
  886. #构造新消息
  887. $msg = array(
  888. "GroupId" => $group_id,
  889. "Limit" => $limit,
  890. "Offset" => $offset
  891. )
  892. ;
  893. #将消息序列化为json串
  894. $req_data = json_encode($msg);
  895. $ret = $this->api("group_open_http_svc", "get_group_member_info", $this->identifier, $this->usersig, $req_data);
  896. $ret = json_decode($ret, true);
  897. return $ret;
  898. }
  899. function group_modify_group_base_info($group_id, $group_name)
  900. {
  901. #构造高级接口所需参数
  902. $info_set = array(
  903. 'introduction' => null,
  904. 'notification' => null,
  905. 'face_url' => null,
  906. 'max_member_num' => null,
  907. // 'apply_join' => "NeedPermission"
  908. );
  909. $app_define_list = array();
  910. $ret = $this->group_modify_group_base_info2($group_id, $group_name, $info_set, $app_define_list);
  911. return $ret;
  912. }
  913. function group_modify_group_base_info2($group_id, $group_name, $info_set, $app_define_list)
  914. {
  915. #构造新消息
  916. $msg = array(
  917. "GroupId" => $group_id,
  918. "Name" => $group_name,
  919. "Introduction" => $info_set['introduction'],
  920. "Notification" => $info_set['notification'],
  921. "FaceUrl" => $info_set['face_url'],
  922. "MaxMemberNum" => $info_set['max_member_num'],
  923. // "ApplyJoinOption" => $info_set['apply_join'],
  924. "AppDefinedData" => $app_define_list
  925. );
  926. #将消息序列化为json串
  927. $req_data = json_encode($msg);
  928. $ret = $this->api("group_open_http_svc", "modify_group_base_info", $this->identifier, $this->usersig, $req_data);
  929. $ret = json_decode($ret, true);
  930. return $ret;
  931. }
  932. function group_add_group_member($group_id, $member_id, $silence)
  933. {
  934. #构造新消息
  935. $mem_list = array();
  936. $mem_elem = array(
  937. "Member_Account" => $member_id
  938. );
  939. array_push($mem_list, $mem_elem);
  940. $msg = array(
  941. "GroupId" => $group_id,
  942. "MemberList" => $mem_list,
  943. "Silence" => $silence
  944. );
  945. #将消息序列化为json串
  946. $req_data = json_encode($msg);
  947. $ret = $this->api("group_open_http_svc", "add_group_member", $this->identifier, $this->usersig, $req_data);
  948. $ret = json_decode($ret, true);
  949. return $ret;
  950. }
  951. function group_add_group_member2($group_id, $mem_list, $silence)
  952. {
  953. #构造新消息
  954. $msg = array(
  955. "GroupId" => $group_id,
  956. "MemberList" => $mem_list,
  957. "Silence" => $silence
  958. );
  959. #将消息序列化为json串
  960. $req_data = json_encode($msg);
  961. $ret = $this->api("group_open_http_svc", "add_group_member", $this->identifier, $this->usersig, $req_data);
  962. $ret = json_decode($ret, true);
  963. return $ret;
  964. }
  965. function group_delete_group_member($group_id, $member_id, $silence)
  966. {
  967. #构造新消息
  968. $mem_list = array();
  969. array_push($mem_list, $member_id);
  970. $msg = array(
  971. "GroupId" => $group_id,
  972. "MemberToDel_Account" => $mem_list,
  973. "Silence" => $silence
  974. );
  975. #将消息序列化为json串
  976. $req_data = json_encode($msg);
  977. $ret = $this->api("group_open_http_svc", "delete_group_member", $this->identifier, $this->usersig, $req_data);
  978. $ret = json_decode($ret, true);
  979. return $ret;
  980. }
  981. function group_modify_group_member_info($group_id, $account_id, $role)
  982. {
  983. #构造高级接口所需参数
  984. $ret = $this->group_modify_group_member_info2($group_id, $account_id, $role, "AcceptAndNotify", 0);
  985. return $ret;
  986. }
  987. function group_modify_group_member_info2($group_id, $account_id, $role, $msg_flag, $shutup_time)
  988. {
  989. #构造新消息
  990. $msg = array(
  991. "GroupId" => $group_id,
  992. "Member_Account" => $account_id,
  993. "Role" => $role
  994. )
  995. ;
  996. #将消息序列化为json串
  997. $req_data = json_encode($msg);
  998. $ret = $this->api("group_open_http_svc", "modify_group_member_info", $this->identifier, $this->usersig, $req_data);
  999. $ret = json_decode($ret, true);
  1000. return $ret;
  1001. }
  1002. function group_destroy_group($group_id)
  1003. {
  1004. #构造新消息
  1005. $msg = array(
  1006. "GroupId" => $group_id,
  1007. )
  1008. ;
  1009. #将消息序列化为json串
  1010. $req_data = json_encode($msg);
  1011. $ret = $this->api("group_open_http_svc", "destroy_group", $this->identifier, $this->usersig, $req_data);
  1012. $ret = json_decode($ret, true);
  1013. return $ret;
  1014. }
  1015. function group_get_joined_group_list($account_id)
  1016. {
  1017. #构造高级接口所需参数
  1018. $base_info_filter = array(
  1019. "Type", //群类型(包括Public(公开群), Private(私密群), ChatRoom(聊天室))
  1020. "Name", //群名称
  1021. "Introduction", //群简介
  1022. "Notification", //群公告
  1023. "FaceUrl", //群头像url地址
  1024. "CreateTime", //群组创建时间
  1025. "Owner_Account", //群主id
  1026. "LastInfoTime", //最后一次系统通知时间
  1027. "LastMsgTime", //最后一次消息发送时间
  1028. "MemberNum", //群组当前成员数目
  1029. "MaxMemberNum", //群组内最大成员数目
  1030. "ApplyJoinOption" //申请加群处理方式(比如FreeAccess 自由加入, NeedPermission 需要同意)
  1031. );
  1032. $self_info_filter = array(
  1033. "Role", //群内身份(Amin/Member)
  1034. "JoinTime", //入群时间
  1035. "MsgFlag", //消息屏蔽类型
  1036. "UnreadMsgNum" //未读消息数量
  1037. );
  1038. $ret = $this->group_get_joined_group_list2($account_id, null, $base_info_filter, $self_info_filter);
  1039. return $ret;
  1040. }
  1041. function group_get_joined_group_list2($account_id, $group_type, $base_info_filter, $self_info_filter)
  1042. {
  1043. #构造新消息
  1044. $filter = new Filter();
  1045. $filter->GroupBaseInfoFilter = $base_info_filter;
  1046. $filter->SelfInfoFilter = $self_info_filter;
  1047. $msg = array(
  1048. "Member_Account" => $account_id,
  1049. "ResponseFilter" => $filter
  1050. );
  1051. #将消息序列化为json串
  1052. $req_data = json_encode($msg);
  1053. $ret = $this->api("group_open_http_svc", "get_joined_group_list", $this->identifier, $this->usersig, $req_data);
  1054. $ret = json_decode($ret, true);
  1055. return $ret;
  1056. }
  1057. function group_get_role_in_group($group_id, $member_id)
  1058. {
  1059. #构造新消息
  1060. $mem_list = array();
  1061. array_push($mem_list, $member_id);
  1062. $msg = array(
  1063. "GroupId" => $group_id,
  1064. "User_Account" => $mem_list,
  1065. )
  1066. ;
  1067. #将消息序列化为json串
  1068. $req_data = json_encode($msg);
  1069. $ret = $this->api("group_open_http_svc", "get_role_in_group", $this->identifier, $this->usersig, $req_data);
  1070. $ret = json_decode($ret, true);
  1071. return $ret;
  1072. }
  1073. function group_forbid_send_msg($group_id, $member_id, $second)
  1074. {
  1075. #构造新消息
  1076. $mem_list = array();
  1077. array_push($mem_list, $member_id);
  1078. $msg = array(
  1079. "GroupId" => $group_id,
  1080. "Members_Account" => $mem_list,
  1081. "ShutUpTime" => $second
  1082. );
  1083. #将消息序列化为json串
  1084. $req_data = json_encode($msg);
  1085. $ret = $this->api("group_open_http_svc", "forbid_send_msg", $this->identifier, $this->usersig, $req_data);
  1086. $ret = json_decode($ret, true);
  1087. return $ret;
  1088. }
  1089. //获取群组被禁言用户列表
  1090. function get_group_shutted_uin($group_id)
  1091. {
  1092. #构造新消息
  1093. $msg = array(
  1094. "GroupId" => $group_id,
  1095. );
  1096. #将消息序列化为json串
  1097. $req_data = json_encode($msg);
  1098. $ret = $this->api("group_open_http_svc", "get_group_shutted_uin", $this->identifier, $this->usersig, $req_data);
  1099. $ret = json_decode($ret, true);
  1100. return $ret;
  1101. }
  1102. function group_send_group_msg($account_id, $group_id, $text_content)
  1103. {
  1104. #构造高级接口所需参数
  1105. $msg_content = array();
  1106. //创建array 所需元素
  1107. $msg_content_elem = array(
  1108. 'MsgType' => 'TIMTextElem', //文本类型
  1109. 'MsgContent' => array(
  1110. 'Text' => $text_content, //hello 为文本信息
  1111. )
  1112. );
  1113. array_push($msg_content, $msg_content_elem);
  1114. $ret = $this->group_send_group_msg2($account_id, $group_id, $msg_content);
  1115. return $ret;
  1116. }
  1117. function group_send_group_msg_pic($account_id, $group_id, $pic_path)
  1118. {
  1119. #构造高级接口所需参数
  1120. //上传图片并获取url
  1121. $busi_type = 1; //表示群消息
  1122. $ret = $this->openpic_pic_upload($account_id, $group_id, $pic_path, $busi_type);
  1123. $tmp = $ret["URL_INFO"];
  1124. $uuid = $ret["File_UUID"];
  1125. $pic_url = $tmp[0]["DownUrl"];
  1126. $img_info = array();
  1127. $img_tmp = $ret["URL_INFO"][0];
  1128. if($img_tmp["PIC_TYPE"] == 4){
  1129. $img_tmp["PIC_TYPE"] = 3;
  1130. }
  1131. $img_info_elem1 = array(
  1132. "URL" => $img_tmp["DownUrl"],
  1133. "Height" => $img_tmp["PIC_Height"],
  1134. "Size" => $img_tmp["PIC_Size"],
  1135. "Type" => $img_tmp["PIC_TYPE"],
  1136. "Width" => $img_tmp["PIC_Width"]
  1137. );
  1138. $img_tmp = $ret["URL_INFO"][1];
  1139. if($img_tmp["PIC_TYPE"] == 4){
  1140. $img_tmp["PIC_TYPE"] = 3;
  1141. }
  1142. $img_info_elem2 = array(
  1143. "URL" => $img_tmp["DownUrl"],
  1144. "Height" => $img_tmp["PIC_Height"],
  1145. "Size" => $img_tmp["PIC_Size"],
  1146. "Type" => $img_tmp["PIC_TYPE"],
  1147. "Width" => $img_tmp["PIC_Width"]
  1148. );
  1149. $img_tmp = $ret["URL_INFO"][2];
  1150. if($img_tmp["PIC_TYPE"] == 4){
  1151. $img_tmp["PIC_TYPE"] = 3;
  1152. }
  1153. $img_info_elem3 = array(
  1154. "URL" => $img_tmp["DownUrl"],
  1155. "Height" => $img_tmp["PIC_Height"],
  1156. "Size" => $img_tmp["PIC_Size"],
  1157. "Type" => $img_tmp["PIC_TYPE"],
  1158. "Width" => $img_tmp["PIC_Width"]
  1159. );
  1160. array_push($img_info, $img_info_elem1);
  1161. array_push($img_info, $img_info_elem2);
  1162. array_push($img_info, $img_info_elem3);
  1163. $msg_content = array();
  1164. //创建array 所需元素
  1165. $msg_content_elem = array(
  1166. 'MsgType' => 'TIMImageElem', //文本类型
  1167. 'MsgContent' => array(
  1168. 'UUID' => $uuid,
  1169. 'ImageInfoArray' => $img_info,
  1170. )
  1171. );
  1172. //将创建的元素$msg_content_elem, 加入array $msg_content
  1173. array_push($msg_content, $msg_content_elem);
  1174. $ret = $this->group_send_group_msg2($account_id, $group_id, $msg_content);
  1175. return $ret;
  1176. }
  1177. function group_send_group_msg2($account_id, $group_id, $msg_content)
  1178. {
  1179. //MsgPriority 可以指定4种优先级,从高到低依次为High,Normal,Low,Lowest,区分大小写。
  1180. $account_id = (string)$account_id;
  1181. if($account_id==''){
  1182. $account_id = $this->identifier;
  1183. }
  1184. #构造新消息
  1185. $msg = array(
  1186. "GroupId" => $group_id,
  1187. "From_Account" => $account_id,
  1188. "MsgPriority" => "High",
  1189. "Random" => rand(1, 65535),
  1190. "MsgBody" => $msg_content
  1191. );
  1192. #将消息序列化为json串
  1193. $req_data = json_encode($msg);
  1194. $ret = $this->api("group_open_http_svc", "send_group_msg", $this->identifier, $this->usersig, $req_data);
  1195. $ret = json_decode($ret, true);
  1196. return $ret;
  1197. }
  1198. function group_send_group_system_notification($group_id, $text_content, $receiver_id)
  1199. {
  1200. #构造高级接口所需参数
  1201. $receiver_list = array();
  1202. if($receiver_id != null){
  1203. array_push($receiver_list, $receiver_id);
  1204. }
  1205. $ret = $this->group_send_group_system_notification2($group_id, $text_content, $receiver_list);
  1206. return $ret;
  1207. }
  1208. function group_send_group_system_notification2($group_id, $content, $receiver_list)
  1209. {
  1210. #构造新消息
  1211. $msg = array(
  1212. "GroupId" => $group_id,
  1213. "ToMembers_Account" => $receiver_list,
  1214. "Content" => $content
  1215. );
  1216. #将消息序列化为json串
  1217. $req_data = json_encode($msg);
  1218. $ret = $this->api("group_open_http_svc", "send_group_system_notification", $this->identifier, $this->usersig, $req_data);
  1219. $ret = json_decode($ret, true);
  1220. return $ret;
  1221. }
  1222. function comm_rest($server, $command, $req_body)
  1223. {
  1224. #将消息序列化为json串
  1225. $req_data = json_encode($req_body);
  1226. $ret = $this->api($server, $command, $this->identifier, $this->usersig, $req_data);
  1227. $ret = json_decode($ret, true);
  1228. return $ret;
  1229. }
  1230. function group_import_group_member($group_id, $member_id, $role)
  1231. {
  1232. #构造高级接口所需参数
  1233. $member_list = array();
  1234. $member_elem = array(
  1235. "Member_Account" => $member_id,
  1236. "Role" => $role
  1237. );
  1238. array_push($member_list, $member_elem);
  1239. $ret = $this->group_import_group_member2($group_id, $member_list);
  1240. return $ret;
  1241. }
  1242. function group_import_group_member2($group_id, $member_list)
  1243. {
  1244. #构造新消息
  1245. $msg = array(
  1246. "GroupId" => $group_id,
  1247. "MemberList" => $member_list,
  1248. );
  1249. #将消息序列化为json串
  1250. $req_data = json_encode($msg);
  1251. $ret = $this->api("group_open_http_svc", "import_group_member", $this->identifier, $this->usersig, $req_data);
  1252. $ret = json_decode($ret, true);
  1253. return $ret;
  1254. }
  1255. function group_import_group_msg($group_id, $from_account, $text)
  1256. {
  1257. #构造高级接口所需参数
  1258. //构造MsgBody
  1259. $msg_content = array(
  1260. "Text" => $text
  1261. );
  1262. $msg_body_elem = array(
  1263. "MsgType" => "TIMTextElem",
  1264. "MsgContent" => $msg_content,
  1265. );
  1266. $msg_body_list = array();
  1267. array_push($msg_body_list, $msg_body_elem);
  1268. //构造MsgList的一个元素
  1269. $msg_list_elem = array(
  1270. "From_Account" => $from_account,
  1271. "SendTime" => time(),
  1272. "Random" => rand(1, 65535),
  1273. "MsgBody" => $msg_body_list
  1274. );
  1275. //构造MsgList
  1276. $msg_list = array();
  1277. array_push($msg_list, $msg_list_elem);
  1278. $ret = $this->group_import_group_msg2($group_id, $msg_list);
  1279. return $ret;
  1280. }
  1281. function group_import_group_msg2($group_id, $msg_list)
  1282. {
  1283. #构造新消息
  1284. $msg = array(
  1285. "GroupId" => $group_id,
  1286. "MsgList" => $msg_list,
  1287. );
  1288. var_dump($msg);
  1289. #将消息序列化为json串
  1290. $req_data = json_encode($msg);
  1291. $ret = $this->api("group_open_http_svc", "import_group_msg", $this->identifier, $this->usersig, $req_data);
  1292. $ret = json_decode($ret, true);
  1293. return $ret;
  1294. }
  1295. function group_set_unread_msg_num($group_id, $member_account, $unread_msg_num)
  1296. {
  1297. #构造新消息
  1298. $msg = array(
  1299. "GroupId" => $group_id,
  1300. "Member_Account" => $member_account,
  1301. "UnreadMsgNum" => (int)$unread_msg_num
  1302. );
  1303. #将消息序列化为json串
  1304. $req_data = json_encode($msg);
  1305. $ret = $this->api("group_open_http_svc", "set_unread_msg_num", $this->identifier, $this->usersig, $req_data);
  1306. $ret = json_decode($ret, true);
  1307. return $ret;
  1308. }
  1309. /**
  1310. * 添加黑名单
  1311. */
  1312. public function sns_black_import($account_id, $receiver)
  1313. {
  1314. #构造新消息
  1315. $msg = array(
  1316. 'From_Account' => $account_id,
  1317. 'To_Account' => array()
  1318. );
  1319. //$receiver_arr = array();
  1320. $msg['To_Account'][] = $receiver;
  1321. //array_push($msg['To_Account'], $receiver_arr);
  1322. #将消息序列化为json串
  1323. $req_data = json_encode($msg);
  1324. $ret = $this->api("sns", "black_list_add", $this->identifier, $this->usersig, $req_data);
  1325. $ret = json_decode($ret, true);
  1326. return $ret;
  1327. }
  1328. /**
  1329. * 删除黑名单
  1330. * @param unknown_type $account_id
  1331. * @param unknown_type $blk_id
  1332. * @return mixed
  1333. */
  1334. public function sns_black_delete($account_id, $blk_id)
  1335. {
  1336. #构造新消息
  1337. $msg = array(
  1338. 'From_Account' => $account_id,
  1339. 'To_Account' => array()
  1340. );
  1341. //$receiver_arr = array();
  1342. //$receiver_arr[] = $blk_id;
  1343. $msg['To_Account'][] = $blk_id;
  1344. //array_push($msg['To_Account'], $receiver_arr);
  1345. #将消息序列化为json串
  1346. $req_data = json_encode($msg);
  1347. $ret = $this->api("sns", "black_list_delete", $this->identifier, $this->usersig, $req_data);
  1348. $ret = json_decode($ret, true);
  1349. return $ret;
  1350. }
  1351. //批量发单聊消息
  1352. public function openim_batchsendmsg($account_id,$msg_content,$to){
  1353. #构造新消息
  1354. $msg = array(
  1355. "From_Account" => $account_id,
  1356. "MsgRandom" => rand(1, 65535), //消息随机数
  1357. "MsgBody" => $msg_content
  1358. );
  1359. #将消息序列化为json串
  1360. if($to){
  1361. $msg['To_Account'] = $to;//目标账户列表
  1362. }
  1363. $req_data = json_encode($msg);
  1364. $ret = $this->api("openim", "batchsendmsg", $this->identifier, $this->usersig, $req_data);
  1365. $ret = json_decode($ret, true);
  1366. return $ret;
  1367. }
  1368. /**
  1369. *
  1370. * @param string $account_id 消息推送方账号。
  1371. * @param MsgBody $msg_content 消息内容,具体格式请参考MsgBody消息内容说明。(一条消息可包括多种消息元素,所以MsgBody为Array类型)
  1372. * @param int $msg_life_time 消息离线存储时间,单位秒,最多7 天。默认为0, 表示不离线存储
  1373. * @return mixed
  1374. */
  1375. public function openim_push($account_id, $msg_content,$condition,$msg_life_time = 604800)
  1376. {
  1377. $account_id = (string)$account_id;
  1378. #构造新消息
  1379. $msg = array(
  1380. 'From_Account' => $account_id,
  1381. 'MsgRandom' => rand(1, 65535),
  1382. "MsgLifeTime"=> $msg_life_time,
  1383. 'MsgBody' => $msg_content,
  1384. );
  1385. #将消息序列化为json串
  1386. $req_data = json_encode($msg);
  1387. $ret = $this->api("openim", "im_push", $this->identifier, $this->usersig, $req_data);
  1388. $ret = json_decode($ret, true);
  1389. return $ret;
  1390. }
  1391. /**
  1392. * 查询APP自定义脏字
  1393. * @return
  1394. * {
  1395. "ActionStatus": "OK",
  1396. "ErrorInfo": "",
  1397. "ErrorCode": 0,
  1398. "DirtyWordsList": [
  1399. "回收金币",
  1400. "代刷人气",
  1401. "抽奖"
  1402. ]
  1403. }
  1404. */
  1405. public function openim_dirty_words_get()
  1406. {
  1407. #构造新消息
  1408. $msg = array();
  1409. #将消息序列化为json串
  1410. $req_data = json_encode($msg);
  1411. $ret = $this->api("openim_dirty_words", "get", $this->identifier, $this->usersig, $req_data);
  1412. $ret = json_decode($ret, true);
  1413. return $ret;
  1414. }
  1415. /**
  1416. * 添加APP自定义脏字
  1417. * @param unknown_type $dirty_words_list
  1418. * @return mixed
  1419. */
  1420. public function openim_dirty_words_add($dirty_words_list = array())
  1421. {
  1422. #构造新消息
  1423. $msg = array(
  1424. 'DirtyWordsList'=>$dirty_words_list,
  1425. );
  1426. #将消息序列化为json串
  1427. $req_data = json_encode($msg);
  1428. $ret = $this->api("openim_dirty_words", "add", $this->identifier, $this->usersig, $req_data);
  1429. $ret = json_decode($ret, true);
  1430. return $ret;
  1431. }
  1432. /**
  1433. * 删除APP自定义脏字
  1434. * @param unknown_type $dirty_words_list
  1435. * @return mixed
  1436. */
  1437. public function openim_dirty_words_delete($dirty_words_list = array())
  1438. {
  1439. #构造新消息
  1440. $msg = array(
  1441. 'DirtyWordsList'=>$dirty_words_list,
  1442. );
  1443. #将消息序列化为json串
  1444. $req_data = json_encode($msg);
  1445. $ret = $this->api("openim_dirty_words", "delete", $this->identifier, $this->usersig, $req_data);
  1446. $ret = json_decode($ret, true);
  1447. return $ret;
  1448. }
  1449. };
  1450. //辅助过滤器类
  1451. class Filter{};
  1452. /** Json数据格式化方法
  1453. * @param array $data 数组数据
  1454. * @param string $indent 缩进字符,默认4个空格
  1455. * @return sting json格式字符串
  1456. */
  1457. function json_format($data, $indent=null)
  1458. {
  1459. // 对数组中每个元素递归进行urlencode操作,保护中文字符
  1460. array_walk_recursive($data, 'json_format_protect');
  1461. // json encode
  1462. $data = json_encode($data);
  1463. // 将urlencode的内容进行urldecode
  1464. $data = urldecode($data);
  1465. // 缩进处理
  1466. $ret = '';
  1467. $pos = 0;
  1468. $length = strlen($data);
  1469. $indent = isset($indent)? $indent : ' ';
  1470. $newline = "\n";
  1471. $prevchar = '';
  1472. $outofquotes = true;
  1473. for($i=0; $i<=$length; $i++){
  1474. $char = substr($data, $i, 1);
  1475. if($char=='"' && $prevchar!='\\')
  1476. {
  1477. $outofquotes = !$outofquotes;
  1478. }elseif(($char=='}' || $char==']') && $outofquotes)
  1479. {
  1480. $ret .= $newline;
  1481. $pos --;
  1482. for($j=0; $j<$pos; $j++){
  1483. $ret .= $indent;
  1484. }
  1485. }
  1486. $ret .= $char;
  1487. if(($char==',' || $char=='{' || $char=='[') && $outofquotes)
  1488. {
  1489. $ret .= $newline;
  1490. if($char=='{' || $char=='['){
  1491. $pos ++;
  1492. }
  1493. for($j=0; $j<$pos; $j++){
  1494. $ret .= $indent;
  1495. }
  1496. }
  1497. $prevchar = $char;
  1498. }
  1499. return $ret;
  1500. }
  1501. /**
  1502. * json_formart辅助函数
  1503. * @param String $val 数组元素
  1504. */
  1505. function json_format_protect(&$val)
  1506. {
  1507. if($val!==true && $val!==false && $val!==null)
  1508. {
  1509. $val = urlencode($val);
  1510. }
  1511. }
  1512. /**
  1513. * 判断操作系统位数
  1514. */
  1515. function is_64bit() {
  1516. $int = "9223372036854775807";
  1517. $int = intval($int);
  1518. if ($int == 9223372036854775807) {
  1519. /* 64bit */
  1520. return true;
  1521. }
  1522. elseif ($int == 2147483647) {
  1523. /* 32bit */
  1524. return false;
  1525. }
  1526. else {
  1527. /* error */
  1528. return "error";
  1529. }
  1530. }