cable8mm / youtube
Renew Laravel PHP Facade/Wrapper for the Youtube Data API v3
Requires
- php: ^8.2
- ext-curl: *
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- nesbot/carbon: ^3.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^9.0|^10.0|^11.0
- vlucas/phpdotenv: ^5.0
README
๐ 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.
โจ Features
- ๐ฏ Simple & Elegant API - Clean, intuitive, and Laravel-style interface
- โก Fluent Interface - Chain methods for better readability
- ๐ Type-Safe - Full type hints and strict typing for PHP 8.2+
- ๐จ Laravel Native - Seamless integration with Laravel ecosystem
- ๐ High Performance - Built-in response caching to reduce API calls
- โ Well Tested - 73 comprehensive tests with 100% pass rate
- ๐ก๏ธ Custom Exceptions - Domain-specific error handling
- ๐ Validation Rules - Built-in validation for YouTube URLs (Laravel 10+)
- ๐ Auto-Discovery - Automatic service provider registration
- ๐ Extensive Documentation - Clear examples and usage guides
๐ Requirements
- PHP 8.2 or higher
- Laravel 10.x, 11.x, 12.x, or 13.x
- YouTube Data API v3 Key (Get one here)
๐ Installation
Install the package via Composer:
composer require cable8mm/youtube
โ๏ธ Configuration
Step 1: Publish Configuration
php artisan vendor:publish --provider="Cable8mm\Youtube\YoutubeServiceProvider"
Step 2: Add API Key
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'), ];
๐ฏ Quick Start
Basic Usage
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);
Fluent Interface
$youtube = (new Youtube($apiKey)) ->useHttpHost(true) ->cache() ->setCacheTtl(1800); $videos = $youtube->getPopularVideos('US', 10);
With Caching (Recommended)
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);
๐ API Reference
Video Methods
// 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);
Search Methods
// 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
Channel Methods
// 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');
Playlist Methods
// 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);
Comment Methods
// Get comment threads by video ID $comments = Youtube::getCommentThreadsByVideoId('video_id', 20, 'time');
Activity Methods
// Get channel activities $activities = Youtube::getActivitiesByChannelId('channel_id', 10);
Utility Methods
// Parse video ID from URL $videoId = Youtube::parseVidFromURL('https://youtu.be/rie-hPVJ7Sw'); // Get channel from URL $channel = Youtube::getChannelFromURL('https://youtube.com/channel/...');
๐ Pagination
Basic Pagination
$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); }
Using paginateResults()
$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]);
โ Validation Rules
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_ID
Note: Uses Laravel 10+ ValidationRule interface with closure-based error messages.
๐งช Testing
Run the test suite:
# Run all tests composer test # Run with API tests enabled YOUTUBE_ENABLED=true composer test
Test Coverage:
- โ 73 comprehensive tests
- โ Unit tests (no API key required)
- โ Integration tests with Orchestra Testbench (requires API key)
- โ 100% pass rate
๐ Package Structure
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
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ Changelog
See CHANGELOG.md for recent changes.
๐ Links
- ๐ฆ Packagist
- ๐ Issue Tracker
- ๐ป GitHub Repository
๐ Credits
- Built on code from Alaouy/youtube
- Inspired by the Laravel community
๐ License
This package is open-sourced software licensed under the MIT license.
Made with โค๏ธ by cable8mm