Backend Development 7 min read

Integrating Alibaba Cloud Live Streaming with ThinkPHP: Configuration, Push and Playback Implementation

This article provides a step‑by‑step guide on configuring Alibaba Cloud Live streaming URLs, implementing push and playback functions in a ThinkPHP application, and using FFMPEG and Hls.js, complete with annotated PHP code examples for developers.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Integrating Alibaba Cloud Live Streaming with ThinkPHP: Configuration, Push and Playback Implementation

With the rapid growth of live streaming, Alibaba Cloud Live has become a popular service. This guide shows how to integrate its push and playback URLs into a ThinkPHP project, explaining each code segment in detail.

The push URL follows the pattern rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum} , where placeholders such as {Domain} , {AppName} , {StreamName} , {AuthKey} , {Timestamp} and {RandomNum} must be replaced with actual values. The playback URL uses the format http://{Domain}/{AppName}/{StreamName}.m3u8 with similar placeholders.

A PHP class LiveAction is defined to store these templates and the concrete configuration values (domains, app name, stream name, auth key). It provides two private helper methods:

private function getPushUrl() {
    $randomNum = rand(100000, 999999);
    $timestamp = time();
    $authKey = md5($this->authKey . $this->appName . $this->streamName . $timestamp . $randomNum);
    $pushUrl = str_replace(
        array('{Domain}', '{AppName}', '{StreamName}', '{AuthKey}', '{Timestamp}', '{RandomNum}'),
        array($this->pushDomain, $this->appName, $this->streamName, $authKey, $timestamp, $randomNum),
        $this->pushUrl
    );
    return $pushUrl;
}

private function getPlayUrl() {
    $playUrl = str_replace(
        array('{Domain}', '{AppName}', '{StreamName}'),
        array($this->playDomain, $this->appName, $this->streamName),
        $this->playUrl
    );
    return $playUrl;
}

To push a local video file to Alibaba Cloud Live, a public push() method builds an FFmpeg command that streams test.flv to the generated push URL and executes it with exec() :

public function push() {
    $pushUrl = $this->getPushUrl();
    $command = 'ffmpeg -re -i test.flv -vcodec copy -acodec aac -f flv ' . $pushUrl;
    exec($command);
}

For playback, a play() method obtains the playback URL, assigns it to the view, and renders the template. The front‑end can then use the Hls.js library to play the .m3u8 stream.

public function play() {
    $playUrl = $this->getPlayUrl();
    $this->assign('playUrl', $playUrl);
    $this->display();
}

The complete class combines all properties and methods, offering a ready‑to‑use solution for both streaming and viewing live video within a ThinkPHP application.

class LiveAction extends Action {
    // Push URL template
    private $pushUrl = 'rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum}';
    // Playback URL template
    private $playUrl = 'http://{Domain}/{AppName}/{StreamName}.m3u8';

    // Configuration values
    private $pushDomain = 'xxx.xxx.com';
    private $playDomain = 'xxx.xxx.com';
    private $appName = 'live';
    private $streamName = 'test';
    private $authKey = '1234567890';

    private function getPushUrl() {
        $randomNum = rand(100000, 999999);
        $timestamp = time();
        $authKey = md5($this->authKey . $this->appName . $this->streamName . $timestamp . $randomNum);
        $pushUrl = str_replace(
            array('{Domain}', '{AppName}', '{StreamName}', '{AuthKey}', '{Timestamp}', '{RandomNum}'),
            array($this->pushDomain, $this->appName, $this->streamName, $authKey, $timestamp, $randomNum),
            $this->pushUrl
        );
        return $pushUrl;
    }

    private function getPlayUrl() {
        $playUrl = str_replace(
            array('{Domain}', '{AppName}', '{StreamName}'),
            array($this->playDomain, $this->appName, $this->streamName),
            $this->playUrl
        );
        return $playUrl;
    }

    public function push() {
        $pushUrl = $this->getPushUrl();
        $command = 'ffmpeg -re -i test.flv -vcodec copy -acodec aac -f flv ' . $pushUrl;
        exec($command);
    }

    public function play() {
        $playUrl = $this->getPlayUrl();
        $this->assign('playUrl', $playUrl);
        $this->display();
    }
}
Live StreamingPHPFFmpegAlibaba CloudHls.jsThinkPHP
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.