Installation
composer require madcoda/php-youtube-api
Add the package to composer.json and run composer update.
First Use Case: Fetching a Video
use Madcoda\YouTube\YouTube;
$youtube = new YouTube('YOUR_API_KEY');
$video = $youtube->videos->get('VIDEO_ID');
echo $video->title;
Key Files to Explore
src/YouTube.php: Main class for API initialization.src/Resources/Videos.php: Video-related methods (e.g., search, upload, comments).src/Resources/Playlists.php: Playlist operations.src/Resources/Channels.php: Channel data retrieval.$search = $youtube->videos->search('laravel', [
'type' => 'video',
'maxResults' => 5,
]);
foreach ($search as $item) {
echo $item->title . "\n";
}
use Madcoda\YouTube\Upload\Video;
$upload = new Video($youtube, [
'title' => 'My Laravel Tutorial',
'description' => 'Learn Laravel basics',
'file' => fopen('video.mp4', 'r'),
]);
$upload->upload();
// Create a playlist
$playlist = $youtube->playlists->create('My Laravel Playlist');
// Add video to playlist
$youtube->playlistItems->insert('PLAYLIST_ID', 'VIDEO_ID');
// Subscribe to a channel
$youtube->subscriptions->insert('CHANNEL_ID');
// List subscriptions
$subscriptions = $youtube->subscriptions->list();
Cache::remember()).Madcoda\YouTube\Exception\YouTubeException.$youtube->getQuota() and implement retries for quotaExceeded errors.YouTube instance in a service provider for dependency injection:
$this->app->singleton(YouTube::class, function ($app) {
return new YouTube(config('services.youtube.api_key'));
});
API Key Management
.env:
YOUTUBE_API_KEY=your_key_here
Rate Limits
403 Forbidden.Deprecated Methods
videos->getById()) may not exist in newer API versions. Refer to the official YouTube API docs for updates.File Uploads
Madcoda\YouTube\Upload\Video, but test with small files first.Time Zones and Dates
use Carbon\Carbon;
$publishedAt = Carbon::parse($video->publishedAt);
Enable Debugging: Set the debug option in the constructor:
$youtube = new YouTube('API_KEY', ['debug' => true]);
Logs HTTP requests/responses to storage/logs/youtube.log.
Common Errors:
400 Bad Request: Validate parameters (e.g., part field in requests).403 Forbidden: Check API key permissions or quota.404 Not Found: Verify IDs (e.g., video/playlist IDs).Custom Responses
Override the default response handling by extending the Madcoda\YouTube\YouTube class:
class CustomYouTube extends YouTube {
protected function handleResponse($response) {
// Custom logic (e.g., transform data)
return parent::handleResponse($response);
}
}
Adding New Resources
The package follows a modular structure. To add support for a new API endpoint (e.g., activities):
Madcoda\YouTube\Resource\AbstractResource.YouTube.php.Mocking for Testing
Use Laravel’s HTTP mocking or create a decorator for the YouTube class:
$mockYoutube = Mockery::mock(YouTube::class)->makePartial();
$mockYoutube->shouldReceive('videos->get')->andReturn($mockVideo);
Webhooks The package doesn’t support webhooks natively, but you can use the Google Cloud Pub/Sub or YouTube Notification API for real-time updates.
How can I help you explore Laravel packages today?