Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

Displaying the Most Popular Youtube Videos with PHP

During one of my random dreams of how to become an internet millionaire, I thought about displaying the top youtube videos on an aesthetically pleasing backdrop.

While I’m not feeling that artsie right now, I did code together a quick script to do this using SimpleXML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
function embed($src)
{
    return '<object width="560" height="340"><param name="movie" value="' .
            $src . '"></param><param name="allowFullScreen" value="true"></param>
            <param name="allowscriptaccess" value="always"></param><embed src="'
.
            $src . '" type="application/x-shockwave-flash" allowscriptaccess="always"
            allowfullscreen="true" width="560" height="340"></embed></object>'
;
}

$xml = simplexml_load_file('http://gdata.youtube.com/feeds/base/standardfeeds/most_popular');

echo '<h1>Most Popular YouTube Videos</h1>';
print '<h2>' . date('F jS, Y', strtotime($xml->updated)) . '</h2>';

echo '<table>';
foreach ($xml->entry as $entry) {
    $link = str_replace('watch?v=', 'v/', $entry->link[0]['href']);
    echo '<tr><td>';

    echo embed($link) .  '<br />';
    echo "<a href='$entry->link[0]['href']'>{$entry->title}</a>";

    echo "</td></tr>";
}
echo '</table>';

Real brief analysis: The function embed() is used to generate the code to embed the video. The first step is loading the file with simplexml_load_file(). (Do keep in mind to make sure you can remotely open a file in php.ini). Next, I generated the title and created a header based on the last time the xml file was updated. The final step was to create a table and generate a loop to display each embedded video with a link to its source (see usage of the embed() function there? yeah….).

This entry was posted in PHP, xml and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>