Embedding your latest Youtube video

I was wanting to embed my latest youtube video on my website. What I mean is, I wanted a simple embed script that would always show the latest video I’d uploaded. After discovering that Youtube doesn’t provide a simple way to do this I started googling to see if someone had written a script for it. I had no luck, so I’ve written one.

Taking use of some simple web scraping from the YouTube API, it’ll take the latest video from any Youtuber and embed a player with the what ever size the coder chooses.

It’s written in PHP and is just one function that can be called anywhere on your page. It takes three parameters: the username, the desired video width and the desired video height.

So here’s the first part which I recommend you place at the top of your page, I’ve commented it to help show what’s happening.

Function to embed the latest video from a YouTube Channel.
Parameters: The username of the Youtuber, the desired width of the video, the desired height of the video.
Example call: get_latest_video(‘wormholer693’, ‘640’, ‘385’);
Author: Andy Barratt
Website: http://www.andybarratt.co.uk

function get_latest_video($username, $width, $height)
        //Set the URL of the search we’re using to get the specified user’s uploads.
        $url = ‘http://gdata.youtube.com/feeds/api/users/’.$username.‘/uploads’;
        //Get the results from the url above and store them in a variable.
        $data = file_get_contents($url);
        //Create a filter to find the url of the first video in the retrieved data.
        $filter = "/<media:content url='(.+?)’/";
        //Use the preg_match function to run our filter on our data and return scraped text then the filtered text (video url) in an array.
        //store our videoURL in a variable.
        $videoURL = $results[1];
        //if a video was found
                //generate the code required to embed the video.
                $embedCode = ‘<object width="’.$width.‘" height="’.$height.‘"><param name="movie" value="’.$videoURL.‘"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="’.$videoURL.‘" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="’.$width.‘" height="’.$height.‘"></embed></object>’;
                //echo the embed code
                echo $embedCode;
                echo ‘No Video Found.’;

So the above is the actual function and below is how we call it, we just call the function stating the username, width and height of the video and it’ll do the rest. So just place the following bit where ever you want the video to appear in your code.

get_latest_video(‘wormholer693’, ‘470’, ‘283’);

You can see this code in action on my Videos Page. Feel free to use it, I’d love to know what you think in the comments below.

Published byapbarratt

I own this here website, bow down before your God!

  • Snat

    Had a look at your code, but got a question. Is there an easy way to get the last video rather then the first ?

    • Snat

      Umm, ignore my comment – something isn’t working on my side.

      Thanks for the code :)

  • AnthonyMinnocci

    Thanks so much man! It helped out so much :)

  • Janice Cumberlidge

    Thanks for this, I just used it and it worked first time, which is more than I can say for a lot of free code out there. Great snippit!

  • James

    This works GREAT! The only question I have is if there is a way to make it show the last two or three videos?

  • Will

    It must be me but I cant get this to work. Getting parsing errors everywhere. Are the inverted commas correct. For example, the inverted comma before wormholer693 is different than the one after it??

    • Pat

      You may have to edit all the apostrophes and quotes. Don’t know if this is just a Mac thing but as soon as I did (kept missing a few) it works perfectly so thanks for posting the script!

  • ghostxsilent

    Thanks so much for the regex. I don’t do that enough to be any good at it. I slightly modified the embed code and thought I should post for others.

    //generate the code required to embed the video.

    $embedCode = “”;

    Everything else is the same. Works like a charm!!

  • Mario Bothge

    Hi, it’s great – THX !!! –
    Sorry for my english :-(
    How we can Extrakt the full list of uploaded Videos-
    The list $data give the complete list return ???
    The $return give the last uploaded ???
    how we can all uploaded Videos listet ???
    Please help – THX

  • Mario Bothge

    Hi, sorry for two threads,
    how we can search by Channel ???

  • Dejan Petrović

    Not working anymore. Any idea how to fix it?