azaw/yt-toolkit

Youtube toolkit

Maintainers

Package info

github.com/AlbertZawadzki/yt-toolkit

pkg:composer/azaw/yt-toolkit

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.4 2026-04-04 14:17 UTC

This package is auto-updated.

Last update: 2026-04-04 14:17:47 UTC


README

A Symfony bundle for interacting with the YouTube Data API v3.

Installation

composer require azaw/yt-toolkit

Register the bundle in config/bundles.php:

return [
    YtToolkit\YtToolkitPackBundle::class => ['all' => true],
];

Configuration

Set your YouTube Data API key as an environment variable:

YOUTUBE_API_KEY=your_api_key_here

The default YoutubeClientSecretProvider reads $_ENV['YOUTUBE_API_KEY']. To use a custom source, implement YoutubeClientSecretProviderInterface and bind it as the alias in your app's service configuration:

services:
  YtToolkit\Contracts\YoutubeClientSecretProviderInterface:
    alias: App\YourCustomSecretProvider

Services

FetchVideosService

Fetches videos from a YouTube channel.

use YtToolkit\Service\FetchVideosService;

$response = $fetchVideosService->fetchVideos(
    channelId: 'UCxxxxxxxxxxxxxxxxxxxxxx',
    maxResults: 25,        // optional, default 25
    pageToken: null,       // optional, for pagination
);

// $response is VideoSearchListResponse
foreach ($response->items as $result) {
    echo $result->id->videoId;
}

// Paginate
$nextPage = $fetchVideosService->fetchVideos('UCxxx', pageToken: $response->nextPageToken);

FetchChannelService

Fetches a channel by its handle (e.g. @channelname).

use YtToolkit\Service\FetchChannelService;

$response = $fetchChannelService->fetchChannelByHandle('@channelname');

// $response is ChannelListResponse
foreach ($response->items as $channel) {
    echo $channel->id;
}

YoutubeParser

Extracts a YouTube video ID from various URL formats (standard, short, embed, live).

use YtToolkit\Parser\YoutubeParser;

$parser = new YoutubeParser();

$videoId = $parser->getCodeFromLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
// 'dQw4w9WgXcQ'

$videoId = $parser->getCodeFromLink('https://youtu.be/dQw4w9WgXcQ');
// 'dQw4w9WgXcQ'

$videoId = $parser->getCodeFromLink('https://www.youtube.com/embed/dQw4w9WgXcQ');
// 'dQw4w9WgXcQ'

$videoId = $parser->getCodeFromLink('https://www.youtube.com/live/dQw4w9WgXcQ');
// 'dQw4w9WgXcQ'

Returns null if no video ID is found.