腾讯云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);

           $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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值