PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ThinkPhp教程 > 正文

    Thinkphp结合网易云IM代码类分享

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:使用网易云IM做APP可以实现私聊、群聊、聊天室等通讯能力功能,网易云IM文档看起来比较复杂,很多新手不知道如何入手,今天B5教程网总结一...
    使用网易云IM做APP可以实现私聊、群聊、聊天室等通讯能力功能,网易云IM文档看起来比较复杂,很多新手不知道如何入手,今天B5教程网总结一下,分享给大家学习使用。

    1.封装的类如下:

    1. <?php 
    2. /** 
    3.  * Created by PhpStorm. 
    4.  * User: www.bcty365.com 
    5.  */ 
    6. namespace Org\Util; 
    7.  
    8. class YunxinServer{ 
    9.  
    10.     private $AppKey;                //开发者平台分配的AppKey 
    11.     private $AppSecret;             //开发者平台分配的AppSecret,可刷新 
    12.     private $Nonce;                 //随机数(最大长度128个字符) 
    13.     private $CurTime;               //当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String) 
    14.     private $CheckSum;              //SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写) 
    15.     const   HEX_DIGITS = "0123456789abcdef"
    16.  
    17.     /** 
    18.      * 参数初始化 
    19.      * @param $AppKey 
    20.      * @param $AppSecret 
    21.      * @param $RequestType [选择php请求方式,fsockopen或curl,若为curl方式,请检查php配置是否开启] 
    22.      */ 
    23.     public function __construct($AppKey,$AppSecret,$RequestType='curl'){ 
    24.         $this->AppKey    = $AppKey
    25.         $this->AppSecret = $AppSecret
    26.         $this->RequestType = $RequestType
    27.     } 
    28.  
    29.     /** 
    30.      * API checksum校验生成 
    31.      * @param  void 
    32.      * @return $CheckSum(对象私有属性) 
    33.      */ 
    34.     public function checkSumBuilder(){ 
    35.         //此部分生成随机字符串 
    36.         $hex_digits = self::HEX_DIGITS; 
    37.         $this->Nonce; 
    38.         for($i=0;$i<128;$i++){          //随机字符串最大128个字符,也可以小于该数 
    39.             $this->Nonce.= $hex_digits[rand(0,15)]; 
    40.         } 
    41.         $this->CurTime = (string)(time());  //当前时间戳,以秒为单位 
    42.  
    43.         $join_string = $this->AppSecret.$this->Nonce.$this->CurTime; 
    44.         $this->CheckSum = sha1($join_string); 
    45.         //print_r($this->CheckSum); 
    46.     } 
    47.  
    48.     /** 
    49.      * 将json字符串转化成php数组 
    50.      * @param  $json_str 
    51.      * @return $json_arr 
    52.      */ 
    53.     public function json_to_array($json_str){ 
    54.         // version 1.6 code ... 
    55.         // if(is_null(json_decode($json_str))){ 
    56.         //     $json_str = $json_str; 
    57.         // }else{ 
    58.         //     $json_str = json_decode($json_str); 
    59.         // } 
    60.         // $json_arr=array(); 
    61.         // //print_r($json_str); 
    62.         // foreach($json_str as $k=>$w){ 
    63.         //     if(is_object($w)){ 
    64.         //         $json_arr[$k]= $this->json_to_array($w); //判断类型是不是object 
    65.         //     }else if(is_array($w)){ 
    66.         //         $json_arr[$k]= $this->json_to_array($w); 
    67.         //     }else{ 
    68.         //         $json_arr[$k]= $w; 
    69.         //     } 
    70.         // } 
    71.         // return $json_arr; 
    72.  
    73.         if(is_array($json_str) || is_object($json_str)){ 
    74.             $json_str = $json_str
    75.         }else if(is_null(json_decode($json_str))){ 
    76.             $json_str = $json_str
    77.         }else
    78.             $json_str =  strval($json_str); 
    79.             $json_str = json_decode($json_str,true); 
    80.         } 
    81.         $json_arr=array(); 
    82.         foreach($json_str as $k=>$w){ 
    83.             if(is_object($w)){ 
    84.                 $json_arr[$k]= $this->json_to_array($w); //判断类型是不是object 
    85.             }else if(is_array($w)){ 
    86.                 $json_arr[$k]= $this->json_to_array($w); 
    87.             }else
    88.                 $json_arr[$k]= $w
    89.             } 
    90.         } 
    91.         return $json_arr
    92.     } 
    93.  
    94.     /** 
    95.      * 使用CURL方式发送post请求 
    96.      * @param  $url     [请求地址] 
    97.      * @param  $data    [array格式数据] 
    98.      * @return $请求返回结果(array) 
    99.      */ 
    100.     public function postDataCurl($url,$data){ 
    101.         $this->checkSumBuilder();       //发送请求前需先生成checkSum 
    102.  
    103.         $timeout = 5000; 
    104.         $http_header = array
    105.             'AppKey:'.$this->AppKey, 
    106.             'Nonce:'.$this->Nonce, 
    107.             'CurTime:'.$this->CurTime, 
    108.             'CheckSum:'.$this->CheckSum, 
    109.             'Content-Type:application/x-www-form-urlencoded;charset=utf-8' 
    110.         ); 
    111.         //print_r($http_header); 
    112.  
    113.         // $postdata = ''; 
    114.         $postdataArray = array(); 
    115.         foreach ($data as $key=>$value){ 
    116.             array_push($postdataArray$key.'='.urlencode($value)); 
    117.             // $postdata.= ($key.'='.urlencode($value).'&'); 
    118.         } 
    119.         $postdata = join('&'$postdataArray); 
    120.  
    121.         $ch = curl_init(); 
    122.         curl_setopt ($ch, CURLOPT_URL, $url); 
    123.         curl_setopt ($ch, CURLOPT_POST, 1); 
    124.         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
    125.         curl_setopt ($ch, CURLOPT_HEADER, false ); 
    126.         curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header); 
    127.         curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题 
    128.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    129.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    130.  
    131.         $result = curl_exec($ch); 
    132.         if (false === $result) { 
    133.             $result =  curl_errno($ch); 
    134.         } 
    135.         curl_close($ch); 
    136.         return $this->json_to_array($result) ; 
    137.     } 
    138.  
    139.     /** 
    140.      * 使用FSOCKOPEN方式发送post请求 
    141.      * @param  $url     [请求地址] 
    142.      * @param  $data    [array格式数据] 
    143.      * @return $请求返回结果(array) 
    144.      */ 
    145.     public function postDataFsockopen($url,$data){ 
    146.         $this->checkSumBuilder();       //发送请求前需先生成checkSum 
    147.  
    148.         // $postdata = ''; 
    149.         $postdataArray = array(); 
    150.         foreach ($data as $key=>$value){ 
    151.             array_push($postdataArray$key.'='.urlencode($value)); 
    152.             // $postdata.= ($key.'='.urlencode($value).'&'); 
    153.         } 
    154.         $postdata = join('&'$postdataArray); 
    155.         // building POST-request: 
    156.         $URL_Info=parse_url($url); 
    157.         if(!isset($URL_Info["port"])){ 
    158.             $URL_Info["port"]=80; 
    159.         } 
    160.         $request = ''
    161.         $request.="POST ".$URL_Info["path"]." HTTP/1.1\r\n"
    162.         $request.="Host:".$URL_Info["host"]."\r\n"
    163.         $request.="Content-type: application/x-www-form-urlencoded;charset=utf-8\r\n"
    164.         $request.="Content-length: ".strlen($postdata)."\r\n"
    165.         $request.="Connection: close\r\n"
    166.         $request.="AppKey: ".$this->AppKey."\r\n"
    167.         $request.="Nonce: ".$this->Nonce."\r\n"
    168.         $request.="CurTime: ".$this->CurTime."\r\n"
    169.         $request.="CheckSum: ".$this->CheckSum."\r\n"
    170.         $request.="\r\n"
    171.         $request.=$postdata."\r\n"
    172.  
    173.         // print_r($request); 
    174.         $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); 
    175.         fputs($fp$request); 
    176.         $result = ''
    177.         while(!feof($fp)) { 
    178.             $result .= fgets($fp, 128); 
    179.         } 
    180.         fclose($fp); 
    181.  
    182.         $str_s = strpos($result,'{'); 
    183.         $str_e = strrpos($result,'}'); 
    184.         $str = substr($result$str_s,$str_e-$str_s+1); 
    185.         return $this->json_to_array($str); 
    186.     } 
    187.  
    188.     /** 
    189.      * 使用CURL方式发送post请求(JSON类型) 
    190.      * @param  $url     [请求地址] 
    191.      * @param  $data    [array格式数据] 
    192.      * @return $请求返回结果(array) 
    193.      */ 
    194.     public function postJsonDataCurl($url,$data){ 
    195.         $this->checkSumBuilder();       //发送请求前需先生成checkSum 
    196.  
    197.         $timeout = 5000; 
    198.         $http_header = array
    199.             'AppKey:'.$this->AppKey, 
    200.             'Nonce:'.$this->Nonce, 
    201.             'CurTime:'.$this->CurTime, 
    202.             'CheckSum:'.$this->CheckSum, 
    203.             'Content-Type:application/json;charset=utf-8' 
    204.         ); 
    205.         //print_r($http_header); 
    206.  
    207.         $postdata = json_encode($data); 
    208.  
    209.         $ch = curl_init(); 
    210.         curl_setopt ($ch, CURLOPT_URL, $url); 
    211.         curl_setopt ($ch, CURLOPT_POST, 1); 
    212.         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
    213.         curl_setopt ($ch, CURLOPT_HEADER, false ); 
    214.         curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header); 
    215.         curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题 
    216.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    217.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    218.  
    219.         $result = curl_exec($ch); 
    220.         if (false === $result) { 
    221.             $result =  curl_errno($ch); 
    222.         } 
    223.         curl_close($ch); 
    224.         return $this->json_to_array($result) ; 
    225.     } 
    226.  
    227.     /** 
    228.      * 使用FSOCKOPEN方式发送post请求(json) 
    229.      * @param  $url     [请求地址] 
    230.      * @param  $data    [array格式数据] 
    231.      * @return $请求返回结果(array) 
    232.      */ 
    233.     public function postJsonDataFsockopen($url$data){ 
    234.         $this->checkSumBuilder();       //发送请求前需先生成checkSum 
    235.  
    236.         $postdata = json_encode($data); 
    237.  
    238.         // building POST-request: 
    239.         $URL_Info=parse_url($url); 
    240.         if(!isset($URL_Info["port"])){ 
    241.             $URL_Info["port"]=80; 
    242.         } 
    243.         $request = ''
    244.         $request.="POST ".$URL_Info["path"]." HTTP/1.1\r\n"
    245.         $request.="Host:".$URL_Info["host"]."\r\n"
    246.         $request.="Content-type: application/json;charset=utf-8\r\n"
    247.         $request.="Content-length: ".strlen($postdata)."\r\n"
    248.         $request.="Connection: close\r\n"
    249.         $request.="AppKey: ".$this->AppKey."\r\n"
    250.         $request.="Nonce: ".$this->Nonce."\r\n"
    251.         $request.="CurTime: ".$this->CurTime."\r\n"
    252.         $request.="CheckSum: ".$this->CheckSum."\r\n"
    253.         $request.="\r\n"
    254.         $request.=$postdata."\r\n"
    255.  
    256.         print_r($request); 
    257.         $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); 
    258.         fputs($fp$request); 
    259.         $result = ''
    260.         while(!feof($fp)) { 
    261.             $result .= fgets($fp, 128); 
    262.         } 
    263.         fclose($fp); 
    264.  
    265.         $str_s = strpos($result,'{'); 
    266.         $str_e = strrpos($result,'}'); 
    267.         $str = substr($result$str_s,$str_e-$str_s+1); 
    268.         return $this->json_to_array($str); 
    269.     } 
    270.  
    271.     /** 
    272.      * 创建云信ID 
    273.      * 1.第三方帐号导入到云信平台; 
    274.      * 2.注意accid,name长度以及考虑管理秘钥token 
    275.      * @param  $accid     [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    276.      * @param  $name      [云信ID昵称,最大长度64字节,用来PUSH推送时显示的昵称] 
    277.      * @param  $props     [json属性,第三方可选填,最大长度1024字节] 
    278.      * @param  $icon      [云信ID头像URL,第三方可选填,最大长度1024] 
    279.      * @param  $token     [云信ID可以指定登录token值,最大长度128字节,并更新,如果未指定,会自动生成token,并在创建成功后返回] 
    280.      * @return $result    [返回array数组对象] 
    281.      */ 
    282.     public function createUserIds($accid,$name='',$props='{}',$icon='',$token=''){ 
    283.         $url = 'https://api.netease.im/nimserver/user/create.action'
    284.         $dataarray
    285.             'accid' => $accid
    286.             'name'  => $name
    287.             'props' => $props
    288.             'icon'  => $icon
    289.             'token' => $token 
    290.         ); 
    291.         if($this->RequestType=='curl'){ 
    292.             $result = $this->postDataCurl($url,$data); 
    293.         }else
    294.             $result = $this->postDataFsockopen($url,$data); 
    295.         } 
    296.         return $result
    297.     } 
    298.  
    299.  
    300.     /** 
    301.      * 更新云信ID 
    302.      * @param  $accid     [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    303.      * @param  $name      [云信ID昵称,最大长度64字节,用来PUSH推送时显示的昵称] 
    304.      * @param  $props     [json属性,第三方可选填,最大长度1024字节] 
    305.      * @param  $token     [云信ID可以指定登录token值,最大长度128字节,并更新,如果未指定,会自动生成token,并在创建成功后返回] 
    306.      * @return $result    [返回array数组对象] 
    307.      */ 
    308.     public function updateUserId($accid,$name='',$props='{}',$token=''){ 
    309.         $url = 'https://api.netease.im/nimserver/user/update.action'
    310.         $dataarray
    311.             'accid' => $accid
    312.             'name'  => $name
    313.             'props' => $props
    314.             'token' => $token 
    315.         ); 
    316.         if($this->RequestType=='curl'){ 
    317.             $result = $this->postDataCurl($url,$data); 
    318.         }else
    319.             $result = $this->postDataFsockopen($url,$data); 
    320.         } 
    321.         return $result
    322.     } 
    323.  
    324.     /** 
    325.      * 更新并获取新token 
    326.      * @param  $accid     [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    327.      * @return $result    [返回array数组对象] 
    328.      */ 
    329.     public function updateUserToken($accid){ 
    330.         $url = 'https://api.netease.im/nimserver/user/refreshToken.action'
    331.         $dataarray
    332.             'accid' => $accid 
    333.         ); 
    334.         if($this->RequestType=='curl'){ 
    335.             $result = $this->postDataCurl($url,$data); 
    336.         }else
    337.             $result = $this->postDataFsockopen($url,$data); 
    338.         } 
    339.         return $result
    340.     } 
    341.  
    342.     /** 
    343.      * 封禁云信ID 
    344.      * 第三方禁用某个云信ID的IM功能,封禁云信ID后,此ID将不能登陆云信imserver 
    345.      * @param  $accid     [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    346.      * @return $result    [返回array数组对象] 
    347.      */ 
    348.     public function blockUserId($accid){ 
    349.         $url = 'https://api.netease.im/nimserver/user/block.action'
    350.         $dataarray
    351.             'accid' => $accid 
    352.         ); 
    353.         if($this->RequestType=='curl'){ 
    354.             $result = $this->postDataCurl($url,$data); 
    355.         }else
    356.             $result = $this->postDataFsockopen($url,$data); 
    357.         } 
    358.         return $result
    359.     } 
    360.  
    361.     /** 
    362.      * 解禁云信ID 
    363.      * 第三方禁用某个云信ID的IM功能,封禁云信ID后,此ID将不能登陆云信imserver 
    364.      * @param  $accid     [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    365.      * @return $result    [返回array数组对象] 
    366.      */ 
    367.     public function unblockUserId($accid){ 
    368.         $url = 'https://api.netease.im/nimserver/user/unblock.action'
    369.         $dataarray
    370.             'accid' => $accid 
    371.         ); 
    372.         if($this->RequestType=='curl'){ 
    373.             $result = $this->postDataCurl($url,$data); 
    374.         }else
    375.             $result = $this->postDataFsockopen($url,$data); 
    376.         } 
    377.         return $result
    378.     } 
    379.  
    380.  
    381.     /** 
    382.      * 更新用户名片 
    383.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    384.      * @param  $name        [云信ID昵称,最大长度64字节,用来PUSH推送时显示的昵称] 
    385.      * @param  $icon        [用户icon,最大长度256字节] 
    386.      * @param  $sign        [用户签名,最大长度256字节] 
    387.      * @param  $email       [用户email,最大长度64字节] 
    388.      * @param  $birth       [用户生日,最大长度16字节] 
    389.      * @param  $mobile      [用户mobile,最大长度32字节] 
    390.      * @param  $ex          [用户名片扩展字段,最大长度1024字节,用户可自行扩展,建议封装成JSON字符串] 
    391.      * @param  $gender      [用户性别,0表示未知,1表示男,2女表示女,其它会报参数错误] 
    392.      * @return $result      [返回array数组对象] 
    393.      */ 
    394.     public function updateUinfo($accid,$name='',$icon='',$sign='',$email='',$birth='',$mobile='',$gender='0',$ex=''){ 
    395.         $url = 'https://api.netease.im/nimserver/user/updateUinfo.action'
    396.         $dataarray
    397.             'accid' => $accid
    398.             'name' => $name
    399.             'icon' => $icon
    400.             'sign' => $sign
    401.             'email' => $email
    402.             'birth' => $birth
    403.             'mobile' => $mobile
    404.             'gender' => $gender
    405.             'ex' => $ex 
    406.         ); 
    407.         if($this->RequestType=='curl'){ 
    408.             $result = $this->postDataCurl($url,$data); 
    409.         }else
    410.             $result = $this->postDataFsockopen($url,$data); 
    411.         } 
    412.         return $result
    413.     } 
    414.  
    415.     /** 
    416.      * 获取用户名片,可批量 
    417.      * @param  $accids    [用户帐号(例如:JSONArray对应的accid串,如:"zhangsan",如果解析出错,会报414)(一次查询最多为200)] 
    418.      * @return $result    [返回array数组对象] 
    419.      */ 
    420.     public function getUinfoss($accids){ 
    421.         $url = 'https://api.netease.im/nimserver/user/getUinfos.action'
    422.         $dataarray
    423.             'accids' => json_encode($accids
    424.         ); 
    425.         if($this->RequestType=='curl'){ 
    426.             $result = $this->postDataCurl($url,$data); 
    427.         }else
    428.             $result = $this->postDataFsockopen($url,$data); 
    429.         } 
    430.         return $result
    431.     } 
    432.  
    433.     /** 
    434.      * 好友关系-加好友 
    435.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    436.      * @param  $faccid        [云信ID昵称,最大长度64字节,用来PUSH推送时显示的昵称] 
    437.      * @param  $type        [用户type,最大长度256字节] 
    438.      * @param  $msg        [用户签名,最大长度256字节] 
    439.      * @return $result      [返回array数组对象] 
    440.      */ 
    441.     public function addFriend($accid,$faccid,$type='1',$msg=''){ 
    442.         $url = 'https://api.netease.im/nimserver/friend/add.action'
    443.         $dataarray
    444.             'accid' => $accid
    445.             'faccid' => $faccid
    446.             'type' => $type
    447.             'msg' => $msg 
    448.         ); 
    449.         if($this->RequestType=='curl'){ 
    450.             $result = $this->postDataCurl($url,$data); 
    451.         }else
    452.             $result = $this->postDataFsockopen($url,$data); 
    453.         } 
    454.         return $result
    455.     } 
    456.  
    457.     /** 
    458.      * 好友关系-更新好友信息 
    459.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    460.      * @param  $faccid        [要修改朋友的accid] 
    461.      * @param  $alias        [给好友增加备注名] 
    462.      * @return $result      [返回array数组对象] 
    463.      */ 
    464.     public function updateFriend($accid,$faccid,$alias){ 
    465.         $url = 'https://api.netease.im/nimserver/friend/update.action'
    466.         $dataarray
    467.             'accid' => $accid
    468.             'faccid' => $faccid
    469.             'alias' => $alias 
    470.         ); 
    471.         if($this->RequestType=='curl'){ 
    472.             $result = $this->postDataCurl($url,$data); 
    473.         }else
    474.             $result = $this->postDataFsockopen($url,$data); 
    475.         } 
    476.         return $result
    477.     } 
    478.  
    479.     /** 
    480.      * 好友关系-获取好友关系 
    481.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    482.      * @return $result      [返回array数组对象] 
    483.      */ 
    484.     public function getFriend($accid){ 
    485.         $url = 'https://api.netease.im/nimserver/friend/get.action'
    486.         $dataarray
    487.             'accid' => $accid
    488.             'createtime' => (string)(time()*1000) 
    489.         ); 
    490.         if($this->RequestType=='curl'){ 
    491.             $result = $this->postDataCurl($url,$data); 
    492.         }else
    493.             $result = $this->postDataFsockopen($url,$data); 
    494.         } 
    495.         return $result
    496.     } 
    497.  
    498.     /** 
    499.      * 好友关系-删除好友信息 
    500.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    501.      * @param  $faccid        [要修改朋友的accid] 
    502.      * @return $result      [返回array数组对象] 
    503.      */ 
    504.     public function deleteFriend($accid,$faccid){ 
    505.         $url = 'https://api.netease.im/nimserver/friend/delete.action'
    506.         $dataarray
    507.             'accid' => $accid
    508.             'faccid' => $faccid 
    509.         ); 
    510.         if($this->RequestType=='curl'){ 
    511.             $result = $this->postDataCurl($url,$data); 
    512.         }else
    513.             $result = $this->postDataFsockopen($url,$data); 
    514.         } 
    515.         return $result
    516.     } 
    517.  
    518.     /** 
    519.      * 好友关系-设置黑名单 
    520.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    521.      * @param  $targetAcc        [被加黑或加静音的帐号] 
    522.      * @param  $relationType        [本次操作的关系类型,1:黑名单操作,2:静音列表操作] 
    523.      * @param  $value        [操作值,0:取消黑名单或静音;1:加入黑名单或静音] 
    524.      * @return $result      [返回array数组对象] 
    525.      */ 
    526.     public function specializeFriend($accid,$targetAcc,$relationType='1',$value='1'){ 
    527.         $url = 'https://api.netease.im/nimserver/user/setSpecialRelation.action'
    528.         $dataarray
    529.             'accid' => $accid
    530.             'targetAcc' => $targetAcc
    531.             'relationType' => $relationType
    532.             'value' => $value 
    533.         ); 
    534.         if($this->RequestType=='curl'){ 
    535.             $result = $this->postDataCurl($url,$data); 
    536.         }else
    537.             $result = $this->postDataFsockopen($url,$data); 
    538.         } 
    539.         return $result
    540.     } 
    541.  
    542.     /** 
    543.      * 好友关系-查看黑名单列表 
    544.      * @param  $accid       [云信ID,最大长度32字节,必须保证一个APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成,不区分大小写,会统一小写处理)] 
    545.      * @return $result      [返回array数组对象] 
    546.      */ 
    547.     public function listBlackFriend($accid){ 
    548.         $url = 'https://api.netease.im/nimserver/user/listBlackAndMuteList.action'
    549.         $dataarray
    550.             'accid' => $accid 
    551.         ); 
    552.         if($this->RequestType=='curl'){ 
    553.             $result = $this->postDataCurl($url,$data); 
    554.         }else
    555.             $result = $this->postDataFsockopen($url,$data); 
    556.         } 
    557.         return $result
    558.     } 
    559.  
    560.     /** 
    561.      * 消息功能-发送普通消息 
    562.      * @param  $from       [发送者accid,用户帐号,最大32字节,APP内唯一] 
    563.      * @param  $ope        [0:点对点个人消息,1:群消息,其他返回414] 
    564.      * @param  $to        [ope==0是表示accid,ope==1表示tid] 
    565.      * @param  $type        [0 表示文本消息,1 表示图片,2 表示语音,3 表示视频,4 表示地理位置信息,6 表示文件,100 自定义消息类型] 
    566.      * @param  $body       [请参考下方消息示例说明中对应消息的body字段。最大长度5000字节,为一个json字段。] 
    567.      * @param  $option       [发消息时特殊指定的行为选项,Json格式,可用于指定消息的漫游,存云端历史,发送方多端同步,推送,消息抄送等特殊行为;option中字段不填时表示默认值] 
    568.      * @param  $pushcontent      [推送内容,发送消息(文本消息除外,type=0),option选项中允许推送(push=true),此字段可以指定推送内容。 最长200字节] 
    569.      * @return $result      [返回array数组对象] 
    570.      */ 
    571.     public function sendMsg($from,$ope,$to,$type,$body,$option=array("push"=>false,"roam"=>true,"history"=>false,"sendersync"=>true, "route"=>false),$pushcontent=''){ 
    572.         $url = 'https://api.netease.im/nimserver/msg/sendMsg.action'
    573.         $dataarray
    574.             'from' => $from
    575.             'ope' => $ope
    576.             'to' => $to
    577.             'type' => $type
    578.             'body' => json_encode($body), 
    579.             'option' => json_encode($option), 
    580.             'pushcontent' => $pushcontent 
    581.         ); 
    582.         if($this->RequestType=='curl'){ 
    583.             $result = $this->postDataCurl($url,$data); 
    584.         }else
    585.             $result = $this->postDataFsockopen($url,$data); 
    586.         } 
    587.         return $result
    588.     } 
    589.  
    590.     /** 
    591.      * 消息功能-发送自定义系统消息 
    592.      * 1.自定义系统通知区别于普通消息,方便开发者进行业务逻辑的通知。 
    593.      * 2.目前支持两种类型:点对点类型和群类型(仅限高级群),根据msgType有所区别。 
    594.      * @param  $from       [发送者accid,用户帐号,最大32字节,APP内唯一] 
    595.      * @param  $msgtype        [0:点对点个人消息,1:群消息,其他返回414] 
    596.      * @param  $to        [msgtype==0是表示accid,msgtype==1表示tid] 
    597.      * @param  $attach        [自定义通知内容,第三方组装的字符串,建议是JSON串,最大长度1024字节] 
    598.      * @param  $pushcontent       [ios推送内容,第三方自己组装的推送内容,如果此属性为空串,自定义通知将不会有推送(pushcontent + payload不能超过200字节)] 
    599.      * @param  $payload       [ios 推送对应的payload,必须是JSON(pushcontent + payload不能超过200字节)] 
    600.      * @param  $sound      [如果有指定推送,此属性指定为客户端本地的声音文件名,长度不要超过30个字节,如果不指定,会使用默认声音] 
    601.      * @return $result      [返回array数组对象] 
    602.      */ 
    603.     public function sendAttachMsg($from,$msgtype,$to,$attach,$pushcontent='',$payload=array(),$sound=''){ 
    604.         $url = 'https://api.netease.im/nimserver/msg/sendAttachMsg.action'
    605.         $dataarray
    606.             'from' => $from
    607.             'msgtype' => $msgtype
    608.             'to' => $to
    609.             'attach' => $attach
    610.             'pushcontent' => $pushcontent
    611.             'payload' => json_encode($payload), 
    612.             'sound' => $sound 
    613.         ); 
    614.         if($this->RequestType=='curl'){ 
    615.             $result = $this->postDataCurl($url,$data); 
    616.         }else
    617.             $result = $this->postDataFsockopen($url,$data); 
    618.         } 
    619.         return $result
    620.     } 
    621.  
    622.     /** 
    623.      * 消息功能-文件上传 
    624.      * @param  $content       [字节流base64串(Base64.encode(bytes)) ,最大15M的字节流] 
    625.      * @param  $type        [上传文件类型] 
    626.      * @return $result      [返回array数组对象] 
    627.      */ 
    628.     public function uploadMsg($content,$type='0'){ 
    629.         $url = 'https://api.netease.im/nimserver/msg/upload.action'
    630.         $dataarray
    631.             'content' => $content
    632.             'type' => $type 
    633.         ); 
    634.         if($this->RequestType=='curl'){ 
    635.             $result = $this->postDataCurl($url,$data); 
    636.         }else
    637.             $result = $this->postDataFsockopen($url,$data); 
    638.         } 
    639.         return $result
    640.     } 
    641.  
    642.     /** 
    643.      * 消息功能-文件上传(multipart方式) 
    644.      * @param  $content       [字节流base64串(Base64.encode(bytes)) ,最大15M的字节流] 
    645.      * @param  $type        [上传文件类型] 
    646.      * @return $result      [返回array数组对象] 
    647.      */ 
    648.     public function uploadMultiMsg($content,$type='0'){ 
    649.         $url = 'https://api.netease.im/nimserver/msg/fileUpload.action'
    650.         $dataarray
    651.             'content' => $content
    652.             'type' => $type 
    653.         ); 
    654.         if($this->RequestType=='curl'){ 
    655.             $result = $this->postDataCurl($url,$data); 
    656.         }else
    657.             $result = $this->postDataFsockopen($url,$data); 
    658.         } 
    659.         return $result
    660.     } 
    661.  
    662.  
    663.     /** 
    664.      * 群组功能(高级群)-创建群 
    665.      * @param  $tname       [群名称,最大长度64字节] 
    666.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    667.      * @param  $members     [["aaa","bbb"](JsonArray对应的accid,如果解析出错会报414),长度最大1024字节] 
    668.      * @param  $announcement [群公告,最大长度1024字节] 
    669.      * @param  $intro       [群描述,最大长度512字节] 
    670.      * @param  $msg       [邀请发送的文字,最大长度150字节] 
    671.      * @param  $magree      [管理后台建群时,0不需要被邀请人同意加入群,1需要被邀请人同意才可以加入群。其它会返回414。] 
    672.      * @param  $joinmode    [群建好后,sdk操作时,0不用验证,1需要验证,2不允许任何人加入。其它返回414] 
    673.      * @param  $custom      [自定义高级群扩展属性,第三方可以跟据此属性自定义扩展自己的群属性。(建议为json),最大长度1024字节.] 
    674.      * @return $result      [返回array数组对象] 
    675.      */ 
    676.     public function createGroup($tname,$owner,$members,$announcement='',$intro='',$msg='',$magree='0',$joinmode='0',$custom='0'){ 
    677.         $url = 'https://api.netease.im/nimserver/team/create.action'
    678.         $dataarray
    679.             'tname' => $tname
    680.             'owner' => $owner
    681.             'members' => json_encode($members), 
    682.             'announcement' => $announcement
    683.             'intro' => $intro
    684.             'msg' => $msg
    685.             'magree' => $magree
    686.             'joinmode' => $joinmode
    687.             'custom' => $custom 
    688.         ); 
    689.         if($this->RequestType=='curl'){ 
    690.             $result = $this->postDataCurl($url,$data); 
    691.         }else
    692.             $result = $this->postDataFsockopen($url,$data); 
    693.         } 
    694.         return $result
    695.     } 
    696.  
    697.     /** 
    698.      * 群组功能(高级群)-拉人入群 
    699.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    700.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    701.      * @param  $members     [["aaa","bbb"](JsonArray对应的accid,如果解析出错会报414),长度最大1024字节] 
    702.      * @param  $magree      [管理后台建群时,0不需要被邀请人同意加入群,1需要被邀请人同意才可以加入群。其它会返回414。] 
    703.      * @param  $joinmode    [群建好后,sdk操作时,0不用验证,1需要验证,2不允许任何人加入。其它返回414] 
    704.      * @param  $custom      [自定义高级群扩展属性,第三方可以跟据此属性自定义扩展自己的群属性。(建议为json),最大长度1024字节.] 
    705.      * @return $result      [返回array数组对象] 
    706.      */ 
    707.     public function addIntoGroup($tid,$owner,$members,$magree='0',$msg='请您入伙'){ 
    708.         $url = 'https://api.netease.im/nimserver/team/add.action'
    709.         $dataarray
    710.             'tid' => $tid
    711.             'owner' => $owner
    712.             'members' => json_encode($members), 
    713.             'magree' => $magree
    714.             'msg' => $msg 
    715.         ); 
    716.         if($this->RequestType=='curl'){ 
    717.             $result = $this->postDataCurl($url,$data); 
    718.         }else
    719.             $result = $this->postDataFsockopen($url,$data); 
    720.         } 
    721.         return $result
    722.     } 
    723.  
    724.     /** 
    725.      * 群组功能(高级群)-踢人出群 
    726.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    727.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    728.      * @param  $member     [被移除人得accid,用户账号,最大长度字节] 
    729.      * @return $result      [返回array数组对象] 
    730.      */ 
    731.     public function kickFromGroup($tid,$owner,$member){ 
    732.         $url = 'https://api.netease.im/nimserver/team/kick.action'
    733.         $dataarray
    734.             'tid' => $tid
    735.             'owner' => $owner
    736.             'member' => $member 
    737.         ); 
    738.         if($this->RequestType=='curl'){ 
    739.             $result = $this->postDataCurl($url,$data); 
    740.         }else
    741.             $result = $this->postDataFsockopen($url,$data); 
    742.         } 
    743.         return $result
    744.     } 
    745.  
    746.     /** 
    747.      * 群组功能(高级群)-解散群 
    748.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    749.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    750.      * @return $result      [返回array数组对象] 
    751.      */ 
    752.     public function removeGroup($tid,$owner){ 
    753.         $url = 'https://api.netease.im/nimserver/team/remove.action'
    754.         $dataarray
    755.             'tid' => $tid
    756.             'owner' => $owner 
    757.         ); 
    758.         if($this->RequestType=='curl'){ 
    759.             $result = $this->postDataCurl($url,$data); 
    760.         }else
    761.             $result = $this->postDataFsockopen($url,$data); 
    762.         } 
    763.         return $result
    764.     } 
    765.  
    766.     /** 
    767.      * 群组功能(高级群)-更新群资料 
    768.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    769.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    770.      * @param  $tname     [群主用户帐号,最大长度32字节] 
    771.      * @param  $announcement [群公告,最大长度1024字节] 
    772.      * @param  $intro       [群描述,最大长度512字节] 
    773.      * @param  $joinmode    [群建好后,sdk操作时,0不用验证,1需要验证,2不允许任何人加入。其它返回414] 
    774.      * @param  $custom      [自定义高级群扩展属性,第三方可以跟据此属性自定义扩展自己的群属性。(建议为json),最大长度1024字节.] 
    775.      * @return $result      [返回array数组对象] 
    776.      */ 
    777.     public function updateGroup($tid,$owner,$tname,$announcement='',$intro='',$joinmode='0',$custom=''){ 
    778.         $url = 'https://api.netease.im/nimserver/team/update.action'
    779.         $dataarray
    780.             'tid' => $tid
    781.             'owner' => $owner
    782.             'tname' => $tname
    783.             'announcement' => $announcement
    784.             'intro' => $intro
    785.             'joinmode' => $joinmode
    786.             'custom' => $custom 
    787.         ); 
    788.         if($this->RequestType=='curl'){ 
    789.             $result = $this->postDataCurl($url,$data); 
    790.         }else
    791.             $result = $this->postDataFsockopen($url,$data); 
    792.         } 
    793.         return $result
    794.     } 
    795.  
    796.     /** 
    797.      * 群组功能(高级群)-群信息与成员列表查询 
    798.      * @param  $tids       [群tid列表,如[\"3083\",\"3084"]] 
    799.      * @param  $ope       [1表示带上群成员列表,0表示不带群成员列表,只返回群信息] 
    800.      * @return $result      [返回array数组对象] 
    801.      */ 
    802.     public function queryGroup($tids,$ope='1'){ 
    803.         $url = 'https://api.netease.im/nimserver/team/query.action'
    804.         $dataarray
    805.             'tids' => json_encode($tids), 
    806.             'ope' => $ope 
    807.         ); 
    808.         if($this->RequestType=='curl'){ 
    809.             $result = $this->postDataCurl($url,$data); 
    810.         }else
    811.             $result = $this->postDataFsockopen($url,$data); 
    812.         } 
    813.         return $result
    814.     } 
    815.  
    816.     /** 
    817.      * 群组功能(高级群)-移交群主 
    818.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    819.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    820.      * @param  $newowner     [新群主帐号,最大长度32字节] 
    821.      * @param  $leave       [1:群主解除群主后离开群,2:群主解除群主后成为普通成员。其它414] 
    822.      * @return $result      [返回array数组对象] 
    823.      */ 
    824.     public function changeGroupOwner($tid,$owner,$newowner,$leave='2'){ 
    825.         $url = 'https://api.netease.im/nimserver/team/changeOwner.action'
    826.         $dataarray
    827.             'tid' => $tid
    828.             'owner' => $owner
    829.             'newowner' => $newowner
    830.             'leave' => $leave 
    831.         ); 
    832.         if($this->RequestType=='curl'){ 
    833.             $result = $this->postDataCurl($url,$data); 
    834.         }else
    835.             $result = $this->postDataFsockopen($url,$data); 
    836.         } 
    837.         return $result
    838.     } 
    839.  
    840.     /** 
    841.      * 群组功能(高级群)-任命管理员 
    842.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    843.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    844.      * @param  $members     [["aaa","bbb"](JsonArray对应的accid,如果解析出错会报414),长度最大1024字节(群成员最多10个)] 
    845.      * @return $result      [返回array数组对象] 
    846.      */ 
    847.     public function addGroupManager($tid,$owner,$members){ 
    848.         $url = 'https://api.netease.im/nimserver/team/addManager.action'
    849.         $dataarray
    850.             'tid' => $tid
    851.             'owner' => $owner
    852.             'members' => json_encode($members
    853.         ); 
    854.         if($this->RequestType=='curl'){ 
    855.             $result = $this->postDataCurl($url,$data); 
    856.         }else
    857.             $result = $this->postDataFsockopen($url,$data); 
    858.         } 
    859.         return $result
    860.     } 
    861.  
    862.     /** 
    863.      * 群组功能(高级群)-移除管理员 
    864.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    865.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    866.      * @param  $members     [["aaa","bbb"](JsonArray对应的accid,如果解析出错会报414),长度最大1024字节(群成员最多10个)] 
    867.      * @return $result      [返回array数组对象] 
    868.      */ 
    869.     public function removeGroupManager($tid,$owner,$members){ 
    870.         $url = 'https://api.netease.im/nimserver/team/removeManager.action'
    871.         $dataarray
    872.             'tid' => $tid
    873.             'owner' => $owner
    874.             'members' => json_encode($members
    875.         ); 
    876.         if($this->RequestType=='curl'){ 
    877.             $result = $this->postDataCurl($url,$data); 
    878.         }else
    879.             $result = $this->postDataFsockopen($url,$data); 
    880.         } 
    881.         return $result
    882.     } 
    883.  
    884.     /** 
    885.      * 群组功能(高级群)-获取某用户所加入的群信息 
    886.      * @param  $accid       [要查询用户的accid] 
    887.      * @return $result      [返回array数组对象] 
    888.      */ 
    889.     public function joinTeams($accid){ 
    890.         $url = 'https://api.netease.im/nimserver/team/joinTeams.action'
    891.         $dataarray
    892.             'accid' => $accid 
    893.         ); 
    894.         if($this->RequestType=='curl'){ 
    895.             $result = $this->postDataCurl($url,$data); 
    896.         }else
    897.             $result = $this->postDataFsockopen($url,$data); 
    898.         } 
    899.         return $result
    900.     } 
    901.  
    902.  
    903.     /** 
    904.      * 群组功能(高级群)-修改群昵称 
    905.      * @param  $tid       [云信服务器产生,群唯一标识,创建群时会返回,最大长度128字节] 
    906.      * @param  $owner       [群主用户帐号,最大长度32字节] 
    907.      * @param  $accid     [要修改群昵称对应群成员的accid] 
    908.      * @param  $nick     [accid对应的群昵称,最大长度32字节。] 
    909.      * @return $result      [返回array数组对象] 
    910.      */ 
    911.     public function updateGroupNick($tid,$owner,$accid,$nick){ 
    912.         $url = 'https://api.netease.im/nimserver/team/updateTeamNick.action'
    913.         $dataarray
    914.             'tid' => $tid
    915.             'owner' => $owner
    916.             'accid' => $accid
    917.             'nick' => $nick 
    918.         ); 
    919.         if($this->RequestType=='curl'){ 
    920.             $result = $this->postDataCurl($url,$data); 
    921.         }else
    922.             $result = $this->postDataFsockopen($url,$data); 
    923.         } 
    924.         return $result
    925.     } 
    926.  
    927.     /** 
    928.      * 历史记录-单聊 
    929.      * @param  $from       [发送者accid] 
    930.      * @param  $to          [接收者accid] 
    931.      * @param  $begintime     [开始时间,ms] 
    932.      * @param  $endtime     [截止时间,ms] 
    933.      * @param  $limit       [本次查询的消息条数上限(最多100条),小于等于0,或者大于100,会提示参数错误] 
    934.      * @param  $reverse    [1按时间正序排列,2按时间降序排列。其它返回参数414.默认是按降序排列。] 
    935.      * @return $result      [返回array数组对象] 
    936.      */ 
    937.     public function querySessionMsg($from,$to,$begintime,$endtime='',$limit='100',$reverse='1'){ 
    938.         $url = 'https://api.netease.im/nimserver/history/querySessionMsg.action'
    939.         $dataarray
    940.             'from' => $from
    941.             'to' => $to
    942.             'begintime' => $begintime
    943.             'endtime' => $endtime
    944.             'limit' => $limit
    945.             'reverse' => $reverse 
    946.         ); 
    947.         if($this->RequestType=='curl'){ 
    948.             $result = $this->postDataCurl($url,$data); 
    949.         }else
    950.             $result = $this->postDataFsockopen($url,$data); 
    951.         } 
    952.         return $result
    953.     } 
    954.  
    955.     /** 
    956.      * 历史记录-群聊 
    957.      * @param  $tid       [群id] 
    958.      * @param  $accid          [查询用户对应的accid.] 
    959.      * @param  $begintime     [开始时间,ms] 
    960.      * @param  $endtime     [截止时间,ms] 
    961.      * @param  $limit       [本次查询的消息条数上限(最多100条),小于等于0,或者大于100,会提示参数错误] 
    962.      * @param  $reverse    [1按时间正序排列,2按时间降序排列。其它返回参数414.默认是按降序排列。] 
    963.      * @return $result      [返回array数组对象] 
    964.      */ 
    965.     public function queryGroupMsg($tid,$accid,$begintime,$endtime='',$limit='100',$reverse='1'){ 
    966.         $url = 'https://api.netease.im/nimserver/history/queryTeamMsg.action'
    967.         $dataarray
    968.             'tid' => $tid
    969.             'accid' => $accid
    970.             'begintime' => $begintime
    971.             'endtime' => $endtime
    972.             'limit' => $limit
    973.             'reverse' => $reverse 
    974.         ); 
    975.         if($this->RequestType=='curl'){ 
    976.             $result = $this->postDataCurl($url,$data); 
    977.         }else
    978.             $result = $this->postDataFsockopen($url,$data); 
    979.         } 
    980.         return $result
    981.     } 
    982.  
    983.     /** 
    984.      * 发送短信验证码 
    985.      * @param  $mobile       [目标手机号] 
    986.      * @param  $deviceId     [目标设备号,可选参数] 
    987.      * @return $result      [返回array数组对象] 
    988.      */ 
    989.     public function sendSmsCode($mobile,$deviceId=''){ 
    990.         $url = 'https://api.netease.im/sms/sendcode.action'
    991.         $dataarray
    992.             'mobile' => $mobile
    993.             'deviceId' => $deviceId 
    994.         ); 
    995.         if($this->RequestType=='curl'){ 
    996.             $result = $this->postDataCurl($url,$data); 
    997.         }else
    998.             $result = $this->postDataFsockopen($url,$data); 
    999.         } 
    1000.         return $result
    1001.     } 
    1002.  
    1003.     /** 
    1004.      * 校验验证码 
    1005.      * @param  $mobile       [目标手机号] 
    1006.      * @param  $code          [验证码] 
    1007.      * @return $result      [返回array数组对象] 
    1008.      */ 
    1009.     public function verifycode($mobile,$code=''){ 
    1010.         $url = 'https://api.netease.im/sms/verifycode.action'
    1011.         $dataarray
    1012.             'mobile' => $mobile
    1013.             'code' => $code 
    1014.         ); 
    1015.         if($this->RequestType=='curl'){ 
    1016.             $result = $this->postDataCurl($url,$data); 
    1017.         }else
    1018.             $result = $this->postDataFsockopen($url,$data); 
    1019.         } 
    1020.         return $result
    1021.     } 
    1022.  
    1023.     /** 
    1024.      * 发送模板短信 
    1025.      * @param  $templateid       [模板编号(由客服配置之后告知开发者)] 
    1026.      * @param  $mobiles          [验证码] 
    1027.      * @param  $params          [短信参数列表,用于依次填充模板,JSONArray格式,如["xxx","yyy"];对于不包含变量的模板,不填此参数表示模板即短信全文内容] 
    1028.      * @return $result      [返回array数组对象] 
    1029.      */ 
    1030.     public function sendSMSTemplate($templateid,$mobiles=array(),$params=array()){ 
    1031.         $url = 'https://api.netease.im/sms/sendtemplate.action'
    1032.         $dataarray
    1033.             'templateid' => $templateid
    1034.             'mobiles' => json_encode($mobiles), 
    1035.             'params' => json_encode($params
    1036.         ); 
    1037.         if($this->RequestType=='curl'){ 
    1038.             $result = $this->postDataCurl($url,$data); 
    1039.         }else
    1040.             $result = $this->postDataFsockopen($url,$data); 
    1041.         } 
    1042.         return $result
    1043.     } 
    1044.  
    1045.     /** 
    1046.      * 查询模板短信发送状态 
    1047.      * @param  $sendid       [发送短信的编号sendid] 
    1048.      * @return $result      [返回array数组对象] 
    1049.      */ 
    1050.     public function querySMSStatus($sendid){ 
    1051.         $url = 'https://api.netease.im/sms/querystatus.action'
    1052.         $dataarray
    1053.             'sendid' => $sendid 
    1054.         ); 
    1055.         if($this->RequestType=='curl'){ 
    1056.             $result = $this->postDataCurl($url,$data); 
    1057.         }else
    1058.             $result = $this->postDataFsockopen($url,$data); 
    1059.         } 
    1060.         return $result
    1061.     } 
    1062.  
    1063.     /** 
    1064.      * 发起单人专线电话 
    1065.      * @param  $callerAcc       [发起本次请求的用户的accid] 
    1066.      * @param  $caller          [主叫方电话号码(不带+86这类国家码,下同)] 
    1067.      * @param  $callee          [被叫方电话号码] 
    1068.      * @param  $maxDur          [本通电话最大可持续时长,单位秒,超过该时长时通话会自动切断] 
    1069.      * @return $result      [返回array数组对象] 
    1070.      */ 
    1071.     public function startcall($callerAcc,$caller,$callee,$maxDur='60'){ 
    1072.         $url = 'https://api.netease.im/call/ecp/startcall.action'
    1073.         $dataarray
    1074.             'callerAcc' => $callerAcc
    1075.             'caller' => $caller
    1076.             'callee' => $callee
    1077.             'maxDur' => $maxDur 
    1078.         ); 
    1079.         if($this->RequestType=='curl'){ 
    1080.             $result = $this->postDataCurl($url,$data); 
    1081.         }else
    1082.             $result = $this->postDataFsockopen($url,$data); 
    1083.         } 
    1084.         return $result
    1085.     } 
    1086.  
    1087.     /** 
    1088.      * 发起专线会议电话 
    1089.      * @param  $callerAcc       [发起本次请求的用户的accid] 
    1090.      * @param  $caller          [主叫方电话号码(不带+86这类国家码,下同)] 
    1091.      * @param  $callee          [所有被叫方电话号码,必须是json格式的字符串,如["13588888888","13699999999"]] 
    1092.      * @param  $maxDur          [本通电话最大可持续时长,单位秒,超过该时长时通话会自动切断] 
    1093.      * @return $result      [返回array数组对象] 
    1094.      */ 
    1095.     public function startconf($callerAcc,$caller,$callee,$maxDur='60'){ 
    1096.         $url = 'https://api.netease.im/call/ecp/startconf.action'
    1097.         $dataarray
    1098.             'callerAcc' => $callerAcc
    1099.             'caller' => $caller
    1100.             'callee' => json_encode($callee), 
    1101.             'maxDur' => $maxDur 
    1102.         ); 
    1103.         if($this->RequestType=='curl'){ 
    1104.             $result = $this->postDataCurl($url,$data); 
    1105.         }else
    1106.             $result = $this->postDataFsockopen($url,$data); 
    1107.         } 
    1108.         return $result
    1109.     } 
    1110.  
    1111.     /** 
    1112.      * 查询单通专线电话或会议的详情 
    1113.      * @param  $session       [本次通话的id号] 
    1114.      * @param  $type          [通话类型,1:专线电话;2:专线会议] 
    1115.      * @return $result      [返回array数组对象] 
    1116.      */ 
    1117.     public function queryCallsBySession($session,$type){ 
    1118.         $url = 'https://api.netease.im/call/ecp/queryBySession.action'
    1119.         $dataarray
    1120.             'session' => $session
    1121.             'type' => $type 
    1122.         ); 
    1123.         if($this->RequestType=='curl'){ 
    1124.             $result = $this->postDataCurl($url,$data); 
    1125.         }else
    1126.             $result = $this->postDataFsockopen($url,$data); 
    1127.         } 
    1128.         return $result
    1129.     } 
    1130.  
    1131.     /* 2016-06-15 新增php调用直播接口示例 */ 
    1132.  
    1133.     /** 
    1134.      * 获取语音视频安全认证签名 
    1135.      * @param  $uid       [用户帐号唯一标识,必须是Long型] 
    1136.      */ 
    1137.     public function getUserSignature($uid){ 
    1138.         $url = 'https://api.netease.im/nimserver/user/getToken.action'
    1139.         $dataarray
    1140.             'uid' => $uid 
    1141.         ); 
    1142.         if($this->RequestType=='curl'){ 
    1143.             $result = $this->postDataCurl($url,$data); 
    1144.         }else
    1145.             $result = $this->postDataFsockopen($url,$data); 
    1146.         } 
    1147.         return $result
    1148.     } 
    1149.  
    1150.     /** 
    1151.      * 创建一个直播频道 
    1152.      * @param  $name       [频道名称, string] 
    1153.      * @param  $type       [频道类型(0:rtmp;1:hls;2:http)] 
    1154.      */ 
    1155.     public function channelCreate($name,$type){ 
    1156.         $url = 'https://vcloud.163.com/app/channel/create'
    1157.         $dataarray
    1158.             'name' => $name
    1159.             'type' => $type 
    1160.         ); 
    1161.         if($this->RequestType=='curl'){ 
    1162.             $result = $this->postJsonDataCurl($url,$data); 
    1163.         }else
    1164.             $result = $this->postJsonDataFsockopen($url,$data); 
    1165.         } 
    1166.         return $result
    1167.     } 
    1168.  
    1169.     /** 
    1170.      * 修改直播频道信息 
    1171.      * @param  $name       [频道名称, string] 
    1172.      * @param  $cid       [频道ID,32位字符串] 
    1173.      * @param  $type       [频道类型(0:rtmp;1:hls;2:http)] 
    1174.      */ 
    1175.     public function channelUpdate($name$cid$type){ 
    1176.         $url = 'https://vcloud.163.com/app/channel/update'
    1177.         $dataarray
    1178.             'name' => $name
    1179.             'cid' => $cid
    1180.             'type' => $type 
    1181.         ); 
    1182.         if($this->RequestType=='curl'){ 
    1183.             $result = $this->postJsonDataCurl($url,$data); 
    1184.         }else
    1185.             $result = $this->postJsonDataFsockopen($url,$data); 
    1186.         } 
    1187.         return $result
    1188.     } 
    1189.  
    1190.     /** 
    1191.      * 删除一个直播频道 
    1192.      * @param  $cid       [频道ID,32位字符串] 
    1193.      */ 
    1194.     public function channelDelete($cid){ 
    1195.         $url = 'https://vcloud.163.com/app/channel/delete'
    1196.         $dataarray
    1197.             'cid' => $cid 
    1198.         ); 
    1199.         if($this->RequestType=='curl'){ 
    1200.             $result = $this->postJsonDataCurl($url,$data); 
    1201.         }else
    1202.             $result = $this->postJsonDataFsockopen($url,$data); 
    1203.         } 
    1204.         return $result
    1205.     } 
    1206.  
    1207.     /** 
    1208.      * 获取一个直播频道的信息 
    1209.      * @param  $cid       [频道ID,32位字符串] 
    1210.      */ 
    1211.     public function channelStats($cid){ 
    1212.         $url = 'https://vcloud.163.com/app/channelstats'
    1213.         $dataarray
    1214.             'cid' => $cid 
    1215.         ); 
    1216.         if($this->RequestType=='curl'){ 
    1217.             $result = $this->postJsonDataCurl($url,$data); 
    1218.         }else
    1219.             $result = $this->postJsonDataFsockopen($url,$data); 
    1220.         } 
    1221.         return $result
    1222.     } 
    1223.  
    1224.     /** 
    1225.      * 获取用户直播频道列表 
    1226.      * @param  $records       [单页记录数,默认值为10] 
    1227.      * @param  $pnum = 1       [要取第几页,默认值为1] 
    1228.      * @param  $ofield       [排序的域,支持的排序域为:ctime(默认)] 
    1229.      * @param  $sort            [升序还是降序,1升序,0降序,默认为desc] 
    1230.      */ 
    1231.     public function channelList($records = 10, $pnum = 1, $ofield = 'ctime'$sort = 0){ 
    1232.         $url = 'https://vcloud.163.com/app/channellist'
    1233.         $dataarray
    1234.             'records' => $records
    1235.             'pnum' => $pnum
    1236.             'ofield' => $ofield
    1237.             'sort' => $sort 
    1238.         ); 
    1239.         if($this->RequestType=='curl'){ 
    1240.             $result = $this->postJsonDataCurl($url,$data); 
    1241.         }else
    1242.             $result = $this->postJsonDataFsockopen($url,$data); 
    1243.         } 
    1244.         return $result
    1245.     } 
    1246.  
    1247.     /** 
    1248.      * 重新获取推流地址 
    1249.      * @param  $cid       [频道ID,32位字符串] 
    1250.      */ 
    1251.     public function channelRefreshAddr($cid){ 
    1252.         $url = 'https://vcloud.163.com/app/address'
    1253.         $dataarray
    1254.             'cid' => $cid 
    1255.         ); 
    1256.         if($this->RequestType=='curl'){ 
    1257.             $result = $this->postJsonDataCurl($url,$data); 
    1258.         }else
    1259.             $result = $this->postJsonDataFsockopen($url,$data); 
    1260.         } 
    1261.         return $result
    1262.     } 
    1263.  
    1264.     // 2015-07-04 聊天室功能开发 gm 
    1265.  
    1266.     /** 
    1267.      * 创建聊天室 
    1268.      * @param $accid 聊天室属主的账号accid 
    1269.      * @param $name  聊天室名称,长度限制128个字符 
    1270.      */ 
    1271.     public function chatroomCreates($accid,$name){ 
    1272.         $url = 'https://api.netease.im/nimserver/chatroom/create.action'
    1273.         $data = array
    1274.             'creator'=>$accid
    1275.             'name'=>$name 
    1276.         ); 
    1277.         if($this->RequestType=='curl'){ 
    1278.             $result = $this->postDataCurl($url,$data); 
    1279.         }else
    1280.             $result = $this->postDataFsockopen($url,$data); 
    1281.         } 
    1282.         return $result
    1283.     } 
    1284.  
    1285.     /** 
    1286.      * 更新聊天室 
    1287.      * @param $roomid  聊天室ID 
    1288.      * @param $name    聊天室名称 
    1289.      * @return array 
    1290.      */ 
    1291.     public function chatroomUpdates($roomid,$name){ 
    1292.         $url = 'https://api.netease.im/nimserver/chatroom/update.action'
    1293.         $data = array
    1294.             'roomid'=>$roomid
    1295.             'name'=>$name 
    1296.         ); 
    1297.         if($this->RequestType=='curl'){ 
    1298.             $result = $this->postDataCurl($url,$data); 
    1299.         }else
    1300.             $result = $this->postDataFsockopen($url,$data); 
    1301.         } 
    1302.         return $result
    1303.     } 
    1304.  
    1305.     /** 
    1306.      * 修改聊天室开启或关闭聊天室 
    1307.      * @param $roomid        聊天室ID 
    1308.      * @param $operator      创建者ID 
    1309.      * @param string $status 修改还是关闭  false => 关闭 
    1310.      */ 
    1311.     public function chatroomToggleCloses($roomid,$operator
    1312.     { 
    1313.         $url = 'https://api.netease.im/nimserver/chatroom/toggleCloseStat.action'
    1314.         $data = array
    1315.             'roomid'=>$roomid
    1316.             'operator'=>$operator
    1317.             'valid'=>'false' 
    1318.         ); 
    1319.         if($this->RequestType=='curl'){ 
    1320.             $result = $this->postDataCurl($url,$data); 
    1321.         }else
    1322.             $result = $this->postDataFsockopen($url,$data); 
    1323.         } 
    1324.         return $result
    1325.     } 
    1326.  
    1327.     public function chatroomToggleStats($roomid,$operator
    1328.     { 
    1329.         $url = 'https://api.netease.im/nimserver/chatroom/toggleCloseStat.action'
    1330.         $data = array
    1331.             'roomid'=>$roomid
    1332.             'operator'=>$operator
    1333.             'valid'=>'true' 
    1334.         ); 
    1335.         if($this->RequestType=='curl'){ 
    1336.             $result = $this->postDataCurl($url,$data); 
    1337.         }else
    1338.             $result = $this->postDataFsockopen($url,$data); 
    1339.         } 
    1340.         return $result
    1341.     } 
    1342.  
    1343.     /** 
    1344.      *设置聊天室内用户角色 
    1345.      * @param $roomid            // 聊天室ID 
    1346.      * @param $operator          // 操作者账号accid   operator必须是创建者 
    1347.      * @param $target            // 被操作者账号accid 
    1348.      * @param $opt 
    1349.      *          1: 设置为管理员,operator必须是创建者 
    1350.                 2:设置普通等级用户,operator必须是创建者或管理员 
    1351.                 -1:设为黑名单用户,operator必须是创建者或管理员 
    1352.                 -2:设为禁言用户,operator必须是创建者或管理员 
    1353.      * @param string $optvalue   // true:设置;false:取消设置 
    1354.      */ 
    1355.     public function chatroomSetMemberRoles($roomid,$operator,$target,$opt,$optvalue
    1356.     { 
    1357.         $url = 'https://api.netease.im/nimserver/chatroom/setMemberRole.action'
    1358.         $data = array
    1359.             'roomid'=>$roomid
    1360.             'operator'=>$operator
    1361.             'target'=>$target
    1362.             'opt'=>$opt
    1363.             'optvalue'=>$optvalue
    1364.         ); 
    1365.         if($this->RequestType=='curl'){ 
    1366.             $result = $this->postDataCurl($url,$data); 
    1367.         }else
    1368.             $result = $this->postDataFsockopen($url,$data); 
    1369.         } 
    1370.         return $result
    1371.     } 
    1372.  
    1373.     // 获取聊天室的信息 
    1374.     public function chatroomgets($roomid
    1375.     { 
    1376.         $url = 'https://api.netease.im/nimserver/chatroom/get.action'
    1377.         $data = array
    1378.             'roomid'=>$roomid
    1379.             'needOnlineUserCount'=>'true' 
    1380.         ); 
    1381.         if($this->RequestType=='curl'){ 
    1382.             $result = $this->postDataCurl($url,$data); 
    1383.         }else
    1384.             $result = $this->postDataFsockopen($url,$data); 
    1385.         } 
    1386.         return $result
    1387.  
    1388.     } 
    1389.  
    1390.  
    1391.  
    1392.  

    2.控制器代码

     

    1. <?php 
    2. /** 
    3.  * Created by PhpStorm. 
    4.  * User: www.bcty365.com 
    5.  */ 
    6. namespace Api\Controller; 
    7. use Think\Controller; 
    8.  
    9. class YunxinController extends Controller{ 
    10.  
    11.     private $model
    12.     private $AppKey = '';     // key 
    13.     private $AppSecret = '';  // secret 
    14.  
    15.     public function _initialize(){ 
    16.         // 实例云信的库 
    17.         $this->model = new \Org\Util\YunxinServer($this->AppKey,$this->AppSecret,'curl'); 
    18.     } 
    19.  
    20.     /** 
    21.      * 创建云信ID 
    22.      *  
    23.     */ 
    24.     public function createUserId($userid
    25.     { 
    26.         $data = M('user')->field('id accid,username,nickname name,headimgurl icon')->where('is_del = 0 AND id='.$userid)->find(); 
    27.         $token = md5($data['accid'].'abc'); 
    28.         // 写入到云信服务器 
    29.         $accid = 'abc'.$data['accid']; 
    30.         $name = $data['name']; 
    31.         $icon = $data['icon']; 
    32.         $info = $this->model->createUserIds($accid,$name,'{}',$icon,$token); 
    33.         return $info
    34.     } 
    35.  
    36.     public function updateUinfos($accid,$name,$icon,$sign,$email,$birth,$mobile,$gender,$ex){ 
    37.         $info = $this->model->updateUinfo($accid,$name,$icon,$sign,$email,$birth,$mobile,$gender,$ex); 
    38.         return $info
    39.     } 
    40.  
    41.     // 获取指定用户的云信ID 
    42.     public function getUinfos(){ 
    43.         $accid = I('request.id'); 
    44.         $accid = array($accid); 
    45.         $info = $this->model->getUinfoss($accid); 
    46.         print_r($info); 
    47.         return $info
    48.     } 
    49.  
    50.     // 创建聊天室 
    51.     /** 
    52.      * @param $accid 聊天室的ID 
    53.      * @param $name  聊天室的名称 
    54.      */ 
    55.     public function chatroomCreate($accid,$name){ 
    56.         $info = $this->model->chatroomCreates($accid,$name); 
    57.         return $info
    58.     } 
    59.  
    60.     // 查询聊天室 
    61.     public function chatroomget(){ 
    62.         $roomid = I('request.id'); 
    63.         $info = $this->model->chatroomgets($roomid); 
    64.         print_r($info); 
    65.         return $info
    66.     } 
    67.  
    68.     /** 
    69.      * 更新聊天室 
    70.      * @param $roomid  聊天室ID 
    71.      * @param $name    聊天室名称 
    72.      * @return array 
    73.      */ 
    74.     public function chatroomUpdate($roomid,$name){ 
    75.         $info = $this->model->chatroomUpdates($roomid,$name); 
    76.         return $info
    77.     } 
    78.  
    79.     /** 
    80.      * 修改聊天室开启或关闭聊天室 
    81.      * @param $roomid        聊天室ID 
    82.      * @param $operator      创建者ID 
    83.      * @param string $status 修改还是关闭  false => 修改 
    84.      */ 
    85.     public function chatroomToggleClose($roomid,$operator){ 
    86.         $info = $this->model->chatroomToggleCloses($roomid,$operator); 
    87.         return $info
    88.     } 
    89.  
    90.     public function chatroomToggleStat($roomid,$operator){ 
    91.         $info = $this->model->chatroomToggleStats($roomid,$operator); 
    92.         return $info
    93.     } 
    94.  
    95.     /** 
    96.      *设置聊天室内用户角色 
    97.      * @param $roomid            // 聊天室ID 
    98.      * @param $operator          // 操作者账号accid   operator必须是创建者 
    99.      * @param $target            // 被操作者账号accid 
    100.      * @param $opt 
    101.      *      1: 设置为管理员,operator必须是创建者 
    102.             2:设置普通等级用户,operator必须是创建者或管理员 
    103.             -1:设为黑名单用户,operator必须是创建者或管理员 
    104.             -2:设为禁言用户,operator必须是创建者或管理员 
    105.      * @param string $optvalue   // true:设置;false:取消设置 
    106.      */ 
    107.     public function chatroomSetMemberRole($roomid,$operator,$target,$opt,$optvalue){ 
    108.         $info = $this->model->chatroomSetMemberRoles($roomid,$operator,$target,$opt,$optvalue); 
    109.         return $info
    110.     } 
    111.  
    112.     /** 
    113.      * 云信消息抄送接口 
    114.      */ 
    115.     public function receiveMsg() 
    116.     { 
    117.         $body = @file_get_contents('php://input'); 
    118.         $data = json_decode($body,true); 
    119.         //file_put_contents('/data/server/work_justeasy_cn/debug.txt',$body); 
    120.         if($data){ 
    121.             $d['eventType']       = $data['eventType']; 
    122.             $d['attach']          = $data['attach']; 
    123.             $d['ext']             = $data['ext']; 
    124.             $d['fromAccount']     = $data['fromAccount']; 
    125.             $d['fromAvator']      = $data['fromAvator']; 
    126.             $d['fromClientType']  = $data['fromClientType']; 
    127.             $d['fromExt']         = $data['fromExt']; 
    128.             $d['fromNick']        = $data['fromNick']; 
    129.             $d['msgTimestamp']    = $data['msgTimestamp']; 
    130.             $d['msgType']         = $data['msgType']; 
    131.             $d['msgidClient']     = $data['msgidClient']; 
    132.             $d['resendFlag']      = $data['resendFlag']; 
    133.             $d['roleInfoTimetag'] = $data['roleInfoTimetag']; 
    134.             $d['roomId']          = $data['roomId']; 
    135.             $d['antispam']        = $data['antispam']; 
    136.             $info = M('receivemsg')->add($d); 
    137.             if($info){ 
    138.                 echo 200; 
    139.             } 
    140.         }else
    141.             echo 500; 
    142.         } 
    143.     } 
    144.  
    145.  

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-83-5949-1.html
    相关热词搜索: Thinkphp 网易云IM