PHP Classes

Laravel Youtube: Retrieve details about videos using Youtube API

Recommend this page to a friend!

  Author Author  
Picture of Roman Kozin
Name: Roman Kozin <contact>
Classes: 7 packages by
Country: Ukraine Ukraine
Innovation award
Innovation award
Nominee: 3x

Winner: 2x


  Detailed description   Download Download .zip .tar.gz  
This package can retrieve details about videos using Youtube API.

It provides a service provider class that allows access to your Laravel application to several functions that send HTTP requests to Youtube Data API v3.

Currently, it can:

- Get the details about a specific video or multiple videos

- Get a list of videos related to another video

- Get the comment threads about a video

- Get a list of popular videos in a country

- Search videos by keyword

- Search or list videos of a channel

- Get the details of the channel, like the name and the ID

- Get the details of a playlist, like the videos, ID, and activities

- Parse the URL of the video page to extract the video ID

- Retrieve lists of videos paginated with a limited number of videos per page

Details

Youtube

Travis Youtube Build

Laravel PHP Facade/Wrapper for the Youtube Data API v3 ( Non-OAuth )

Requirements

Looking for Youtube Package for either of these: PHP 5, Laravel 5.0, Laravel 4? Visit the php5-branch

Installation

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

composer require alaouy/youtube

Configuration

In /config/app.php add YoutubeServiceProvider:

KielD01\Youtube\YoutubeServiceProvider::class,

Do not forget to add also Youtube facade there:

'Youtube' => KielD01\Youtube\Facades\Youtube::class,

Publish config settings:

$ php artisan vendor:publish --provider="KielD01\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

// use KielD01\Youtube\Facades\Youtube;


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

// Get multiple videos info from an array
$videoList = Youtube::getVideoInfo(['rie-hPVJ7Sw','iKHTawgyKWQ']);

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

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

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

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

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

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

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

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

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.

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

The returned JSON is decoded as PHP objects (not Array). Please read the "Reference" section of the Official API doc.

Youtube Data API v3

Credits

Built on code from Madcoda's php-youtube-api.


  Classes of Roman Kozin  >  Laravel Youtube  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: Laravel Youtube
Base name: laravel-youtube
Description: Retrieve details about videos using Youtube API
Version: -
PHP version: 5
License: The PHP License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Libraries Frameworks and libraries of cooperating classes View top rated classes
Group folder image Web services Web data clipping, SOAP or XML-RPC clients and servers View top rated classes
Group folder image Video Movies and video data generation and manipulation View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 2 directories)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageconfig (1 file)
Files folder imageFacades (1 file)
  Plain text file Youtube.php Class Class source
  Plain text file YoutubeServiceProvider.php Class Class source

  Files folder image Files  /  src  /  config  
File Role Description
  Accessible without login Plain text file youtube.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file Youtube.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file YoutubeTest.php Class Class source

Download Download all files: laravel-youtube.tar.gz laravel-youtube.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 2 directories)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageconfig (1 file)
Files folder imageFacades (1 file)
  Plain text file Youtube.php Class Class source
  Plain text file YoutubeServiceProvider.php Class Class source

  Files folder image Files  /  src  /  config  
File Role Description
  Accessible without login Plain text file youtube.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file Youtube.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file YoutubeTest.php Class Class source

Download Download all files: laravel-youtube.tar.gz laravel-youtube.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.