Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Display YouTube videos from a users RSS Feed with PHP

David Carr

Tutorials PHP & MySQL

Table of Contents

Using an RSS feed you can load videos from a users feed, this can be as simple as a list of videos to embed them.

Demo

The demo loads the 10 latest videos from my YouTube channel.

To get started first create a function to use curl which will load the rss feed to pass to it.

function curl_get_contents($url) {
    // Initiate the curl session
    $ch = curl_init();
    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    // Removes the headers from the output
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //set timeout
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    // Execute the curl session
    $output = curl_exec($ch);
    // Close the curl session
    curl_close($ch);
    // Return the output as a variable
    return $output;
}

Now call the function and passing the address to an RSS feed, change the channel_id to the id of the channel you want to use. To find the id of a channel:

Go to the YouTube channel you want to track
View the page’s source code
Look for the following text: channel-external-id
Get the value for that element (it’ll look something like UCBcRF18a7Qf58cCRy5xuWwQ
Credit for the above steps from https://danielmiessler.com/blog/rss-feed-youtube-channel

If you don't have curl installed you can use file_get_contents instead.

After adding the feed path to a variable convert it to xml by using SimpleXmlElement

$feed = curl_get_contents("https://www.youtube.com/feeds/videos.xml?channel_id=UC7QziMG7v407vgQLUzyFG_A");
$xml = new SimpleXmlElement($feed);

At this point you might want to see what the $xml variable contains using print_r is a good way of displaying its contents:

 print_r($xml);

 Now the $xml is an object pulling information out is a simple process for instance to display the title of the video you need to go down to that part of the object this is done using -> notations each one moves to the next section $xml->entry moves into the entry section from there you can grab the title:

 echo $xml->entry->title;

This only gets the very first item to get more than one you'll need to loop through all the entries, you can use a foreach here but you have better control using a for loop then you can either loop for all of the entries or limit them to say the first 10, this approach makes more sense they may be a lot of entries to it makes sense to limit them.

If you did want to load all entries you can count them by using:

$count = count($xml->entry);

In this instance, I'm only loading 10 entries to reduce loading times, inside a for loop which says while $i is less than 10 increments $i so once it reaches 20 it will stop incrementing $i and stop.

 for ($i=0; $i < 10; $i++) { 

Inside the loop the first thing I'm doing is looking at the url of the video and I only want the video id part for 
example look at the following url http://www.youtube.com/watch?v=EeX7MOWKi0s&feature=youtube_gdata notice EeX7MOWKi0s that's the part I want to use, it's worth noting this step is only needed if you want to embed the video.

First I collect the url as its an array I've appended () to the end next I'm exploding where the url have an & only looking at the href array element. Then removing the address as that part does not change.

$url = $xml->entry[$i]->link->attributes();
$videourl = explode("&",$url['href']);
$video = str_replace("https://www.youtube.com/watch?v=","",$videourl[0]);

To embed the video its jut a case of using an iframe and passing in the video id:

echo '<p><iframe width="560" height="315" src="https://www.youtube.com/embed/'.$video.'" frameborder="0" allowfullscreen></iframe></p>';

As we are in a loop to display the correct item we need to tell the array, in this case, the entry which part we want to use I do this by passing in $i after the entry so to get all the titles in a loop:

echo $xml->entry[$i]->title;

All of the items are called the same way so I won't go over all of them, I've only chosen a few in my example you could have the author the category etc.

Another one worth noting is the published date I don't like how it looks in the feed to I've customised it by using date and strtotime.

date('jS M Y h:i:s', strtotime($xml->entry[$i]->published))

This has a much nice look to it, plus you can easily change how the date and time will be displayed.

Putting it all together:

<?php

function curl_get_contents($url) {
    // Initiate the curl session
    $ch = curl_init();
    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    // Removes the headers from the output
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //set timeout
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    // Execute the curl session
    $output = curl_exec($ch);
    // Close the curl session
    curl_close($ch);
    // Return the output as a variable
    return $output;
}

$feed = curl_get_contents("https://www.youtube.com/feeds/videos.xml?channel_id=UC7QziMG7v407vgQLUzyFG_A");
$xml = new SimpleXmlElement($feed);

$count = count($xml->entry);
for ($i=0; $i < 10; $i++) { 
    $url = $xml->entry[$i]->link->attributes();
    $videourl = explode("&",$url['href']);
    $video = str_replace("https://www.youtube.com/watch?v=","",$videourl[0]);
    
    echo '<h1>'.$xml->entry[$i]->title.'</h1>';
    echo '<p>Posted on '.date('jS M Y h:i:s', strtotime($xml->entry[$i]->published)).'</p>';
    echo '<p><iframe width="560" height="315" src="https://www.youtube.com/embed/'.$video.'" frameborder="0" allowfullscreen></iframe></p>';
    echo '<p>'.$xml->entry[$i]->content.'</p>';
    echo '<p><a href="'.$url['href'].'">Play on Youtube</a></p>';
}
?>

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.