In youtube
How do i get the id of the latest uploaded video (the one that comes in the url like v=....) of a channel to which Im subscribed, for embeded
im using php in my server side
Asked
Active
Viewed 1.5k times
4 Answers
9
As of April 20, 2015, it looks like the answer mentioned above may no longer work. Here's an example using a YouTube Channel ID (can be found in the source of a Channel page.)
<?php
$id = NULL;
$channel_id = 'someChannelID';
$xml = simplexml_load_file(sprintf('https://www.youtube.com/feeds/videos.xml?channel_id=%s', $channel_id));
if (!empty($xml->entry[0]->children('yt', true)->videoId[0])){
$id = $xml->entry[0]->children('yt', true)->videoId[0];
}
echo $id; // Outputs the video ID.

Greg Brendel
- 111
- 1
- 3
-
1Could you detail the "may no longer work" sentence? Did something change to the YT API? – Cristik Apr 20 '15 at 19:56
-
I did not do much research yet, but I was using the method posted and my latest video was being returned as the following: https://youtube.com/devicesupport which points to this page: https://support.google.com/youtube/answer/6098135?hl=en – Greg Brendel Apr 20 '15 at 20:52
-
Hi, How can i get latest 5 YouTube Video for my slider..plz help me. – Ye Htun Z Jul 20 '16 at 07:05
8
Here's a example using the YouTube RSS feeds, simplexml_load_file
, parse_url
, and parse_str
.
<?php
$id = NULL;
$username = 'YouTube';
$xml = simplexml_load_file(sprintf('http://gdata.youtube.com/feeds/base/users/%s/uploads?alt=rss&v=2&orderby=published', $username));
if ( ! empty($xml->channel->item[0]->link) )
{
parse_str(parse_url($xml->channel->item[0]->link, PHP_URL_QUERY), $url_query);
if ( ! empty($url_query['v']) )
$id = $url_query['v'];
}
echo $id; // Outputs the video ID.

Francois Deschenes
- 24,816
- 4
- 64
- 61
2
Here's how to do it via the YouTube API once you set up your API Key.
<?php
$channel_id = 'someChannelId';
$api_key = 'yourAPIKey';
$json_url="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=".$channel_id."&key=".$api_key;
$json = file_get_contents($json_url);
$listFromYouTube=json_decode($json);
$id = $listFromYouTube->items[0]->snippet->resourceId->videoId;
echo $id; // Outputs the video ID.
Generating your API Key:
- Goto - https://console.developers.google.com
- Create a new project
- Select "APIs & auth"
- Select "APIs"
- Select "Youtube Data API v3" and enable it
- Select "Credentials"
- Create new Key, select browser and then press create (don't enter anything in the text box)

Greg Brendel
- 111
- 1
- 3
-
In the above the $channel_id should actually be the playlist id which can be found by going to and looking in the related playlists section.https://www.googleapis.com/youtube/v3/channels?id={channel_id}&key={api_key}&part=snippet,contentDetails,statistics – Alan Hollis Oct 01 '15 at 10:12
0
Same here as with Greg Brendel. YT did make some changes: "As we upgrade the YouTube Data API to bring more features, we’ll begin shutting down the old version on April 20, 2015. This will result in the current YouTube app not working on certain device models from 2012 and older."
https://support.google.com/youtube/answer/6098135?p=yt_devicesupport&hl=en&rd=1