Retrieving Video ID from youtube-api JSON playlist

3.5k Views Asked by At

So here's a bit of code to output a playlist as an list so I can style it up and load a player. At the moment I just want some raw I can see which data I need.

$playlist_id = "bKIMlqJAz6GSqtm2x2rFQuIa8NKEhCTa";
$url = "https://gdata.youtube.com/feeds/api/playlists/".$playlist_id."?v=2.1&alt=json";
$data = json_decode(file_get_contents($url),true);
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);

echo "Playlist Name: ".$info["title"]['$t'].'<br/>';
echo "Number of Videos (".$nVideo."):<br/>";

for($i=0;$i<$nVideo;$i++){
echo "ID: ".$video[$i]['id']['$t'].'<br/>'; // this line causing problems
echo "Name: ".$video[$i]['title']['$t'].'<br/>';
echo "Link: ".$video[$i]['link'][0]['href'].'<br/>';
echo "Image: <img src='".$video[$i]['media$group']['media$thumbnail'][1]['url']."' /><br />";
}

All works fine, doodey doodey. Except I can't get hold of the ID.

The ID returns for example this bunch of garbage; ID:tag:youtube.com,2008:playlist:bKIMlqJAz6GSqtm2x2rFQuIa8NKEhCTa:PLLdE_Ow1C_Zv2bmDW9Yj8Foj4dKO3C4BJthoFmebTFUs

instead of LFRRGYNOUqs which is correctly returned by the link and title.

I have search Google's documentation it appears that $video[$i]['id'] is the object I need.

So why is returning that string and not my video ID?

1

There are 1 best solutions below

0
On

According to the api for the xml part (the "json" version is basically the same): "The tag specifies a URN that uniquely and permanently identifies a feed or feed entry."

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_id

In your case, you could use $video[$i]['media$group']['yt$videoid']['$t'].

You might want to use "jsonc" instead of "json", it returns the data formatted specifically for json instead of doing a transcription of the xml version. i.e. https://gdata.youtube.com/feeds/api/playlists/bKIMlqJAz6GSqtm2x2rFQuIa8NKEhCTa?v=2.1&alt=jsonc

There you'll notice that each "item" in the playlist has an id corresponding to a feed entry and a video section with its own id.