PHP教程

PHP对接环信im

本文主要是介绍PHP对接环信im,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!