kield-01 / youtube
Laravel PHP Facade/Wrapper for the Youtube Data API v3
2.2.3
2019-03-28 14:19 UTC
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: ^6.1
This package is auto-updated.
Last update: 2024-09-29 02:02:29 UTC
README
Laravel PHP Facade/Wrapper for the Youtube Data API v3 (非OAuth)
需求
- PHP 7.0 或更高版本
- Laravel 5.1 或更高版本
- 从 Google Console 获取 API 密钥
寻找适用于 PHP 5、Laravel 5.0 或 Laravel 4 的 Youtube 包?请访问 php5-branch
安装
在控制台运行以下命令将包下载到您的项目中
composer require alaouy/youtube
配置
在 /config/app.php
中添加 YoutubeServiceProvider
KielD01\Youtube\YoutubeServiceProvider::class,
不要忘记也添加 Youtube 门面
'Youtube' => KielD01\Youtube\Facades\Youtube::class,
发布配置设置
$ php artisan vendor:publish --provider="KielD01\Youtube\YoutubeServiceProvider"
在文件中设置您的 Youtube API 密钥
/config/youtube.php
或在 .env 文件中
YOUTUBE_API_KEY = KEY
或者您可以在运行时程序化地设置密钥
Youtube::setApiKey('KEY');
用法
// use KielD01\Youtube\Facades\Youtube; // Return an STD PHP object $video = Youtube::getVideoInfo('rie-hPVJ7Sw'); // Get multiple videos info from an array $videoList = Youtube::getVideoInfo(['rie-hPVJ7Sw','iKHTawgyKWQ']); // Get multiple videos related to a video $relatedVideos = Youtube::getRelatedVideos('iKHTawgyKWQ'); // Get comment threads by videoId $commentThreads = Youtube::getCommentThreadsByVideoId('zwiUB_Lh3iA'); // Get popular videos in a country, return an array of PHP objects $videoList = Youtube::getPopularVideos('us'); // Search playlists, channels and videos. return an array of PHP objects $results = Youtube::search('Android'); // Only search videos, return an array of PHP objects $videoList = Youtube::searchVideos('Android'); // Search only videos in a given channel, return an array of PHP objects $videoList = Youtube::searchChannelVideos('keyword', 'UCk1SpWNzOs4MYmr0uICEntg', 40); // List videos in a given channel, return an array of PHP objects $videoList = Youtube::listChannelVideos('UCk1SpWNzOs4MYmr0uICEntg', 40); $results = Youtube::searchAdvanced([ /* params */ ]); // Get channel data by channel name, return an STD PHP object $channel = Youtube::getChannelByName('xdadevelopers'); // Get channel data by channel ID, return an STD PHP object $channel = Youtube::getChannelById('UCk1SpWNzOs4MYmr0uICEntg'); // Get playlist by ID, return an STD PHP object $playlist = Youtube::getPlaylistById('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs'); // Get playlists by multiple ID's, return an array of STD PHP objects $playlists = Youtube::getPlaylistById(['PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs', 'PL590L5WQmH8cUsRyHkk1cPGxW0j5kmhm0']); // Get playlist by channel ID, return an array of PHP objects $playlists = Youtube::getPlaylistsByChannelId('UCk1SpWNzOs4MYmr0uICEntg'); // Get items in a playlist by playlist ID, return an array of PHP objects $playlistItems = Youtube::getPlaylistItemsByPlaylistId('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs'); // Get channel activities by channel ID, return an array of PHP objects $activities = Youtube::getActivitiesByChannelId('UCk1SpWNzOs4MYmr0uICEntg'); // Retrieve video ID from original YouTube URL $videoId = Youtube::parseVidFromURL('https://www.youtube.com/watch?v=moSFlvxnbgk'); // result: moSFlvxnbgk
基本搜索分页
// Set default parameters $params = [ 'q' => 'Android', 'type' => 'video', 'part' => 'id, snippet', 'maxResults' => 50 ]; // Make intial call. with second argument to reveal page info such as page tokens $search = Youtube::searchAdvanced($params, true); // Check if we have a pageToken if (isset($search['info']['nextPageToken'])) { $params['pageToken'] = $search['info']['nextPageToken']; } // Make another call and repeat $search = Youtube::searchAdvanced($params, true); // Add results key with info parameter set print_r($search['results']); /* Alternative approach with new built-in paginateResults function */ // Same params as before $params = [ 'q' => 'Android', 'type' => 'video', 'part' => 'id, snippet', 'maxResults' => 50 ]; // An array to store page tokens so we can go back and forth $pageTokens = []; // Make inital search $search = Youtube::paginateResults($params, null); // Store token $pageTokens[] = $search['info']['nextPageToken']; // Go to next page in result $search = Youtube::paginateResults($params, $pageTokens[0]); // Store token $pageTokens[] = $search['info']['nextPageToken']; // Go to next page in result $search = Youtube::paginateResults($params, $pageTokens[1]); // Store token $pageTokens[] = $search['info']['nextPageToken']; // Go back a page $search = Youtube::paginateResults($params, $pageTokens[0]); // Add results key with info parameter set print_r($search['results']);
上面的分页相当基础。根据您要实现的目标,您可能需要创建一个递归函数来遍历结果。
运行单元测试
如果您已经在环境中安装了 PHPUnit,请运行
$ phpunit
如果您没有安装 PHPUnit,可以运行以下命令
$ composer update $ ./vendor/bin/phpunit
返回数据格式
返回的 JSON 被解码为 PHP 对象(而不是数组)。请阅读官方 API 文档中的 "参考" 部分。
Youtube Data API v3
致谢
基于 Madcoda 的 php-youtube-api 代码构建。