腾讯云IM REST API V4接口统一请求方法 PHP
//腾讯云IM数据请求 identifier:标识体,method_name:方法名,data:请求消息体【json字符串】function txImPostParam($identifier,$method_name,$data){$appid=''; //腾讯云IM appid$user_sign=txImUserSign($identifier);$random=get_str(8);$ba
//腾讯云IM数据请求 identifier:标识体,method_name:方法名,data:请求消息体【json字符串】
function txImPostParam($identifier,$method_name,$data){
$appid=''; //腾讯云IM appid
$user_sign=txImUserSign($identifier);
$random=get_str(8);
$basic_url='https://console.tim.qq.com/v4/';
$url=$basic_url.$method_name;
$url.='?sdkappid='.$appid.'&identifier='.$identifier.'&usersig='.$user_sign.'&random='.$random.'&contenttype=json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$return_str = curl_exec($curl);
curl_close($curl);
return $return_str;
}
//腾讯云IMUserSign
function txImUserSign($id){
$sig='';
$appid='';
$appkey='';
// sdk 下载地址:https://cloud.tencent.com/document/product/269/32688
require_once API_ROOT.'/../sdk/tencentIM/TLSSigAPIv2.php';
$api = new \Tencent\TLSSigAPIv2($appid,$appkey);
$sign = $api->genUserSig($id);
return $sign;
}
//随机生成存数字字符串
function get_str($length){
$str = '0123456789';
$len = strlen($str)-1;
$randstr = '';
for ($i=0;$i<$length;$i++) {
$num=mt_rand(0,$len);
$randstr .= $str[$num];
}
return $randstr;
}
调用接口的方法:
$identifier=''; //管理员账号
$method_name='group_open_http_svc/get_group_info'; //请求接口方法名
//请求数据 不同的接口数据不同,需参考官方文档
$data=array(
"GroupIdList"=>[
$groupid
],
);
$data=json_encode($data);
//参考文档:https://cloud.tencent.com/document/product/269/1617
$response=txImPostParam($identifier,$method_name,$data);
$result=json_decode($response,true);
if($result && $result['ActionStatus']=='OK' && $result['ErrorCode']==0){
}
更多推荐
所有评论(0)