Monday 27 October 2014

Zend Gdata Youtube API - Search Video - View Video Detail

Zend Gdata Youtube API - Search Video - View Video Detail

Youtube API is very useful to show the youtube videos in your website. You don't required any account for search the video. You can filter the video by query string, category, limit no of records & you can see even video detail.
You can also upload OR do changes in uploaded videos but for this you need youtube account.On the behalf of my experience I am presenting some easy example which let you understand the youtube API. These example are testing with zend framework 1.12. Please let me your thoughts on this...



Search Videos by Category
$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();
$query->category = 'Comedy/dog';
//$query->setCategory('Comedy/dog'); OR 
$videos = $yt->getVideoFeed($query);
$this->printVideoFeed($videos);


Search Videos by query string
$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();
$query->videoQuery = 'dogs';
$videos = $yt->getVideoFeed($query);
$this->printVideoFeed($videos);


List Top Rated Videos
$yt = new Zend_Gdata_YouTube();
$videos = $yt->getTopRatedVideoFeed();
$this->printVideoFeed($videos);


List Today's Top Rated Videos
$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();
$query->setTime('today');
$videos = $yt->getVideoFeed($query);
$this->printVideoFeed($videos);


List videos by viewCount & Add limit
$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();
$query->orderBy = 'viewCount';
$query->startIndex = 10;
$query->maxResults = 20;
$videos = $yt->getVideoFeed($query);
$this->printVideoFeed($videos);



Example In Zend Framework 
class IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {

        // action body
    }

    function printVideoFeed($videoFeed) {
        foreach ($videoFeed as $videoEntry) {
            $this->printVideoEntry($videoEntry);
        }
    }

    function printVideoEntry($videoEntry) {
        echo '\n Video: ' . $videoEntry->getVideoTitle();
        echo '\n Video ID: ' . $videoEntry->getVideoId();
        echo '\n Updated: ' . $videoEntry->getUpdated();
        echo '\n Description: ' . $videoEntry->getVideoDescription();
        echo '\n Category: ' . $videoEntry->getVideoCategory();
        echo '\n Tags: ' . implode(", ", $videoEntry->getVideoTags());
        echo '\n Watch page: ' . $videoEntry->getVideoWatchPageUrl();
        echo '\n Flash Player Url: ' . $videoEntry->getFlashPlayerUrl();
        echo '\n Duration: ' . $videoEntry->getVideoDuration();
        echo '\n View count: ' . $videoEntry->getVideoViewCount();
        echo '\n Rating: ' . $videoEntry->getVideoRatingInfo();
        echo '\n Geo Location: ' . $videoEntry->getVideoGeoLocation();
        echo '\n Recorded on: ' . $videoEntry->getVideoRecorded();

        foreach ($videoEntry->mediaGroup->content as $content) {
            if ($content->type === "video/3gpp") {
                echo '
 Mobile RTSP link: ' . $content->url;
            }            
        }
        echo "\n\n";
    }

    function youtubeAction() {
        $yt = new Zend_Gdata_YouTube();
        $query = $yt->newVideoQuery();
        $query->category = 'Comedy/dog';
        //$query->setCategory('Comedy/dog'); OR 
        $videos = $yt->getVideoFeed($query);
        $this->printVideoFeed($videos);
        die("");
    }

}

Execute:/index/youtube


Output:
Video: My 200th Video
Video ID: h23oPnh1WJM
Updated: 2014-10-27T00:30:41.000Z
Description: Please subscribe to my channel and my vlog channel! I make new videos here every Wednesday and make vlogs during my majestical daily life. JennaMarbles Jenna...
Category: Comedy
Tags:
Watch page: https://www.youtube.com/watch?v=h23oPnh1WJM&feature=youtube_gdata_player
Flash Player Url: https://www.youtube.com/v/h23oPnh1WJM?version=3&f=videos&app=youtube_gdata
Duration: 263
View count: 3640139
Rating: Array
Geo Location:
Recorded on:
Mobile RTSP link: rtsp://r6---sn-p5qlsu76.c.youtube.com/CiILENy73wIaGQmTWHV4PuhthxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
Mobile RTSP link: rtsp://r6---sn-p5qlsu76.c.youtube.com/CiILENy73wIaGQmTWHV4PuhthxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp