Home » PHP

To fetch the latest youtube video from user channel using php

22 April 2013 No Comment
Share on Facebook

If you need to create a widget & showcase the latest video uploaded in youtube channel to your users. Can be in your website or advertisements or anywhere, it is now possible with youtube API.

youtube-fetch-latest-video

Youtube feeds

The API returns a videos feed in response to a request to search for videos. A videos feed contains a maximum of 999 entries. To retrieve search results, send an API request to the following URL:

http://gdata.youtube.com/feeds/projection/videos?v=2

Videos uploaded by a specific user

To request a feed of all videos uploaded by the currently logged-in user, send a GET request to the following URL. This request requires an authentication token, which enables YouTube to identify the user.

https://gdata.youtube.com/feeds/api/users/default/uploads

Learn more:
https://developers.google.com/youtube/2.0/developers_guide_protocol_video_feeds

Using PHP to fetch latest youtube channel video:

We are going to use php code to fetch latest youtube user channel video of “officialpsy”

PHP code to fetch youtube channel:

<?php
$feedURL = 'http://gdata.youtube.com/feeds/api/users/officialpsy/uploads?max-results=1';
$sxml = simplexml_load_file($feedURL);
$i=0;
	foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
      $videoURL = str_replace("&feature=youtube_gdata_player","?feature=player_detailpage",$watch);
	  $videoURL = str_replace("watch?v=","embed/",$videoURL);
      ?>
      <div class="videoitem">
        <div class="videothumb">
        	<iframe width="470" height="360" src="<?php echo $videoURL; ?>" frameborder="0" allowfullscreen></iframe>
        </div>
        <div class="videotitle">
            <h3><a href="<?php echo $watch; ?>" class="watchvideo"><?php echo $media->group->title; ?></a></h3>
            <p><?php echo $media->group->description; ?></p>
        </div>
      </div>
<?php } ?>

Output:
code-output

Conclusion:
In this sample snippet code we have seen how to get the lastest youtube video from user channel.

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner