YFYQ

近日做了一个即时通讯相关的社交项目。

该项目集成了腾讯云的即时通讯IM。

这其中借鉴了 克洛克达尔丶的博客,地址:https://www.cnblogs.com/chenggege/p/8126475.html

因为大大用的文档可能比较早了一些,现在有些改动,

对应的config 配置相关

    #腾讯云即时通讯IM相关配置
    \'tencentim\' => [
        \'sdkappid\'   => \'#####\',  #应用 sdkappid 
        \'key\'        => \'#####\',  #应用秘钥
        \'identifier\' => \'#####\',  #应用管理员
    ],

 

生成UserSig 接口可使用:腾讯云应用生成 UserSig

然后就是调用封装的对应类库咯代码附上:

<?php

#腾讯云IM 相关
#20200810
#11:25

namespace app\index\controller;

use think\Controller;
use tencent\TLSSigAPIv2;

class Tencent extends Controller
{
    /**
    * 创建UserSig
    * @param $username 用户账号
    */
    public function createUserSig($identifier){

        if(!$identifier){
            $identifier = config(\'txyconfig.identifier\');
        }

        $key = config(\'txyconfig.key\');
        $sdkappid = config(\'txyconfig.sdkappid\');
        $api = new TLSSigAPIv2($sdkappid,$key);

        $sig = $api->genSig($identifier); 
        return $sig;
    }

    #测试使用
    #拉取资料接口
    #示例:https://console.tim.qq.com/v4/profile/portrait_get?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
    public function getuserinfo()
    {
        $Interface = \'profile/portrait_get\';

        $options = [
            \'To_Account\' => [\'65986725\'],
            \'TagList\' => [
                \'Tag_Profile_IM_Nick\'
            ]
        ];
        $return = $this->interfaces($options,$Interface);
    }

    /**
    * 腾讯云通信公共接口     
    * @param array $options[\'Nick\'] 昵称
    * @param array $options[\'FaceUrl\'] 头像url
    * @param str $Interface 腾讯接口地址例如(registration_service/register_account_v1)    
    */
    public function interfaces($options,$Interface)
    {

        $usersig = $this->createUserSig();

        $optionStr = "usersig=".$usersig."&identifier=".config(\'txyconfig.identifier\')."&sdkappid=".config("txyconfig.sdkappid")."&random=".$this->returnRandom()."&contenttype=json";
        
        $url = "https://console.tim.qq.com/v4/".$Interface."?".$optionStr;
      
        $result = $this->postCurl ( $url, $options);
        
        $info = json_decode($result,true);
        $info[\'usersig\'] = $usersig;

        return $info;
    }

    /**
     * CURL Post发送数据
     *
     * @param $url 地址
     * @param $option 参数数据
     * @param $header 消息头
     * @param $type 发送方式
     */
    private function postCurl($url, $option, $header = 0, $type = \'POST\') {
        $curl = curl_init (); // 启动一个CURL会话
        curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl, CURLOPT_USERAGENT, \'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)\' ); // 模拟用户使用的浏览器
        if (! empty ( $option )) {
            $options = json_encode ( $option );
            curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
        }
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
        curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $type );
        $result = curl_exec ( $curl ); // 执行操作
        
        curl_close ( $curl ); // 关闭CURL会话
        return $result;
    }

    /**
    * 返回随机数 
    */
    public function returnRandom(){
        return rand("111111111","999999999");
    }


    

}

 

代码主体还是使用的原来大佬的相关数据

不过大佬使用的是TP3.2  修改了一部分代码,改为TP5使用

以上即是本篇内容

 

2020年08月10号

 

分类:

技术点:

相关文章: