PHP对接环信im

2021/11/8 14:09:53

本文主要是介绍PHP对接环信im,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class Chat
{
    protected $org_name = 'xxx';
    protected $app_name = 'demo';
    protected $grant_type = 'XXXX';
    protected $client_id = 'XXXX';
    protected $client_secret = 'XXXX';
    protected $RESTApi = 'http://a1.easemob.com/';
 
 
    //header token
    public function access_token(){
        session_start();
        $_SESSION['expires_in'] =  $_SESSION['expires_in'] ?? null; dd($_SESSION);
        if(time()< $_SESSION['expires_in']) $token= $_SESSION['token'];
        if(time()> $_SESSION['expires_in']) {
            $_SESSION['expires_in']=  $_SESSION['token'] = null;
            $data = [
                'grant_type' => $this->grant_type,
                'client_id' => $this->client_id,
                'client_secret' => $this->client_secret,
            ];
            $url = $this->RESTApi . $this->org_name . '/' . $this->app_name . '/token';
            $data = json_encode($data);
            $result = $this->httpsCurl($url, $data, 'POST');
            $result = json_decode($result, true);
 
            $_SESSION['expires_in'] = time() + $result['expires_in'];
            $_SESSION['token'] = $result['access_token'];
 
            $token =$_SESSION['token'];
        }
        return $token;
    }
 
    //注册用户
    public function registerUser()
    {
        $url = $this->RESTApi.$this->org_name.'/'.$this->app_name.'/users';
        $data = [
            'username'=>'11111',
            'password'=>'123456',
        ];
        $data = json_encode($data);
        $token = $this->access_token();
 
        $result =  $this->postCurlHeader($url,$token,$data);
        $result = json_decode($result,true);
        if(empty($result['path'])) return echoArr(0,'注册失败');
        return echoArr(1,'注册成功');
    }
 
    // 发送消息-文本
    public function seedMessage()
    {
        $url = $this->RESTApi.$this->org_name. '/' .$this->app_name.'/messages';
        $token = $this->access_token();
 
         $data = [
            'target_type'=>'users',
            'target'=>["1570845"],
            'msg'=>[
                'type'=>'txt',
                'msg'=>'testmessage',
            ],
            'from'=>'1570845',
        ];
         $data = json_encode($data);
        $result = $this->postCurlHeader($url,$token,$data); ;
        $result = json_decode($result,true);
        if(!empty($result['path']))  return echoArr(0,'发送失败');
 
        return echoArr(1,'注册成功');
    }
 
    /**
     *  curl post 请求 不带header
     *
     */
    public function httpsCurl($url,$data='',$method='GET')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if($method=='POST')
        {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data !='')
            {
                curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
            }
        }
 
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
 
    /**
     * curl post header
     * @param $url
     * @param $token
     * @param $data
     * @return bool|string
     */
    public function postCurlHeader($url,$token,$data)
     {
         $curl = curl_init();
 
         curl_setopt_array($curl, array(
             CURLOPT_URL => $url,
             CURLOPT_RETURNTRANSFER => true,
             CURLOPT_ENCODING => "",
             CURLOPT_MAXREDIRS => 10,
             CURLOPT_TIMEOUT => 0,
             CURLOPT_FOLLOWLOCATION => true,
             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
             CURLOPT_CUSTOMREQUEST => "POST",
             CURLOPT_POSTFIELDS =>  $data,
             CURLOPT_HTTPHEADER => array(
                 "Authorization: Bearer ".$token,
                 "Content-Type: application/json",
                 "Accept: application/json"
             ),
         ));
 
         $response = curl_exec($curl);
 
         curl_close($curl);
 
         return  $response;
     }
 
    public function mytestAction($url,$token,$postBody=[]){
//        $appkey='123';
//        $appSecret='456';
 
        $headers = array();
        $headers[] = "Authorization:Bearer ".$token;
        $headers[] = "Content-Type:application/json";
 
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
        curl_setopt($curl, CURLOPT_HTTPGET, true);
//        curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');//使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。(这个加不加没啥影响)
        $data = curl_exec($curl);
        return $data;
    }
 
}


这篇关于PHP对接环信im的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


原文链接: https://blog.csdn.net/weixin_45566227/article/details/121205890
扫一扫关注最新编程教程