Files

248 lines
7.7 KiB
PHP

<?php
namespace aodianyun;
class TisApi{
private $accessId;
private $host = "http://api.dms.aodianyun.com:80";
private $accessKey;
private $s_key;
public function __construct($accessId,$accessKey,$s_key) {
$this->accessKey = $accessKey;
$this->accessId = $accessId;
$this->s_key = $s_key;
}
public function Sign($str){
$signature = $this->accessId.":".base64_encode(hash_hmac("sha1", $str, $this->accessKey, true));
return $signature;
}
/**
* 获取历史消息
* @param $param
* @param $instId
* @return array|mixed
*/
public function history($param,$instId){
if(empty($instId)){
return array(
'Flag'=>101,
'FlagString'=>'参数错误'
);
}
$skip = 0;
$num = 100;
if(!empty($param['skip'])){
$skip=$param['skip'];
}
if(!empty($param['num'])){
$num=$param['num'];
}
return $this->curl("/v1/tis/words/".$instId."/history?skip=".$skip."&num=".$num);
}
/**
* 发送消息
* @param $param
* @param $instId
* @return array|mixed
*/
public function sendMsg($param,$instId){
if(empty($param['content'])){
return array(
'Flag'=>101,
'FlagString'=>'参数错误'
);
}
return $this->curl("/v1/tis/words/".$instId."/auto","POST",
json_encode(array('content' => $param['content'])));
}
/**
* 创建实例
* @param $param
*
* {
"dmsSecKey":"464", //dms服务中的s_key
"auditEnable":0, //可选,默认为0。是否开启消息审核。
"filterType":1, //可选,默认为0。当开启审核时起作用。0:不过滤,1:包含关键字的需要审核,3:包含关键字的直接替换为*
"filterKeys":"shit", //可选,默认为""
"description":"实力1", //可选,默认为"",描述
"groupIds":[20,30] //可选,需要关联的表情组的Id,该表情组必须是已经存在的。
//表情组Id可以在控制台--TIS服务--表情管理--选择一个表情组--查看表情--浏览器地址上的groupId参数中获取
}
* @return array|mixed
*/
/**
* 创建实例
* @param $dmsSecKey dms服务中的s_key
* @param $auditEnable 可选,默认为0。是否开启消息审核。
* @param $filterType 可选,默认为0。当开启审核时起作用。0:不过滤,1:包含关键字的需要审核,3:包含关键字的直接替换为*
* @param $filterKeys 可选,默认为""
* @param $description 可选,默认为"",描述
* @param $groupIds 可选,需要关联的表情组的Id,该表情组必须是已经存在的。
* @return array|mixed
*/
public function instAuto($auditEnable=0,$filterType=0,$filterKeys='',$description='',$groupIds=[]){
$param = [
'dmsSecKey'=>$this->s_key,
// 'auditEnable'=>$auditEnable,
// 'filterType'=>$filterType,
// 'filterKeys'=>$filterKeys,
// 'description'=>$description
];
if($groupIds){
$param['groupIds'] = $groupIds;
}
return $this->curl("/v1/tis/inst/auto","POST",json_encode($param));
}
/**
* 获取表情
* 根据{groupId}和{skip}、{num}获取表情信息
* @param $param
* @return array|mixed
*/
public function getFacesByGroup($param){
if(empty($param['groupId'])){
return array(
'Flag'=>101,
'FlagString'=>'参数错误'
);
}
$skip = 0;
$num = 100;
if(!empty($param['skip'])){
$skip=$param['skip'];
}
if(!empty($param['num'])){
$num=$param['num'];
}
return $this->curl("/v1/tis/faces?groupId=".$param['groupId']."&skip=".$skip."&num=".$num);
}
/**
* 获取分组
* @param $param skip,num:默认为skip=0,num=100
* @param $instId instId: TIS实例Id,如果没有这个参数,则查询用户下分组,而非实例下的分组
* @return array|mixed
*/
public function getFaceGroups($param,$instId){
$skip = 0;
$num = 100;
if(empty($instId)){
return array(
'Flag'=>101,
'FlagString'=>'参数错误'
);
}
if(!empty($param['skip'])){
$skip=$param['skip'];
}
if(!empty($param['num'])){
$num=$param['num'];
}
return $this->curl("/v1/tis/faceGroups?skip=".$skip."&num=".$num."&instId=".$instId);
}
/**
* 获取实例信息
* @param $param
* @param $instId
* @return array|mixed
*/
public function getTisInst($instId){
if(empty($instId) ){
return array(
'Flag'=>101,
'FlagString'=>'参数错误'
);
}
return $this->curl("/v1/tis/inst/".$instId);
}
/**
* 获取表情包信息
* @param $param
* @return array|mixed
*/
public function getPackage($param){
if(empty($param['pkgId']) ){
return array(
'Flag'=>101,
'FlagString'=>'参数错误'
);
}
return $this->curl("/v1/tis/packages/".$param['pkgId']);
}
private function curl($path,$method="GET",$content="",$expire=3600,$timeout=60){
$ch = curl_init();
$expire = time() + $expire;
if(($method == "POST" || $method == "PUT") && !empty($content)){
$contentMd5 = md5($content);
$str = $method."\n".$path."\n".$expire."\n".$contentMd5."\n";
}else{
$str = $method."\n".$path."\n".$expire."\n";
}
$query_url = $this->host.$path;
curl_setopt($ch,CURLOPT_URL,$query_url);
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
// curl_setopt($ch,CURLOPT_USERAGENT,$useragent);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
if($method == 'POST') {
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$content);
} else if($method == 'PUT') {
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$content);
}
$authorization = 'Authorization: tis '.$this->Sign($str);
//print_r($authorization);exit();
$header = array($authorization,'AD-Expire: '.$expire,'Content-Type: application/json');
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
//execute post
$response = curl_exec($ch);
//get response code
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//close connection
curl_close($ch);
//return result
if($response_code == 200 || $response_code == 201 || $response_code == 204) {
$rst = json_decode(trim($response),true);
} else {
$rr = json_decode(trim($response),true);
if($rr) {
$rst = $rr;
} else {
$rst = array(
'Flag'=>$response_code,
'FlagString'=>trim($response)
);
}
}
return $rst;
}
}
?>