腾讯通信云服务端使用心得,腾讯云IM

腾讯通信服务端使用心得

 

1.腾讯通信服务入口并创建应用

  方便使用保留url地址 :   https://cloud.tencent.com/product/im

  注册账号腾讯云账号->通过审核->创建应用

       

  *  创建应用完成后点击应用配置,帐号体系集成(配置完毕之后就可以进行接口接入的准备工作了)

   

 

2.接口接入准备工作

  将应用配置中的基础配置写到独立文件中简单保存

  

 

   提取SdkAppId,管理员账号(APP管理账号)在项目中需要使用

   

 3. APP管理员帐号生成usersig

   3.1 下载tls后台api (其中有生成usersig的demo)

    下载地址:https://share.weiyun.com/2b3abe0e3f185c440cf455647455f661(腾讯云通信官方提供)

     

    

    解压tls_sig_api-windows-64.zip压缩文件

    

    

      

    1.将D:/tls_sig_api-windows-64/php/TLSSig.php文件放置自己项目中(生成usersig类文件

    2.将在第二部分接入准备工作中提到的验证方式文件解压后放置项目根目录

             

  3.2 PHP调用TLSSig工具类生成usersig

/**
    * 创建UserSig
    *
    * @param $Txy_identifier 管理员账号(APP管理账号)在第一部分账号体系中有提到
    */
    public function createUserSig($Txy_identifier='admin'){

        //调用TLSSig工具类并实例化
        $api = new \Org\TLSSig\TLSSig();

        $api->SetAppid(C("Txy_sdkappid"));

        //private_key账号体系中下载的私钥
        $private = file_get_contents('./keys/private_key');

        $api->SetPrivateKey($private);

        //public_key账号体系中下载的公钥
        $public = file_get_contents('./keys/public_key');

        $api->SetPublicKey($public);

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

    echo $this->createUserSig();

  到此处接口准备工作已准备完成

 

4.接口接入部分

 

  4.1 创建接口公共类   

    说明:公共类继承与thinkphp3.2.2 

<?php 

namespace Interface_1_1_6\Controller;
use Think\Controller;

/**
 * 腾讯云通信REST API
 * @author    chengyujia <chengyujia1228@163.com>
 */
class TxyController extends CommonController {
   
    /**
    * 创建UserSig
    * @param $username 用户账号
    */
    public function createUserSig($Txy_identifier){

        if(!$Txy_identifier){
            $Txy_identifier = C("Txy_identifier");
        }

        $api = new \Org\TLSSig\TLSSig();
        $api->SetAppid(C("Txy_sdkappid"));
        $private = file_get_contents('./keys/private_key');
        $api->SetPrivateKey($private);
        $public = file_get_contents('./keys/public_key');
        $api->SetPublicKey($public);
        $sig = $api->genSig($Txy_identifier);
        
        return $sig;
    }

    /**
    * 腾讯云通信公共接口     
    * @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=".C("Txy_identifier")."&sdkappid=".C("Txy_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");
    }

    
}
?>

 

 

 

   4.2 调用接口进行注册

    

     $options['Identifier'] = 'zhangsan';
        $options['IdentifierType'] = 3;
        $options['Password'] = '123456';

        //腾讯云注册账号
        $register_account_res = $this->interfaces($options,'registration_service/register_account_v1');

   

   要忙工作了先到这里,有问题请留言,小弟才疏学浅,有错的地方请大家提出,我给予改成

 

posted @ 2017-12-27 16:34  克洛克达尔丶  阅读(4669)  评论(2编辑  收藏  举报