Code Monkey home page Code Monkey logo

php-youtube-api's Introduction

php-youtube-api

A basic PHP wrapper for the Youtube Data API v3 ( Non-OAuth ). Designed to let devs easily fetch public data (Video, Channel, Playlists info) from Youtube. No 3rd party dependancy. (except PHPUnit) The reason of returning the decoded JSON response directly is that you only need to read the Google API doc to use this library, instead of learning my set of API again (Keep it simple).

Well...actually some parameters are missing in this library, because I don't need them at this point, if you desire a particular feature please file an issue here :)

Currently will not consider adding OAuth endpoints. (those required "authorized request")

Install

Installation is easy with composer just run.

composer require madcoda/php-youtube-api

Usage (Plain PHP project)

require 'vendor/autoload.php';

use Madcoda\Youtube;

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

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

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

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

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

Basic Search Pagination

use Madcoda\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 recurssive function that traverses the results.

Usage (Laravel Project)

Add the dependency in the composer.json, then run

$ composer update

Since the Laravel framework also configured to autoload composer dependencies (in bootstrap/autoload.php), You don't need to add any require or include statements, just use the class

app/controllers/YoutubeController.php

class YoutubeController extends BaseController {

    public function index()
    {
        $youtube = new Madcoda\Youtube(array('key' => '/* Your API key here */'));
	    print_r($youtube->getVideoInfo(Input::get('vid')));
    }

}

If you want to use this class as "Youtube", you can add an aliases, edit the app/config/app.php, Insert the following line:

'aliases' => array(
     //...
    'Youtube' => 'Madcoda\Youtube',
),

Run Unit Test

If you have PHPUnit installed in your environment, just run

$ phpunit

If you don't have PHPUnit installed, you can run this

$ composer update
$ ./vendor/bin/phpunit

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 [email protected] :)

php-youtube-api's People

Contributors

faenir avatar jamesggordon avatar madcoda avatar marvin255 avatar mhor avatar saruman avatar sidhitesh7 avatar wyrihaximus avatar z38 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.