Last active
August 29, 2015 14:25
-
-
Save Richzendy/874ab61889b4d8882684 to your computer and use it in GitHub Desktop.
Get youtube playlist using XML RSS with DOM PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Get youtube playlist using XML RSS with DOM PHP | |
* Based on http://bavotasan.com/2010/display-rss-feed-with-php/ | |
*/ | |
?> | |
<style> | |
.y-title h3 { | |
text-align: center; | |
font-size: 16px; | |
} | |
.y-title h3 a { | |
color: #000; | |
} | |
.y-thumb { | |
width: 95%; | |
margin: 0 auto; | |
} | |
.y-desc { | |
font-size: 12px; | |
text-align: justify; | |
padding: 0px 10px; | |
border-bottom: 2px solid; | |
margin-bottom: 20px; | |
} | |
</style> | |
<?php | |
$rss = new DOMDocument(); | |
$rss->load("https://www.youtube.com/feeds/videos.xml?playlist_id=PLYvJ3ZpD6FKIDaoe_uVXHk_2W1Bqg54tv"); | |
$feed = array(); | |
foreach ($rss->getElementsByTagName('entry') as $node) { | |
$item = array ( | |
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, | |
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, | |
'link' => $node->getElementsByTagName('link')->item(0)->getAttribute('href'), | |
'thumb' => $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url'), | |
); | |
array_push($feed, $item); | |
} | |
$y_limit = 5; // Number of youtube videos | |
for($x=0;$x<$y_limit;$x++) { | |
$y_title = str_replace(' & ', ' & ', $feed[$x]['title']); | |
$y_link = $feed[$x]['link']; | |
$y_desc = substr($feed[$x]['desc'], 0, 100); | |
$y_thumb = $feed[$x]['thumb']; | |
echo '<div class="y-title"><h3><a target="blank_" href="'.$y_link.'" title="'.$y_title.'">'.$y_title.'</a></h3></div>'; | |
echo '<div class="y-thumb"><a target="blank_" href="'.$y_link.'" title="'.$y_title.'"><img alt="'.$y_title.'" src="'.$y_thumb.'" /></a></div>'; | |
if($y_desc) { echo '<div class="y-desc">'.$y_desc.'...</div>'; } else { echo '<div class="y-desc"> </div>'; } | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment