PHPackages                             devsarfo/youtube - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. devsarfo/youtube

ActiveLibrary[API Development](/categories/api)

devsarfo/youtube
================

Laravel Package for the Youtube Data API v3

v1.1.1(1y ago)138↓100%MITPHPPHP ^7.0|^8.0

Since Jan 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/devsarfo/laravel-youtube)[ Packagist](https://packagist.org/packages/devsarfo/youtube)[ RSS](/packages/devsarfo-youtube/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (1)Versions (3)Used By (0)

Laravel Youtube
===============

[](#laravel-youtube)

Laravel Package for the Youtube Data API v3

Requirements
------------

[](#requirements)

- PHP 7.0 or higher
- Laravel 5.1 or higher
- API key from [Google Console](https://console.developers.google.com)

Installation
------------

[](#installation)

Run in console below command to download package to your project:

```
composer require devsarfo/youtube
```

Configuration
-------------

[](#configuration)

In `/config/app.php` add YoutubeServiceProvider (Laravel &lt; 5.5):

```
DevSarfo\Youtube\YoutubeServiceProvider::class,
```

Do not forget to add also Youtube facade there (Laravel &lt; 5.5):

```
'Youtube' => DevSarfo\Youtube\Facades\Youtube::class,
```

Publish config settings:

```
$ php artisan vendor:publish --provider="DevSarfo\Youtube\YoutubeServiceProvider"

```

Set your Youtube API key in the file:

```
/config/youtube.php
```

Or in the .env file

```
YOUTUBE_API_KEY = KEY
```

Or you can set the key programmatically at run time :

```
Youtube::setApiKey('KEY');
```

Usage
-----

[](#usage)

```
// use DevSarfo\Youtube\Facades\Youtube;

// Return an STD PHP object
$video = Youtube::getVideoInfo('n5Xp3M8lvzw');

// Get multiple videos info from an array
$videoList = Youtube::getVideoInfo(['n5Xp3M8lvzw','Attb0hi2Dpk']);

// Get localized video info
$video = Youtube::getLocalizedVideoInfo('Attb0hi2Dpk', 'en');

// Get multiple videos related to a video
$relatedVideos = Youtube::getRelatedVideos('Attb0hi2Dpk');

// Get comment threads by videoId
$commentThreads = Youtube::getCommentThreadsByVideoId('Attb0hi2Dpk');

// 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', 'UCTiH4aWrbJ1u0UVmjH_rUxQ', 40);

// List videos in a given channel, return an array of PHP objects
$videoList = Youtube::listChannelVideos('UCTiH4aWrbJ1u0UVmjH_rUxQ', 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('UCTiH4aWrbJ1u0UVmjH_rUxQ');

// Get playlist by ID, return an STD PHP object
$playlist = Youtube::getPlaylistById('PLKreJXVT4v6w2PinY');

// 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('UCTiH4aWrbJ1u0UVmjH_rUxQ');

// 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('UCTiH4aWrbJ1u0UVmjH_rUxQ');

// Retrieve video ID from original YouTube URL
$videoId = Youtube::parseVidFromURL('https://www.youtube.com/watch?v=iFshb7ZsXAg');
// result: iFshb7ZsXAg
```

Validation Rules
----------------

[](#validation-rules)

```
// use DevSarfo\Youtube\Rules\ValidYoutubeVideo;

// Validate a YouTube Video URL
[
    'youtube_video_url' => ['bail', 'required', new ValidYoutubeVideo]
];
```

You can use the bail rule in conjunction with this in order to prevent unnecessary queries.

Basic Search Pagination
-----------------------

[](#basic-search-pagination)

```
// 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']);
```

The pagination above is quite basic. Depending on what you are trying to achieve you may want to create a recursive function that traverses the results.

Manual Class Instantiation
--------------------------

[](#manual-class-instantiation)

```
// Directly call the YouTube constructor
$youtube = new Youtube(config('YOUTUBE_API_KEY'));

// By default, if the $_SERVER['HTTP_HOST'] header is set,
// it will be used as the `Referer` header. To override
// this setting, set 'use-http-host' to false during
// object construction:
$youtube = new Youtube(config('YOUTUBE_API_KEY'), ['use-http-host' => false]);

// This setting can also be set after the object was created
$youtube->useHttpHost(false);
```

Run Unit Test
-------------

[](#run-unit-test)

If you have PHPUnit installed in your environment, run:

```
$ phpunit
```

If you don't have PHPUnit installed, you can run the following:

```
$ composer update
$ ./vendor/bin/phpunit
```

Format of returned data
-----------------------

[](#format-of-returned-data)

The returned JSON is decoded as PHP objects (not Array). Please read the ["Reference" section](https://developers.google.com/youtube/v3/docs/) of the Official API doc.

Youtube Data API v3
-------------------

[](#youtube-data-api-v3)

- [Youtube Data API v3 Doc](https://developers.google.com/youtube/v3/)
- [Obtain API key from Google API Console](https://console.developers.google.com)

Donation
--------

[](#donation)

If you find this project to be of use to you please consider buying me a cup of tea :)

[![paypal](https://camo.githubusercontent.com/e1ff554a09e8e92bef25abc553ff05b88f45afd695877cf12f3a46558ef65b2e/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e61746543435f4c472e676966)](https://paypal.me/BernardSarfoTwumasi)

Credits
-------

[](#credits)

Built on code from Madcoda's [php-youtube-api](https://github.com/madcoda/php-youtube-api) and Mustapha Alaouy's [Youtube](https://github.com/alaouy/Youtube/). .

###  Health Score

26

—

LowBetter than 46% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~68 days

Total

2

Last Release

725d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4427f1c6360881b26a4c99137175c96caf7ae61996e66d63c0229c7265f5ec43?d=identicon)[devsarfo](/maintainers/devsarfo)

---

Top Contributors

[![devsarfo](https://avatars.githubusercontent.com/u/20325487?v=4)](https://github.com/devsarfo "devsarfo (4 commits)")

---

Tags

apilaravelvideoyoutubedevsarfo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/devsarfo-youtube/health.svg)

```
[![Health](https://phpackages.com/badges/devsarfo-youtube/health.svg)](https://phpackages.com/packages/devsarfo-youtube)
```

###  Alternatives

[alaouy/youtube

Laravel PHP Facade/Wrapper for the Youtube Data API v3

8091.2M8](/packages/alaouy-youtube)[madcoda/php-youtube-api

PHP wrapper for the Youtube Data API v3

4311.2M8](/packages/madcoda-php-youtube-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
