|
@@ -0,0 +1,166 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\api\controller;
|
|
|
+
|
|
|
+class Huanxin
|
|
|
+{
|
|
|
+ protected $org_name = '1122220822129603';
|
|
|
+ protected $app_name = 'demo';
|
|
|
+ protected $grant_type = 'client_7goxi0';
|
|
|
+ protected $client_id = 'YXA6HR9VT2UGQRW8gEAofXh6Lg';
|
|
|
+ protected $client_secret = 'YXA6ZWNqeGKEiVNqjHH8qKLHQDCJQr4';
|
|
|
+ protected $RESTApi = 'https://api.cn1.mqtt.chat/app/7goxi0';
|
|
|
+
|
|
|
+
|
|
|
+ //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);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送消息-文本
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|