Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GetStream/stream-video-php-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stream Video PHP Example

Example PHP code snippets for Stream Video API integration

Requirements

  • PHP: ^8.1

Install dependencies

Install dependencies:

composer install

Run the project

Create a .env file in the repository root, with this content:

STREAM_API_KEY=<Your API key>
STREAM_API_SECRET=<Your API secret>

Unit Tests

composer test

Integration Tests

vendor/bin/phpunit tests/Integration

Main file

php src/main.php

Create client

use App\Client;

$apiKey = $_ENV['STREAM_API_KEY'];
$apiSecret = $_ENV['STREAM_API_SECRET'];
    
$client = new Client($apiKey, $apiSecret);

Create or update users

API docs: https://getstream.io/video/docs/api/authentication/#creating-users

use App\DTO\UserRequest;

$inputUser = new UserRequest(
    id: 'sara',
    role: 'user',
    name: 'Sara',
    image: 'https://example.com/avatar.jpg',
    custom: ['nickname' => 'Sara']
);

$response = $client->upsertUsers([$inputUser]);

Token creation

Stream API documentation for tokens: https://getstream.io/video/docs/api/authentication/#user-tokens

Provide user id

use App\DTO\TokenParams;

// Create a basic token with just a user ID
$token = $client->createToken(new TokenParams(
    userId: 'user-123'
));

Optionally provide validity in seconds

// Create a token that expires in 2 hours (7200 seconds) - default validity is 1 hour
$token = $client->createToken(new TokenParams(
    userId: 'user-123',
    validityInSeconds: 7200
));

Create call token

Allows access for specific calls only

use App\DTO\CallTokenParams;

// Create a token for specific calls
$token = $client->createCallToken(new CallTokenParams(
    userId: 'user-123',
    callCids: ['livestream:123', 'livestream:456']
));

Optionally provide a role for call tokens

// Create a call token with a specific role
$token = $client->createCallToken(new CallTokenParams(
    userId: 'user-123',
    callCids: ['livestream:789'],
    role: 'admin'
));

Create calls

API docs: https://getstream.io/video/docs/api/calls/#creating-calls

use App\DTO\GetOrCreateCallRequest;
use App\DTO\CallRequest;

// With only createdBy provided
$call = $this->client->call($this->callType, $this->callId);
$response = $call->getOrCreateCall(new GetOrCreateCallRequest(
    data: new CallRequest(
        createdById: '<user id>'
    )
));

$call = $client->call('livestream', Uuid::uuid4()->toString());
$callResponse = $call->getOrCreateCall(new GetOrCreateCallRequest(
    // Will send call.notification to members
    notify: true,
    data: new CallRequest(
        members: [new MemberRequest(userId: $user1['id']), new MemberRequest(userId: $user2['id'])],
        createdById: $user1['id'],
    ),
));

// Override call settings as well
$callRequest = new CallRequest(
    createdById: '<user id>',
    startsAt: (new \DateTime('+1 hour'))->format(\DateTime::ATOM), // ISO 8601 date string
    members: $members,
    settingsOverride: new CallSettingsRequest(
        backstage: new BackstageSettingsRequest(
            enabled: true,
            joinAheadTimeSeconds: 5*60
        )
    ),
    // Additionally you can provide custom data as well
    custom: ['topic' => 'Integration Test']
);

Go live and stop live

Go live

API docs: https://getstream.io/video/docs/api/streaming/backstage/#go-live

use App\DTO\GoLiveRequest;

$call->goLive(new GoLiveRequest());

// or provide optional config params
$call->goLive(new GoLiveRequest(
    // Optionally start displaying closed captions for call participants
    start_closed_caption: true,
    // Optionally start HLS broadcast
    start_hls: true,
    // Optionally start recording the call
    start_recording: true,
    // Optionally start saving the call transcription to a file
    start_transcription: true,
));

Stop live

API docs: https://getstream.io/video/docs/api/streaming/backstage/#stop-live

use App\DTO\StopLiveRequest;

$call->stopLive(new StopLiveRequest());

// or provide optional config params
$call->stopLive(new StopLiveRequest(
    // Optionally prevent stopping HLS broadcast
    continue_hls: true,
    // Optionally prevent stopping recording
    continue_recording: true,
    // Optionally prevent stopping closed captions
    continue_closed_caption: true,
    // Optionally prevent stopping call transcription
    continue_transcription: true,
));

Delete calls

API docs: https://getstream.io/video/docs/api/gdpr/calls/#calls-deletion

// Soft delete
$call->deleteCall();

// Hard delete
$call->deleteCall(true);

Delete users

API docs: https://getstream.io/video/docs/api/gdpr/users/#users-deletion

$response = $client->deleteUsers([$user['id']], [
    'user' => 'hard',
    'calls' => 'hard'
]);

Useful resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages