Renew Laravel PHP Facade/Wrapper for the Youtube Data API v3
cable8mm/youtube is a Laravel package for renew laravel php facade/wrapper for the youtube data api v3.
It currently has 1 GitHub stars and 244 downloads on Packagist (latest version v5.1.0).
Install it with composer require cable8mm/youtube.
Discover more Laravel packages by cable8mm
or browse all Laravel packages to compare alternatives.
Last updated
π A modern, elegant, and feature-rich Laravel wrapper for YouTube Data API v3 (Non-OAuth)
A beautifully crafted Laravel package that provides a simple, fluent interface to interact with YouTube Data API v3. Built with PHP 8.2+ features, comprehensive test coverage, and developer experience in mind.
Install the package via Composer:
composer require cable8mm/youtube
php artisan vendor:publish --provider="Cable8mm\Youtube\YoutubeServiceProvider"
Add your YouTube API key to your .env file:
YOUTUBE_API_KEY=your_api_key_here
Or directly in config/youtube.php:
return [
'key' => env('YOUTUBE_API_KEY', 'YOUR_API_KEY'),
];
use Cable8mm\Youtube\Facades\Youtube;
// Get video information
$video = Youtube::getVideoInfo('rie-hPVJ7Sw');
// Get multiple videos
$videos = Youtube::getVideoInfo(['rie-hPVJ7Sw', 'iKHTawgyKWQ']);
// Search videos
$results = Youtube::searchVideos('Laravel Tutorial', 10);
// Get channel information
$channel = Youtube::getChannelById('UCk1SpWNzOs4MYmr0uICEntg');
// Get popular videos by country
$popular = Youtube::getPopularVideos('US', 10);
$youtube = (new Youtube($apiKey))
->useHttpHost(true)
->cache()
->setCacheTtl(1800);
$videos = $youtube->getPopularVideos('US', 10);
Enable caching to reduce API calls and improve performance:
// Via constructor
$youtube = new Youtube($apiKey, [
'cache_enabled' => true,
'cache_ttl' => 3600, // 1 hour
]);
// Or via fluent interface
$youtube = (new Youtube($apiKey))->cache()->setCacheTtl(3600);
// Get single video info
$video = Youtube::getVideoInfo('video_id');
// Get multiple videos
$videos = Youtube::getVideoInfo(['id1', 'id2']);
// Get localized video info
$video = Youtube::getLocalizedVideoInfo('video_id', 'ko');
// Get popular videos by region
$videos = Youtube::getPopularVideos('KR', 20);
// General search
$results = Youtube::search('Laravel', 10);
// Search videos only
$videos = Youtube::searchVideos('Laravel', 10, 'viewCount');
// Search in specific channel
$videos = Youtube::searchChannelVideos('keyword', 'channel_id', 20);
// Advanced search with custom parameters
$results = Youtube::searchAdvanced([
'q' => 'Laravel',
'type' => 'video',
'part' => 'id,snippet',
'maxResults' => 50,
'order' => 'date'
], true); // true = include page info
// Get channel by ID
$channel = Youtube::getChannelById('channel_id');
// Get channel by name
$channel = Youtube::getChannelByName('username');
// Get channel videos
$videos = Youtube::getChannelVideos('channel_id', 10, null, false, '');
// List channel videos
$videos = Youtube::listChannelVideos('channel_id', 10, 'date');
// Get playlist by ID
$playlist = Youtube::getPlaylistById('playlist_id');
// Get multiple playlists
$playlists = Youtube::getPlaylistById(['id1', 'id2']);
// Get playlists by channel
$playlists = Youtube::getPlaylistsByChannelId('channel_id');
// Get playlist items
$items = Youtube::getPlaylistItemsByPlaylistId('playlist_id', '', 50);
// Get comment threads by video ID
$comments = Youtube::getCommentThreadsByVideoId('video_id', 20, 'time');
// Get channel activities
$activities = Youtube::getActivitiesByChannelId('channel_id', 10);
// Parse video ID from URL
$videoId = Youtube::parseVidFromURL('https://youtu.be/rie-hPVJ7Sw');
// Get channel from URL
$channel = Youtube::getChannelFromURL('https://youtube.com/channel/...');
$params = [
'q' => 'Laravel',
'type' => 'video',
'part' => 'id,snippet',
'maxResults' => 50
];
// Get first page
$search = Youtube::searchAdvanced($params, true);
// Get next page
if (isset($search['info']['nextPageToken'])) {
$params['pageToken'] = $search['info']['nextPageToken'];
$nextPage = Youtube::searchAdvanced($params, true);
}
$params = [
'q' => 'Laravel',
'type' => 'video',
'part' => 'id,snippet',
'maxResults' => 50
];
$pageTokens = [];
// Initial search
$search = Youtube::paginateResults($params, null);
$pageTokens[] = $search['info']['nextPageToken'];
// Navigate through pages
$page1 = Youtube::paginateResults($params, $pageTokens[0]);
$page2 = Youtube::paginateResults($params, $pageTokens[1]);
// Go back
$previousPage = Youtube::paginateResults($params, $pageTokens[0]);
Validate YouTube video URLs in your Laravel forms:
use Cable8mm\Youtube\Rules\ValidYoutubeVideo;
$request->validate([
'video_url' => ['bail', 'required', new ValidYoutubeVideo],
]);
Supported URL formats:
https://www.youtube.com/watch?v=VIDEO_IDhttps://youtu.be/VIDEO_IDhttps://www.youtube.com/embed/VIDEO_IDNote: Uses Laravel 10+ ValidationRule interface with closure-based error messages.
Run the test suite:
# Run all tests
composer test
# Run with API tests enabled
YOUTUBE_ENABLED=true composer test
Test Coverage:
src/
βββ Youtube.php # Main class
βββ YoutubeServiceProvider.php # Laravel service provider
βββ Facades/
β βββ Youtube.php # Laravel facade
βββ Rules/
β βββ ValidYoutubeVideo.php # Validation rule
βββ Cache/
β βββ YoutubeCache.php # Caching layer
βββ Exceptions/
β βββ YoutubeApiException.php # Custom exceptions
βββ config/
βββ youtube.php # Configuration file
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)See CHANGELOG.md for recent changes.
This package is open-sourced software licensed under the MIT license.
Made with β€οΈ by cable8mm