Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

YouTube API for PHP

4.78/5 (2 votes)
5 Dec 2014CPOL 22.5K   443  
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)