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/';
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,'注册成功');
}
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;
}
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=[]){
$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_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
$data = curl_exec($curl);
return $data;
}
}
|