騰訊通信雲服務端使用心得
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');
要忙工作了先到這里,有問題請留言,小弟才疏學淺,有錯的地方請大家提出,我給予改成