Quantcast
Viewing latest article 10
Browse Latest Browse All 22

To fetch the latest youtube video from user channel using php

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.

Image may be NSFW.
Clik here to view.
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:
Image may be NSFW.
Clik here to view.
code-output

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

Related posts:

  1. Getting current page URL using simple PHP script
  2. select random rows / records in mysql and PHP query
  3. Send / Pass values from Flash as3 to PHP without opening new window
  4. PHP online voting system using jquery ajax
  5. PHP multiple file upload script | uploading files code

Viewing latest article 10
Browse Latest Browse All 22

Trending Articles