Tag Archives: generate rss

My own RSS for music files from PHP

If you have read the previous post about the Website Music Player than you should be able to understand a little more about what is said in this post.

I was searching for hours and hours the other day and found some snippets of code to generate my music RSS and wow I love IT! I used PHP and xml to generate the RSS format.

I first had thought about how I generated the .xml code from the media player list and then found some things that could help in order to generate the correct information for the RSS feed.

So here is the code that I made that works:
(put this code in a music_rss.php file)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
 
$dir = "mp3";
$url = "http://www.thomasnorberg.com/mediaplayer/playerMultipleMenuPackage";
$email = "tnorberg at thomasnorberg dott com (Thomas Norberg)";
$name = "Tom's Blog";
$n = 0;
$playlist = array();
$fdir = opendir($dir);
while($i = readdir($fdir)) {
   // if a .mp3 string is found, add the file to the array
   if (strpos(strtolower($i),".mp3") !== false){
  	   $playlist[$n] = $i;
           $n++;
      }
}
 
//sort($playlist);
//shuffle($playlist);
//array_multisort($playlist);
 
$ttl = 1440;
$music_rss = "$url/playlist22.php";
header('Content-type: application/xml');
$rss_output = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n";
$rss_output .= "<rss xmlns:atom='http://www.w3.org/2005/Atom/' version='2.0'>\n";
$rss_output .= "<channel>\n\n";
$rss_output .= "<atom:link href='$music_rss' rel='self' type='application/rss+xml' />\n";
$rss_output .= "<title>$name Music RSS</title>\n";
$rss_output .= "<description>Where anything is everything</description>\n";
$rss_output .= "<link>$url/$dir</link>\n";
$rss_output .= "<language>en-us</language>\n";
$rss_output .= "<ttl>$ttl</ttl>\n";
 
for ($j=$n-1; $j>$n-11; $j--) {
   $title = str_replace(".mp3","", $playlist[$j]);
   $title = str_replace("_", " ", $title);
   $other = str_replace(" ", "%20", $playlist[$j]);
   $files = "$dir{$playlist[$j]}";
   $filetime = filectime($files);
   $size = filesize($files);
    $rss_output .= "\n<item>\n\n";
    $rss_output .= "<title>$title</title>\n";
    $rss_output .= "<link>$url/$dir/{$other}</link>\n";  
    $rss_output .= "<pubDate>" . date ("r", $filetime); 
    $rss_output .= "</pubDate>\n";
    $rss_output .= "<enclosure url='$url/$dir/{$other}' type='audio/mpeg' length='$size' />\n";
    $rss_output .= "<guid>$url/$dir/{$other}</guid>\n";  
    $rss_output .= "<author>$email</author>\n";
    $rss_output .= "</item>\n";
}
 
$rss_output .= "</channel>\n</rss>\n";
echo $rss_output;
closedir($fdir);
?>

This is what it should look like. Of course you have to change all the appropriate titles and everything or your won’t have anything to show on your RSS feed, such as the top three lines of code in the music_rss.php file:

1
2
3
4
5
6
<?php
 
$dir = "mp3";
$url = "http://www.thomasnorberg.com/mediaplayer/playerMultipleMenuPackage";
$email = "tnorberg at thomasnorberg dott com (Thomas Norberg)";
$name = "Tom's Blog";

And as always if you have any questions or comments please fell free to ask.

GD Star Rating
loading...
GD Star Rating
loading...