p3in/l5-youtube-api

PHP wrapper for the Youtube Data API v3

This package's canonical repository appears to be gone and the package has been frozen as a result.

dev-master 2016-06-24 17:08 UTC

This package is auto-updated.

Last update: 2019-03-10 03:12:05 UTC


README

Build Status

A "Fork" of madcoda/php-youtube-api to allow for uploads.

Requirements

  • PHP >=5.5
  • CURL extension in PHP

Install

Edit your composer.json and add this to the require section

"p3in/l5-youtube-api": "dev-master"

then run the following command in your command line shell

$ composer update

Getting started with Laravel 5 project

Install

Add this package to composer.json and run composer update if you haven't already done do.

composer -vvv update

Go to /config/app.php, In providers, add:


P3in\Youtube\YoutubeServiceProvider::class,

In aliases, add:

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

Config

In the command line, please run

php artisan vendor:publish

In your /config/youtube.php, you should fill in your api key

<?php

return array(

    /*
     *  You can set the API Key here
     */

    'key' => 'ABCDEFGHIJKLMNOPQabcdefghijklmnopq'

);

Test it out

To Verify it's working, feel free to create a controller and map to a route, here is a simple controller example

In app/Http/routes.php

Route::get('/', 'YoutubeController@index');

In app/Http/Controllers/YoutubeController.php

namespace App\Http\Controllers;

class YoutubeController extends Controller {

    public function index()
    {
        dd(Youtube::getVideoInfo(Input::get('vid', 'dQw4w9WgXcQ')));
    }

}

(under construction)

Query the Data API

// Return a std PHP object
$video = $youtube->getVideoInfo('rie-hPVJ7Sw');

// Return a std PHP object
$channel = $youtube->getChannelByName('xdadevelopers');

// Return a std PHP object
$channel = $youtube->getChannelById('UCk1SpWNzOs4MYmr0uICEntg');

// Return a std PHP object
$playlist = $youtube->getPlaylistById('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs');

// Return an array of PHP objects
$playlists = $youtube->getPlaylistsByChannelId('UCk1SpWNzOs4MYmr0uICEntg');

// Return an array of PHP objects
$playlistItems = $youtube->getPlaylistItemsByPlaylistId('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs');

// Return an array of PHP objects
$activities = $youtube->getActivitiesByChannelId('UCk1SpWNzOs4MYmr0uICEntg');

// Parse Youtube URL into videoId
$videoId = $youtube->parseVIdFromURL('https://www.youtube.com/watch?v=moSFlvxnbgk');
// result: moSFlvxnbgk

Search

// Search playlists, channels and videos, Return an array of PHP objects
$results = $youtube->search('Android');

// Search only 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', 50);

$results = $youtube->searchAdvanced(array( /* params */ ));

Basic Search Pagination

use P3in\Youtube;

$youtube = new Youtube(array('key' => '/* Your API key here */'));

// Set Default Parameters
$params = array(
    '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 = array(
    'q'             => 'Android',
    'type'          => 'video',
    'part'          => 'id, snippet',
    'maxResults'    => 50
);

// an array to store page tokens so we can go back and forth
$pageTokens   = array();

// 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.

Format of returned data

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

Youtube Data API v3

Contact

For bugs, complain and suggestions please file an Issue here or send email to support@plus3interactive.com :)

License

plus3interactive l5-youtube-api is licensed under the MIT License.