PHP code example of riven / hyperf-zego

1. Go to this page and download the library: Download riven/hyperf-zego 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/ */

    

riven / hyperf-zego example snippets




declare(strict_types=1);

use function Hyperf\Support\env;

return [
    'app_id'           => env("ZEGO_APP_ID", '1234567890'),
    /** Token04 与实时音视频服务端 API 共用的 ServerSecret(控制台项目配置) */
    'secret'           => env("ZEGO_SECRET", 'fa94dd0f974cf2e293728a526b028271'),
    /** 服务端 API 接入域名,见「调用方式」接入地址表 */
    'rtc_api_base_url' => env('ZEGO_RTC_API_BASE_URL', 'https://rtc-api.zego.im'),
    /**
     * 是否测试环境 IsTest。老项目需填 true/false;新项目可保持 null 表示不传该参数。
     * @var bool|string|null
     */
    'is_test'          => null,
    /** 服务端回调校验密钥 callbacksecret(退出房间等回调 SHA1 验签) */
    'callback_secret'  => env('ZEGO_CALLBACK_SECRET', ''),
];



use ZEGO\ZegoErrorCodes;
use ZEGO\ZegoServerAssistant;
use Hyperf\Utils\ApplicationContext;

// 从容器中获取配置
$config = ApplicationContext::getContainer()->get(\Hyperf\Contract\ConfigInterface::class);
$appId = (int) $config->get('zego.app_id');
$secret = $config->get('zego.secret');

$userId = 'demo';
$payload = '';

// 生成令牌,3600 为令牌过期时间,单位:秒
$token = ZegoServerAssistant::generateToken04($appId, $userId, $secret, 3600, $payload);

if ($token->code == ZegoErrorCodes::success) {
    echo "生成的令牌:" . $token->token . "\n";
} else {
    echo "生成令牌失败:" . $token->message . "\n";
}



use ZEGO\ZegoErrorCodes;
use ZEGO\ZegoServerAssistant;
use Hyperf\Utils\ApplicationContext;

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

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

// 从容器中获取配置
$config = ApplicationContext::getContainer()->get(\Hyperf\Contract\ConfigInterface::class);
$appId = (int) $config->get('zego.app_id');
$secret = $config->get('zego.secret');

$userId = 'demo';
$roomId = "demo";

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

$payload = json_encode($rtcRoomPayLoad);

// 生成令牌,3600 为令牌过期时间,单位:秒
$token = ZegoServerAssistant::generateToken04($appId, $userId, $secret, 3600, $payload);

if ($token->code == ZegoErrorCodes::success) {
    echo "生成的令牌:" . $token->token . "\n";
} else {
    echo "生成令牌失败:" . $token->message . "\n";
}

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

class ZegoAssistantToken {
    public $code;      // 错误码,0 表示成功
    public $message;   // 错误信息
    public $token;     // 生成的令牌
}
bash
php bin/hyperf.php vendor:publish riven/hyperf-zego
bash
php bin/hyperf.php vendor:publish riven/hyperf-zego