腾讯云API接口封装

密钥和依赖类直接官网下载,主要展示PhpServer.php封装类,

<?php
require_once 'TLSSig.php';
class PhpServer
{

    private $sdkappid = '140****74';//sdkId
    private $identifier  = 'P**r';//账户名称
    /**
     * 创建UserSig
     * @param $username 用户账号
     */
    public function createUserSig($Txy_identifier=null){

        if(!$Txy_identifier){
            $Txy_identifier = $this->identifier;
        }
        $api = new TLSSigAPI();
        $api->SetAppid($this->sdkappid);
		
        $private = file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'keys/private_key');

        $api->SetPrivateKey($private);
        $public = file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'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=".$this->identifier."&sdkappid=".$this->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, $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");
    }

调用 :

$api = new PhpServer();
$options['Identifier'] = $users ['member_id']; 
$options['Nick'] = $users ['nickname']; 
$options['FaceUrl'] = $users ['face']; 
$ret = $api->interfaces($options,'im_open_login_svc/account_import');

猜你喜欢

转载自blog.csdn.net/zclwjy/article/details/81699806