Skip to content
Advertisement

How can I get all videos for a given channel?

I want to be able to retrieve Youtube videos from a Youtube channel.

My goal is to display them on a page. Currently I do it manually by getting an iframe like this:

<iframe width="560" height="315" 
        src="//www.youtube.com/embed/snRkGatdzm0" 
        frameborder="0" allowfullscreen></iframe>

I am aware about Youtube API. I could not find a way to use it to get all videos from a channel.

So for example like the above I would just need the link Id –> snRkGatdzm0.

I prefer to work with php so If I get an array of video Id. I would just loop through them something like this:

foreach ($youtubevideos as $youtubevideo) {
    echo '<iframe src="//www.youtube.com/embed/$youtubevideo"></iframe>'
}

note: the loop might be wrong (I added this fake loop because I know you guys love to see code lol)

Please advice.


I don’t know if I am clear enough, I just want to be able to get all videos links from a channel so I can display them in a page. I don’t want to hardcode it. That’s all to make it short.

Advertisement

Answer

Here is a sample code to achieve what you want using YouTube API 2.0.

This is just quick and dirty way to do it. Keep in mind it will be deprecated soon because YouTube 3.0 is out already.

    $channelName ='YourChannelName';
    $channelURL ="http://gdata.youtube.com/feeds/api/users/$channelName/uploads";
    $xml=simplexml_load_string(file_get_contents($channelURL));


    foreach ($xml->entry as $entry) {
        //Get each link
        $link = $entry->id;
        //Retrieve video id from link
        $link_array = explode('/',$link);
        $video_id = array_pop($link_array);
        //Create the iframe
        echo "<iframe src="//www.youtube.com/embed/$video_id"></iframe>";
    }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement