您的位置 首页 技术专栏

php怎么实现在线直播功能

24小时课堂在线收录php怎么实现在线直播功能,php实现在线直播功能的方法:1、在控制台找到直播云服务,创建直播云空间;2、按需要将域名解析出来;3、安装composer包;4、通过pv…感谢您关注php怎么实现在线直播功能。

php实现在线直播功能的方法:1、在控制台找到直播云服务,创建直播云空间;2、按需要将域名解析出来;3、安装composer包;4、通过pveStart方法实现直播即可。

本文操作环境:Windows7系统,PHP7.1版,Dell G3电脑。

php怎么实现在线直播功能?

php 七牛云实现直播功能:

一:最近在做一个直播卖货的项目,后台搭建好了准备接入直播,搜了几家阿里,TX和七牛,结果阿里的直播php只有代码没有文档,TX的我朋友说代码比较乱就不考虑了,上了七牛注册了一个账户,申请直播空间的时候被域名卡主了,已经备案的域名还要再网站公安备案一次

https://developer.qiniu.com/af/kb/3987/how-to-make-website-and-inquires-the-popce-put-on-record-information?ref=support.qiniu.com

又搜了搜发现涉及网络表演业务的,需办理《网络文化经营许可证》,请咨询当地人民政府文化行政部门,等待申请完以后在进行下一步。

二:域名备案终于好了,开始搞第二步,实现直播功能,移动端可以参考七牛云SDK,下面是服务端推流案例,本次使用的是rtmp流实现直播,在控制台找到直播云服务,创建直播云空间php怎么实现在线直播功能 创建好直播空间后会生成几个二级域名,按需要将域名解析出来,然后就到了下面的样子php怎么实现在线直播功能 代码运行起来后会在直播流中看到你说创建的直播流播放历史等信息php怎么实现在线直播功能 安装composer包

php composer.phar require qiniu/php-sdk

再vendor/pip-engineering/pip-sdk-php.v2里能找到两个案例,一个是直播的,一个是连麦的,这次先实现直播,下一篇再更新一下连麦

<?phpnamespace App\Modules\Api\Http\Controllers;use App\Modules\Live\Models\Broadcast;use App\Modules\Live\Repositories\BroadcastRepositoryEloquent;use Illuminate\Http\Request;use Qiniu\Pip\Cpent;use Qiniu\Pip\Mac;use function Qiniu\Pip\RTMPPlayURL;use function Qiniu\Pip\RTMPPubpshURL;use function Qiniu\Pip\SnapshotPlayURL;class LiveController extends ApiBaseController{ private $auth; private $accessKey; private $secretKey; private $hubName; /** * Create a new controller instance. * * @return void */ pubpc function __construct() { $this->accessKey = config(“qiniu.accessKey”); $this->secretKey = config(“qiniu.secretKey”); $this->hubName = config(“qiniu.bucket”); parent::__construct(); } /** *开启直播 */ pubpc function pveStart(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); $cpent = new Cpent($mac); $hub = $cpent->hub($this->hubName); //获取stream $streamKey = $broadcast[‘show_id’]; $stream = $hub->stream($streamKey); $pst = $hub->pstStreams($streamKey, 1, “”); //如果没找到对应的直播流创建新直播流 if (count($pst[‘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’]]); } //更新商城直播间 pubpc function updateShop($broadcast, $streamKey, &$msg = ”) { //获取推流地址 $p_href = RTMPPubpshURL(“pip-pubpsh.chengduphong.com”, $this->hubName, $streamKey, 3600, $this->accessKey, $this->secretKey); //获取播放地址 $g_href = RTMPPlayURL(“pip-pubpsh.chengduphong.com”, $this->hubName, $streamKey); //截图直播地址 $pic = SnapshotPlayURL(“pip-pubpsh.chengduphong.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怎么实现在线直播功能的详细内容,更多请关注24小时课堂在线其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/100462.html

为您推荐

返回顶部