You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建用户并关联至现有Nexmo应用?使用vonage-php-sdk-core包实现该需求的可行性咨询

Hey there! Let's break down your questions step by step.


1. How to Create a User and Associate with an Existing Nexmo App

Creating a user and linking them to your app involves two key steps: first creating the user in your Vonage account, then generating a JWT token that ties the user to your specific app (this is how the user authenticates to use the app's capabilities like RTC or Voice).

Quick CLI Reference (for context)

You already know app creation via CLI, but here's how to create a user and generate a linking token with it:

# Create a user
nexmo user:create --name "John Doe"

# Generate a JWT to link the user to your existing app
nexmo jwt:generate private.key sub="USER_ID" application_id="APP_ID"

2. Can the vonage-php-sdk-core Package Create Users and Associate with Existing Apps?

Absolutely! The SDK has all the tools you need to create users and generate the necessary tokens to link them to your app. Here's a complete implementation:

Step 1: Set Up the SDK

First, ensure you have the SDK installed via Composer:

composer require vonage/client-core

Initialize the client with your Vonage API credentials:

require 'vendor/autoload.php';

use Vonage\Client;
use Vonage\Client\Credentials\Basic;
use Vonage\User\UserClient;

$credentials = new Basic('YOUR_VONAGE_API_KEY', 'YOUR_VONAGE_API_SECRET');
$client = new Client($credentials);

Step 2: Create a User

Use the UserClient to create a new account-level user. You can include optional metadata for context:

$userClient = $client->get(UserClient::class);

$user = $userClient->createUser([
    'name' => 'john_doe', // Unique user identifier
    'display_name' => 'John Doe', // Friendly display name (optional)
    'metadata' => [ // Custom data (optional)
        'role' => 'regular_user',
        'email' => 'john@example.com'
    ]
]);

$userId = $user->getId();
echo "Created user with ID: " . $userId;

Step 3: Associate the User with Your Existing App

To link the user to your app, generate a JWT token using your app's private key. This token includes the user ID and app ID, which authenticates the user for your app's capabilities:

use Vonage\Jwt\Generator;
use Vonage\Jwt\Token;

// Load your app's private key (from the private.key file created during app setup)
$privateKey = file_get_contents('private.key');

$generator = new Generator($privateKey);
$token = $generator->generateToken(
    new Token(
        [
            'application_id' => 'YOUR_EXISTING_APP_ID',
            'sub' => $userId, // The user ID we just created
            'exp' => time() + 86400 // Token expires in 1 day (adjust as needed)
        ]
    )
);

echo "JWT Token for user-app association: " . $token;

This JWT is what you'll use in your client-side code (web/mobile) to let the user access your app's Voice/RTC features.


Key Notes

  • Users are account-level resources, so you can link the same user to multiple apps by generating separate JWT tokens for each app.
  • Keep your app's private key secure—never expose it to client-side code.
  • The sub claim in the JWT is mandatory; it tells Vonage exactly which user is accessing the app.

内容的提问来源于stack exchange,提问作者Jazz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:54:07