mokhosh / laravel-youtube-api
这是一个基于 Youtube Api v3 的包装器,它以 Laravel 的方式简化了功能。
v1.5.0
2024-01-01 15:23 UTC
Requires
- google/apiclient: ^2.0
Requires (Dev)
README
PHP (Laravel) 包,用于 Google / YouTube API V3 及 Google 认证
特性
安装
composer require mokhosh/laravel-youtube-api
将服务提供者添加到 config/app.php 中 providers 数组
Mokhosh\LaravelYoutubeApi\LaravelYoutubeApiServiceProvider::class
执行以下命令以获取配置
php artisan vendor:publish --tag='youtube-api'
创建您的 Google OAuth 凭据的步骤
- 转到
https://console.developers.google.com
- 使用您的凭据登录,然后创建一个新的项目。
- 在创建密钥时启用以下功能
- YouTube 数据 API
- YouTube 分析 API
- YouTube 报告 API
- 然后从凭据选项卡创建
API 密钥
。 - 然后在 OAuth 认证屏幕中输入
产品名称
(您的站点名称)。 - 创建凭据 > 选择 OAuth 客户端 ID。 (在这里您将获得 client_id 和 client_secret)
- 然后在授权 JavaScript 原始位置部分添加
您的站点 URL
。 - 在授权重定向 URL 部分添加
添加一个您想要 auth 代码返回的 URL
(登录回调) - 您将获得值(确切地说 - client_id、client_secret 和 api_key)
- 然后在这些值 - client_id、client_secret、api_key 和 redirect_url 中添加到 .env 文件中,您现在可以开始使用该包了。
使用方法
Google 认证
- 添加代码以调用 API 类
<?php namespace Your\App\NameSpace; use Mokhosh\LaravelYoutubeApi\AuthenticateService;
- 生成认证 URL
$authObject = new AuthenticateService; # Replace the identifier with a unqiue identifier for account or channel $authUrl = $authObject->getLoginUrl('email','identifier');
- 获取认证代码和标识符 现在,一旦用户通过访问 URL 进行授权,认证代码将重定向到 .env 中指定的 redirect_url,带有参数 code(这将是认证代码)和 state(这将是我们在创建 loginUrl 时添加的标识符)
$code = Input::get('code'); $identifier = Input::get('state');
- 频道认证令牌和详情
$authObject = new AuthenticateService; $authResponse = $authObject->authChannelWithCode($code);
- 这将返回一个数组:
$authResponse['token'] (Channel Token)
$authResponse['channel_details']
$authResponse['live_streaming_status'] (enabled or disabled)
完整的直播 API
- 添加代码以调用 API 类
<?php namespace Your\App\NameSpace; use Mokhosh\LaravelYoutubeApi\LiveStreamService;
- 创建 YouTube 事件
# data format creating live event $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 LiveStreamService(); /** * 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 事件
$ytEventObj = new LiveStreamService(); /** * 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 事件
$ytEventObj = new LiveStreamService(); # 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 事件流
$ytEventObj = new LiveStreamService(); /** * $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, $youtubeEventId, $broadcastStatus);
- 停止 YouTube 事件流
$ytEventObj = new LiveStreamService(); /** * $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, $youtubeEventId, $broadcastStatus); // $broadcastStatus = ["complete"]
完整的 YouTube 频道 API
- 添加代码以调用 API 类
<?php namespace Your\App\NameSpace; use Mokhosh\LaravelYoutubeApi\ChannelService;
- 按频道 ID 获取频道详情 如果您想为多个频道获取频道详情,请在参数中添加以逗号(,)分隔的频道 ID(请注意,这不需要授权令牌)
/** * [channelsListById -gets the channnel details and ] * $part 'id,snippet,contentDetails,status, statistics, contentOwnerDetails, brandingSettings' * $params [array channels id(comma separated ids ) or you can get ('forUsername' => 'GoogleDevelopers')] */ $part = 'id,snippet'; $params = array('id'=> 'channel_1_id,channel_2_id'); $channelServiceObject = new ChannelService; $channelDetails = $channelServiceObject->channelsListById($part, $params);
- 按令牌获取频道详情 已授权令牌的用户频道的详情
$channelServiceObject = new ChannelService; $channelDetails = $channelServiceObject->getChannelDetails($authToken);
- 频道订阅列表 频道的订阅列表
/* * $params array('channelId'=>'', 'totalResults'= 10) * totalResults is different of maxResults from Google Api. * totalResults = the amount of results you want * maxResults = max of results PER PAGE. We don't need this parameter here since it will loop until it gets all the results you want. */ $channelServiceObject = new ChannelService; $channelDetails = $channelServiceObject->subscriptionByChannelId($params);
- 为授权频道添加订阅
/* * properties array('snippet.resourceId.kind' => 'youtube#channel','snippet.resourceId.channelId' => 'UCqIOaYtQak4-FD2-yI7hFkw') */ $channelServiceObject = new ChannelService; $response = $channelServiceObject->addSubscriptions($properties, $token, $part='snippet', $params=[]);
为授权频道移除订阅 要删除订阅,我们需要订阅 ID,这可以从订阅列表中找到。
$response = $channelServiceObject->removeSubscription( $token, $subscriptionId);
- 更新频道品牌设置 更新频道详情和首选项。
/* * $properties array('id' => '', * 'brandingSettings.channel.description' => '', * 'brandingSettings.channel.keywords' => '', * 'brandingSettings.channel.defaultLanguage' => '', * 'brandingSettings.channel.defaultTab' => '', * 'brandingSettings.channel.moderateComments' => '', * 'brandingSettings.channel.showRelatedChannels' => '', * 'brandingSettings.channel.showBrowseView' => '', * 'brandingSettings.channel.featuredChannelsTitle' => '', * 'brandingSettings.channel.featuredChannelsUrls[]' => '', * 'brandingSettings.channel.unsubscribedTrailer' => '') */ $channelServiceObject = new ChannelService; $response = $channelServiceObject->updateChannelBrandingSettings($googleToken, $properties);
完整的 YouTube 视频 API
- 添加代码以调用 API 类
<?php namespace Your\App\NameSpace; use Mokhosh\LaravelYoutubeApi\VideoService;
- 按 ID 列出视频
$part ='snippet,contentDetails,id,statistics'; $params =array('id'=>'xyzgh'); $videoServiceObject = new VideoService; $response = $videoServiceObject->videosListById($part, $params);
- 将视频上传到您的频道
/* * $videoPath path to the video * $data array('title'=>"", * 'description'=>"", * 'tags'=>"", * 'category_id'=>"", * 'video_status'=>"") */ $videoServiceObject = new VideoService; $response = $videoServiceObject->uploadVideo($googleToken, $videoPath, $data);
- 从您的频道删除视频
$videoServiceObject = new VideoService; $response = $videoServiceObject->deleteVideo($googleToken, $videoId);
- 评分视频 添加喜欢、不喜欢或从视频中删除响应
# rating 'like' or 'dislike' or 'none' $videoServiceObject = new VideoService; $response = $videoServiceObject->videosRate($googleToken, $videoId, $rating);