sahusoftcom / youtube-livestream-api
用于视频直播的 Google / YouTube API 的 PHP (Laravel) 包,支持 Google Auth
v2.0.0
2019-02-28 16:38 UTC
Requires
- php: >=5.5.9
- google/apiclient: ^2.0
- laravel/framework: ^5.1
README
用于视频直播的 Google / YouTube API 的 PHP (Laravel) 包,支持 Google Auth
安装
composer require sahusoftcom/youtube-livestream-api
将服务提供者添加到 config/app.php 中的提供者数组
SahusoftCom\YoutubeApi\LiveStreamApiServiceProvider::class
执行以下命令以获取配置
php artisan vendor:publish --tag='youtube-config'
创建 Google OAuth 凭据的步骤
- 转到
https://console.developers.google.com
- 使用您的凭据登录,然后创建一个新项目。
- 创建密钥时启用以下功能
- YouTube 数据 API
- YouTube 分析 API
- YouTube 报告 API
- 然后在凭据选项卡中创建
API 密钥
。 - 然后在 OAuth 协议屏幕中输入
产品名称
(您的站点名称)。 - 创建凭据 > 选择 OAuth 客户端 ID. (在这里您将获得 client_id 和 client_secret)
- 然后,在授权 JavaScript 原始位置部分添加
您的站点 URL
。 - 在授权重定向 URL 部分添加
添加您想要授权代码返回的 URL
(redirect_url) - 您将获得值(确切地说 - client_id、client_secret、api_key、redirect_url)
- 现在,将这些值 - client_id、client_secret、api_key 和 redirect_url 添加到环境文件中。
设置应用程序
身份验证和授权
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\AuthService; // Add Code to call the api class
$authServiceObject = new AuthService(); # Replace the identifier with a unqiue identifier for account or channel $authUrl = $authServiceObject->getLoginUrl('email','identifier');
- 现在您将获得 $authUrl,当重定向到同一页面时,将要求权限并授权访问所需的频道。
- 提交要求后,您将在登录回调 URL (已添加到 Google 控制台的重定向 URL) 上获得 $authcode & 您可能已在 .env 文件中指定。
- 使用以下 authcode 生成 auth token
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\AuthService; // Add Code to call the api class
$authServiceObject = new AuthService(); $authToken = $authServiceObject->getToken($authcode);
创建 YouTube 事件
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$data = array( "title" => "", "description" => "", "thumbnail_path" => "", // Optional "event_start_date_time" => "", "event_end_date_time" => "", // Optional "time_zone" => "", 'privacy_status' => "", // default: "public" OR "private" "language_name" => "", // default: "English" "tag_array" => "" // Optional and should not be more than 500 characters ); $ytEventObj = new YoutubeLiveEventService(); /** * The broadcast function returns array of details from YouTube. * Store this information & will be required to supply to youtube * for live streaming using encoder of your choice. */ $response = $ytEventObj->broadcast($authToken, $data); if ( !empty($response) ) { $youtubeEventId = $response['broadcast_response']['id']; $serverUrl = $response['stream_response']['cdn']->ingestionInfo->ingestionAddress; $serverKey = $response['stream_response']['cdn']->ingestionInfo->streamName; }
更新 YouTube 事件
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService(); /** * The updateBroadcast response give details of the youtube_event_id,server_url and server_key. * The server_url & server_key gets updated in the process. (save the updated server_key and server_url). */ $response = $ytEventObj->updateBroadcast($authToken, $data, $youtubeEventId); // $youtubeEventId = $response['broadcast_response']['id']; // $serverUrl = $response['stream_response']['cdn']->ingestionInfo->ingestionAddress; // $serverKey = $response['stream_response']['cdn']->ingestionInfo->streamName
删除 YouTube 事件
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService(); /** * Deleting the event requires authentication token for the channel in which the event is created and the youtube_event_id */ $ytEventObj->deleteEvent($authToken, $youtubeEventId);
启动 YouTube 事件流
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService(); /** * $broadcastStatus - ["testing", "live"] * Starting the event takes place in 3 steps * 1. Start sending the stream to the server_url via server_key recieved as a response in creating the event via the encoder of your choice. * 2. Once stream sending has started, stream test should be done by passing $broadcastStatus="testing" & it will return response for stream status. * 3. If transitioEvent() returns successfull for testing broadcast status, then start live streaming your video by passing $broadcastStatus="live" * & in response it will return us the stream status. */ $streamStatus = $ytEventObj->transitionEvent($authToken, $youtube_event_id,$broadcastStatus);
停止 YouTube 事件流
<?php namespace Your\App\NameSpace; use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService(); /** * $broadcastStatus - ["complete"] * Once live streaming gets started succesfully. We can stop the streaming the video by passing broadcastStatus="complete" and in response it will give us the stream status. */ $ytEventObj->transitionEvent($authToken,$youtube_event_id, $broadcastStatus); // $broadcastStatus = ["complete"]