php實現線上直播功能的方法:1、在控制台找到直播雲服務,創建直播雲空間;2、按需要將域名解析出來;3、安裝composer包;4、透過liveStart方法實現直播即可。
本文操作環境:Windows7系統,PHP7.1版,Dell G3電腦。
php怎麼實現線上直播功能?
php 七牛雲實現直播功能:
一:最近在做一個直播賣貨的項目,後台搭建好了準備接入直播,搜了幾家阿里,TX和七牛,結果阿里的直播php只有代碼沒有文檔,TX的我朋友說代碼比較亂就不考慮了,上了七牛註冊了一個帳戶,申請直播空間的時候被域名卡主了,已經備案的網域還要再網站公安備案一次
https://developer.qiniu.com/af/kb/3987/how-to-make-website-and-inquires-the-police-put-on-record-information?ref=support.qiniu.com
又搜發現涉及網路表演業務的,需辦理《網路文化經營許可證》,請諮詢當地人民政府文化行政部門,等待申請完後在進行下一步。
二:網域備案終於好了,開始搞第二步,實作直播功能,行動端可以參考七牛雲SDK,以下是服務端推流案例,本次使用的是rtmp流實現直播,在控制台找到直播雲服務,創建直播雲空間
創建好直播空間後會生成幾個二級域名,按需要將域名解析出來,然後就到了下面的樣子
代碼運行起來後會在直播流中看到你說創建的直播流播放歷史等信息
安裝composer套件
php composer.phar require qiniu/php-sdk
再vendor/pili-engineering/pili-sdk-php.v2裡能找到兩個案例,一個是直播的,一個是連麥的,這次先實現直播,下一篇再更新一下連麥
<?php namespace App\Modules\Api\Http\Controllers; use App\Modules\Live\Models\Broadcast; use App\Modules\Live\Repositories\BroadcastRepositoryEloquent; use Illuminate\Http\Request; use Qiniu\Pili\Client; use Qiniu\Pili\Mac; use function Qiniu\Pili\RTMPPlayURL; use function Qiniu\Pili\RTMPPublishURL; use function Qiniu\Pili\SnapshotPlayURL; class LiveController extends ApiBaseController { private $auth; private $accessKey; private $secretKey; private $hubName; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->accessKey = config("qiniu.accessKey"); $this->secretKey = config("qiniu.secretKey"); $this->hubName = config("qiniu.bucket"); parent::__construct(); } /** *开启直播 */ public function liveStart(Request $request) { $userInfo = parent::getAuthenticatedUser($msg); if (isset($userInfo['user']) && !empty($userInfo['user'])) { $request->offsetSet('user_id', $userInfo['user']['id']); } else { return $this->sendResponse($msg, 'error', '', 401); } $data = $request->all(); $broadcast = app(BroadcastRepositoryEloquent::class)->findWhere(['type' => $data['type'], 'user_id' => $data['user_id']])->first(); if (empty($broadcast)) { return $this->sendResponse(trans('admin.operate_failed') . '未找到直播间'); } $broadcast['name'] = $data['name']; //创建hub $mac = new Mac($this->accessKey, $this->secretKey); $client = new Client($mac); $hub = $client->hub($this->hubName); //获取stream $streamKey = $broadcast['show_id']; $stream = $hub->stream($streamKey); $list = $hub->listStreams($streamKey, 1, ""); //如果没找到对应的直播流创建新直播流 if (count($list['keys']) == 0) { //获取stream $hub->create($streamKey); } if ($data['type'] == 0) { $result = $this->updateShop($broadcast, $streamKey, $msg); if ($result == false) { return $this->sendResponse(trans('admin.operate_failed') . $msg); } } else { $result = $this->updateCurriculum($broadcast, $streamKey, $msg); if ($result == false) { return $this->sendResponse(trans('admin.operate_failed') . $msg); } } return $this->sendResponse(trans('admin.operate_succeeded'), 'succ', ['p_href' => $broadcast['p_href']]); } //更新商城直播间 public function updateShop($broadcast, $streamKey, &$msg = '') { //获取推流地址 $p_href = RTMPPublishURL("pili-publish.chengdulihong.com", $this->hubName, $streamKey, 3600, $this->accessKey, $this->secretKey); //获取播放地址 $g_href = RTMPPlayURL("pili-publish.chengdulihong.com", $this->hubName, $streamKey); //截图直播地址 $pic = SnapshotPlayURL("pili-publish.chengdulihong.com", $this->hubName, $streamKey); //更新直播间状态 $u_broadcast = $broadcast->fill(['name' => $broadcast['name'], 'chatroom_status' => 0, 'p_href' => $p_href, 'g_href' => $g_href, 'pic' => $pic])->save(); if ($u_broadcast == false) { return $this->sendResponse(trans('admin.operate_failed') . '更新直播间出错'); } return true; }
推薦學習:《PHP視訊教學》
#以上是php怎麼實現線上直播功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!