当前位置:首页 > Web开发 > 正文

腾讯云即时通信 IM 处事端 SDK for PHP

2024-03-31 Web开发

标签:

使用本扩展前需要登录 即时通信 IM 控制台 创建应用,配置打点员、获取 app_id、Key 等关键信息

更多请检察并熟读 即时通信 IM 处事端API , REST API 接口列表

一 腾讯云IM API(tp5凡是放在extend目录下)

<?php
namespace tencentyun\im;
/**

腾讯IM API
*/
class im{
private $sdkappid; // 创建应用时即时通信 IM 控制台分配的 SDKAppID
private $identifier; // 必需为 App 打点员帐号
private $key; // 私钥
private $usersig; // 签名
private $random; // 随机数
private $postfix; // url参数
public function __construct($sdkappid,$identifier,$key) {
$this->sdkappid = $sdkappid;
$this->identifier = $identifier;
$this->key = $key;
$this->usersig = $this->genSig($identifier);
$this->random = (int)$this->nonce_str();
$this->postfix = ‘?sdkappid=‘.$this->sdkappid.‘&identifier=‘.$this->identifier.‘&usersig=‘.$this->usersig.‘&random=‘.$this->random.‘&contenttype=json‘;
}
/**

用于 url 的 base64 encode

‘+‘ => ‘*‘, ‘/‘ => ‘-‘, ‘=‘ => ‘_‘

@param string $string 需要编码的数据

@return string 编码后的base64串,掉败返回false

@throws \Exception
/
private function base64_url_encode($string) {
static $replace = Array(‘+‘ => ‘‘, ‘/‘ => ‘-‘, ‘=‘ => ‘_‘);
$base64 = base64_encode($string);
if ($base64 === false) {
throw new \Exception(‘base64_encode error‘);
}
return str_replace(array_keys($replace), array_values($replace), $base64);
}
/**

用于 url 的 base64 decode

‘+‘ => ‘*‘, ‘/‘ => ‘-‘, ‘=‘ => ‘_‘

@param string $base64 需要解码的base64串

@return string 解码后的数据,掉败返回false

@throws \Exception
/
private function base64_url_decode($base64) {
static $replace = Array(‘+‘ => ‘‘, ‘/‘ => ‘-‘, ‘=‘ => ‘_‘);
$string = str_replace(array_values($replace), array_keys($replace), $base64);
$result = base64_decode($string);
if ($result == false) {
throw new \Exception(‘base64_url_decode error‘);
}
return $result;
}
/**

使用 hmac sha256 生成 sig 字段内容,颠末 base64 编码

@param $identifier 用户名,utf-8 编码

@param $curr_time 当前生成 sig 的 unix 时间戳

@param $expire 有效期,单位秒

@param $base64_userbuf base64 编码后的 userbuf

@param $userbuf_enabled 是否开启 userbuf

@return string base64 后的 sig
*/
private function hmacsha256($identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled) {
$content_to_be_signed = "TLS.identifier:" . $identifier . "\n"
. "TLS.sdkappid:" . $this->sdkappid . "\n"
. "TLS.time:" . $curr_time . "\n"
. "TLS.expire:" . $expire . "\n";
if (true == $userbuf_enabled) {
$content_to_be_signed .= "TLS.userbuf:" . $base64_userbuf . "\n";
}
return base64_encode(hash_hmac( ‘sha256‘, $content_to_be_signed, $this->key, true));
}
/**

生成签名。

@param $identifier 用户账号

@param int $expire 过期时间,单位秒,默认 180 天

@param $userbuf base64 编码后的 userbuf

@param $userbuf_enabled 是否开启 userbuf

@return string 签名字符串

@throws \Exception
*/
private function __genSig($identifier, $expire, $userbuf, $userbuf_enabled) {
$curr_time = time();
$sig_array = Array(
‘TLS.ver‘ => ‘2.0‘,
‘TLS.identifier‘ => strval($identifier),
‘TLS.sdkappid‘ => intval($this->sdkappid),
‘TLS.expire‘ => intval($expire),
‘TLS.time‘ => intval($curr_time)
);
$base64_userbuf = ‘‘;
if (true == $userbuf_enabled) {
$base64_userbuf = base64_encode($userbuf);
$sig_array[‘TLS.userbuf‘] = strval($base64_userbuf);
}
$sig_array[‘TLS.sig‘] = $this->hmacsha256($identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled);
if ($sig_array[‘TLS.sig‘] === false) {
throw new \Exception(‘base64_encode error‘);
}
$json_str_sig = json_encode($sig_array);
if ($json_str_sig === false) {
throw new \Exception(‘json_encode error‘);
}
$compressed = gzcompress($json_str_sig);
if ($compressed === false) {
throw new \Exception(‘gzcompress error‘);
}
return $this->base64_url_encode($compressed);
}
/**

生成签名

@param $identifier 用户账号

@param int $expire 过期时间,单位秒,默认 180 天

@return string 签名字符串

@throws \Exception
/
public function genSig($identifier, $expire=6060*24) {
return $this->__genSig($identifier, $expire, ‘‘, false);
}
/**

带 userbuf 生成签名。

@param $identifier 用户账号

@param int $expire 过期时间,单位秒,,默认 180 天

@param string $userbuf 用户数据

@return string 签名字符串

@throws \Exception
*/
public function genSigWithUserBuf($identifier, $expire, $userbuf) {
return $this->__genSig($identifier, $expire, $userbuf, true);
}
/**

验证签名。

@param string $sig 签名内容

@param string $identifier 需要验证用户名,utf-8 编码

@param int $init_time 返回的生成时间,unix 时间戳

@param int $expire_time 返回的有效期,单位秒

@param string $userbuf 返回的用户数据

@param string $error_msg 掉败时的错误信息

@return boolean 验证是否告成

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31980.html