Click here to Skip to main content
15,888,124 members
Articles / Programming Languages / PHP
Tip/Trick

YouTube API for PHP

Rate me:
Please Sign up or sign in to vote.
4.78/5 (2 votes)
5 Dec 2014CPOL 22K   440   1   3
Youtube API for PHP to feed a video list from a particular channel without Oauth, this API only needs a Channel ID and Channel name, no need to generate API key access and so on, simply it's fair enough.

Image 1

Introduction

Youtube API for PHP to feed a video list from a particular channel without Oauth, this API only needs a Channel ID and Channel name. Youtube API is to demonstrate how to show video in our website without 0auth and API Key.

Background

This API is created for only a website to display a video list from their channel without 0auth & API key!

Using the Code

Basic PHP codes are used to feed a video list.

PHP
//Enter Your Channel Name
$channel_name="xyz123";
//Enter Your ID
$channel="Uhjfhdkjhfdf454dfde";


// Google Gdata feed url is used to fetch videos from channel.
//
//->results=[2] this is used to how much videos present in feed list         
                $feedURL=urlencode('https://gdata.youtube.com/feeds/api/videos?author=
                '.$channel.'&start-index=1&max-results=2&orderby=published');
                if (@simplexml_load_file($feedURL))
                {
                    $sxml = simplexml_load_file($feedURL);//converts feedlist into XML
                    $counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
                    $total = $counts->totalResults;
                    foreach ($sxml->entry as $entry) {
                    $media = $entry->children('http://search.yahoo.com/mrss/');
                    $attrs = $media->group->player->attributes();
                    $watch = $attrs['url']; //URL of the video       
                    $yt = $media->children('http://gdata.youtube.com/schemas/2007');
                    $attrs = $yt->duration->attributes();
                    $length = $attrs['seconds'];  
                    $minute=floor($length/60);
                    $second=$length-$minute*60;        
                    $gd = $entry->children('http://schemas.google.com/g/2005');
                    if ($gd->rating) {
                          $attrs = $gd->rating->attributes();
                          $rating = $attrs['average'];
                    } else {
                          $rating = 0;
                    }
                    $attrs = $media->group->thumbnail[1]
                    ->attributes();//generate thumbnail for each video.
                    
              
...

Points of Interest

Actually, it loads the video feed list from Google gdata URL. This simple API is to demonstrate how to show video in our website without 0auth.

History

  • Version 1.0 - It just shows the feed list only, no link to the original video (Youtube URL) on each video in feed
  • Version 1.0.1 - Link to the individual video is updated in a feed list

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
Workaholic!
Sr Software Developer.
Web Developer and Desktop Application Development.

Comments and Discussions

 
Questionvideo list by keyword instead of channel Pin
Member 1319255910-May-17 13:06
Member 1319255910-May-17 13:06 
SuggestionCode alterations Pin
Chubby Ninja12-Dec-14 6:08
Chubby Ninja12-Dec-14 6:08 
GeneralRe: Code alterations Pin
Prabakaran T12-Dec-14 17:47
professionalPrabakaran T12-Dec-14 17:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.