PHP code example of zegoim / server_assistant

1. Go to this page and download the library: Download zegoim/server_assistant library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zegoim / server_assistant example snippets





use ZEGO\ZegoServerAssistant;
use ZEGO\ZegoErrorCodes;

$appId = 1;
$userId = 'demo';
$secret = 'fa94dd0f974cf2e293728a526b028271';
$payload = '';
$token = ZegoServerAssistant::generateToken04($appId,$userId,$secret,3600,$payload);
if( $token->code == ZegoErrorCodes::success ){
  print_r($token->token);
}


use ZEGO\ZegoServerAssistant;
use ZEGO\ZegoErrorCodes;

//权限位定义
const PrivilegeKeyLogin   = 1; // 登录
const PrivilegeKeyPublish = 2; // 推流

//权限开关定义
const PrivilegeEnable     = 1; // 开启
const PrivilegeDisable    = 0; // 关闭

$appId = 1;
$userId = 'demo';
$roomId = "demo";
$secret = 'fa94dd0f974cf2e293728a526b028271';
$rtcRoomPayLoad = [
    'room_id' => $roomId, //房间id;用于对接口的房间id进行强验证
    'privilege' => [     //权限位开关列表;用于对接口的操作权限进行强验证
        PrivilegeKeyLogin => PrivilegeEnable,
        PrivilegeKeyPublish => PrivilegeDisable,
    ],
    'stream_id_list' => [] //流列表;用于对接口的流id进行强验证;允许为空,如果为空,则不对流id验证
];

$payload = json_encode($rtcRoomPayLoad);

$token = ZegoServerAssistant::generateToken04($appId, $userId, $secret, 3600, $payload);
if( $token->code == ZegoErrorCodes::success ){
  print_r($token);
}

namespace ZEGO;

class ZegoErrorCodes{
    const success                       = 0;  // 获取鉴权 token 成功
    const appIDInvalid                  = 1;  // 调用方法时传入 appID 参数错误
    const userIDInvalid                 = 3;  // 调用方法时传入 userID 参数错误
    const secretInvalid                 = 5;  // 调用方法时传入 secret 参数错误
    const effectiveTimeInSecondsInvalid = 6;  // 调用方法时传入 effectiveTimeInSeconds 参数错误
}

    /**
     * 根据所提供的参数列表生成用于与即构服务端通信的鉴权
     *
     * @param integer $appId Zego派发的数字ID, 各个开发者的唯一标识
     * @param string $userId 用户 ID
     * @param string $secret 由即构提供的与 appId 对应的密钥,请妥善保管,切勿外泄
     * @param integer $effectiveTimeInSeconds token 的有效时长,单位:秒
     * @param string $payload 业务扩展字段,json串
     * @return ZegoAssistantToken 返回 token 内容,值为ZEGO\ZegoAssistantToken对象,在使用前,请检查对象code属性是否为 ZEGO\ZegoErrorCodes::success。实际 token 内容保存在 token 属性中
     */
    public static function generateToken04(int $appId, string $userId, string $secret, int $effectiveTimeInSeconds, string $payload )




namespace ZEGO;

class ZegoAssistantToken{
    public $code;
    public $message = '';
    public $token;
}