php怎么实现在线直播功能
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怎么实现在线直播功能的详细内容,更多请关注其它相关文章!
上一篇: MySQL数据库的23个注意事项
下一篇: jquery是什么意思?能用来做什么?