1. 程式人生 > 實用技巧 >騰訊雲即時通訊IM 公共整合

騰訊雲即時通訊IM 公共整合

近日做了一個即時通訊相關的社交專案。

該專案集成了騰訊雲的即時通訊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號