Loading ...
Sorry, an error occurred while loading the content.

Re: Question about RSS...and how to generate file dynamically.

Expand Messages
  • phlbbrtn
    I use the following script for my blog. The blog is entirely PHP with a MySQL backbone. http://www.io.com/~phlbbrtn/blog/ I use a cron job on the server to
    Message 1 of 14 , Jul 28, 2003
    • 0 Attachment
      I use the following script for my blog. The blog is entirely PHP with
      a MySQL backbone. http://www.io.com/~phlbbrtn/blog/

      I use a cron job on the server to run it, but I am sure there are
      other ways of doing it.

      The str_replace's and variables are for translating character entities
      and such.

      The first "echo" line works just fine.

      <?php
      $when = date('D, d M Y');
      $whenz = date('H:i:s');
      echo "<?xml version=\"1.0\"?>\n";
      echo "<rss version=\"0.91\">\n";
      echo "\t<channel>\n";
      echo "\t<link>http://www.io.com/~phlbbrtn/blog/index.php</link>\n";
      echo "\t<title>Phil Burton's Blog</title>\n";
      echo "\t<description>Eating the Universe</description>\n";
      echo "\t<lastBuildDate>$when $whenz CST</lastBuildDate>\n";
      echo "\t<language>en-us</language>\n";
      $db_name = "database_name";
      $table_name = "blog";
      $connection = @mysql_connect("localhost", "username", "password") or
      die(mysql_error());
      $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
      $sql = "SELECT * FROM $table_name ORDER BY num DESC limit 20";
      $result = @mysql_query($sql, $connection) or die(mysql_error());
      while ($row = mysql_fetch_array($result)) {
      $num = $row['num'];
      $title = $row['title'];
      $link = $row['link'];
      $date = $row['date'];
      $time = $row['time'];
      $post = stripslashes($row['post']);
      $urp = str_replace("'","'",$title);
      echo "\n\t<item>\n";
      echo "\t\t <title>$urp</title>\n";
      if ($link == "") {
      echo "\t\t
      <link>http://www.io.com/~phlbbrtn/blog/archive.php?id=$num</link>\n";
      } else { echo "\t\t\t<link>$link</link>\n";
      }
      echo "\t\t <description>";
      $foo = str_replace("&","&",$post);
      $text = str_replace("\r\n\r\n","<br><br>",$foo);
      $text2 = str_replace("\r\n"," ",$text);
      $text3 = str_replace("<","<",$text2);
      $text4 = str_replace(">",">",$text3);
      $gooey = str_replace("\"",""",$text4);
      $emu = str_replace("////","<br>",$gooey);
      $george = str_replace("///","    ",$emu);
      echo DoHTMLEntities($george);
      if ($link != "") {
      echo "<br><br><a
      href="$link">Link</a>\n\t\t\t</description>\n\t</item>";
      } else {
      echo "\n\t\t\t</description>\n\t</item>\n";
      }
      }
      echo "\t</channel>\n</rss>";
      ?>
    • Danny Ayers
      Aren t there any XML libraries available for PHP?
      Message 2 of 14 , Jul 29, 2003
      • 0 Attachment
        Aren't there any XML libraries available for PHP?

        > -----Original Message-----
        > From: phlbbrtn [mailto:phlbbrtn@...]
        > Sent: 29 July 2003 02:33
        > To: RSS2-Support@yahoogroups.com
        > Subject: [RSS2-Support] Re: Question about RSS...and how to generate
        > file dynamically.
        >
        >
        > I use the following script for my blog. The blog is entirely PHP with
        > a MySQL backbone. http://www.io.com/~phlbbrtn/blog/
        >
        > I use a cron job on the server to run it, but I am sure there are
        > other ways of doing it.
        >
        > The str_replace's and variables are for translating character entities
        > and such.
        >
        > The first "echo" line works just fine.
        >
        > <?php
        > $when = date('D, d M Y');
        > $whenz = date('H:i:s');
        > echo "<?xml version=\"1.0\"?>\n";
        > echo "<rss version=\"0.91\">\n";
        > echo "\t<channel>\n";
        > echo "\t<link>http://www.io.com/~phlbbrtn/blog/index.php</link>\n";
        > echo "\t<title>Phil Burton's Blog</title>\n";
        > echo "\t<description>Eating the Universe</description>\n";
        > echo "\t<lastBuildDate>$when $whenz CST</lastBuildDate>\n";
        > echo "\t<language>en-us</language>\n";
        > $db_name = "database_name";
        > $table_name = "blog";
        > $connection = @mysql_connect("localhost", "username", "password") or
        > die(mysql_error());
        > $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
        > $sql = "SELECT * FROM $table_name ORDER BY num DESC limit 20";
        > $result = @mysql_query($sql, $connection) or die(mysql_error());
        > while ($row = mysql_fetch_array($result)) {
        > $num = $row['num'];
        > $title = $row['title'];
        > $link = $row['link'];
        > $date = $row['date'];
        > $time = $row['time'];
        > $post = stripslashes($row['post']);
        > $urp = str_replace("'","'",$title);
        > echo "\n\t<item>\n";
        > echo "\t\t <title>$urp</title>\n";
        > if ($link == "") {
        > echo "\t\t
        > <link>http://www.io.com/~phlbbrtn/blog/archive.php?id=$num</link>\n";
        > } else { echo "\t\t\t<link>$link</link>\n";
        > }
        > echo "\t\t <description>";
        > $foo = str_replace("&","&",$post);
        > $text = str_replace("\r\n\r\n","<br><br>",$foo);
        > $text2 = str_replace("\r\n"," ",$text);
        > $text3 = str_replace("<","<",$text2);
        > $text4 = str_replace(">",">",$text3);
        > $gooey = str_replace("\"",""",$text4);
        > $emu = str_replace("////","<br>",$gooey);
        > $george = str_replace("///","    ",$emu);
        > echo DoHTMLEntities($george);
        > if ($link != "") {
        > echo "<br><br><a
        > href="$link">Link</a>\n\t\t\t</description>\n\t
        > </item>";
        > } else {
        > echo "\n\t\t\t</description>\n\t</item>\n";
        > }
        > }
        > echo "\t</channel>\n</rss>";
        > ?>
        >
        >
        >
        > To unsubscribe from this group, send an email to:
        > RSS2-Support-unsubscribe@yahoogroups.com
        >
        >
        >
        > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        >
        >
      • phlbbrtn
        ... I should have said that the cron job utilizes lynx -source -dump to output to the RSS file. Works just great.
        Message 3 of 14 , Jul 29, 2003
        • 0 Attachment
          --- In RSS2-Support@yahoogroups.com, "phlbbrtn" <phlbbrtn@i...> wrote:

          > I use a cron job on the server to run it, but I am sure there are
          > other ways of doing it.

          I should have said that the cron job utilizes "lynx -source -dump" to
          output to the RSS file. Works just great.
        • Bill Kearney
          ... Um, wget seems to have more options and is less likely to be difficult . There s also curl. My past experiences with lynx lead me to believe it s not the
          Message 4 of 14 , Jul 29, 2003
          • 0 Attachment
            > I should have said that the cron job utilizes "lynx -source -dump" to
            > output to the RSS file. Works just great.

            Um, wget seems to have more options and is less likely to 'be difficult'.
            There's also curl.

            My past experiences with lynx lead me to believe it's not the greatest at
            dealing with programmatic retrieval.

            -Bill
          • phlbbrtn
            ... greatest at ... Sure, whatever. I am just telling what works for me. What works for me is fully automated and requires no ongoing maintenance. I did not
            Message 5 of 14 , Jul 29, 2003
            • 0 Attachment
              >
              > My past experiences with lynx lead me to believe it's not the
              greatest at
              > dealing with programmatic retrieval.
              >
              > -Bill

              Sure, whatever.

              I am just telling what works for me. What "works for me" is fully
              automated and requires no ongoing maintenance.

              I did not know wget or curl could retrieve interpreted PHP output(!?)

              Phil
            • Bill Kearney
              ... Likewise, I found lynx s lack of features found in wget and curl to be a problem as my programming needs grew. Like using authentication and cookies. ...
              Message 6 of 14 , Jul 29, 2003
              • 0 Attachment
                > I am just telling what works for me. What "works for me" is fully
                > automated and requires no ongoing maintenance.

                Likewise, I found lynx's lack of features found in wget and curl to be a problem
                as my programming needs grew. Like using authentication and cookies.

                > I did not know wget or curl could retrieve interpreted PHP output(!?)

                They're capable of resolving URLs via ftp, http and https, how is it a surprise
                to think they wouldn't retrieve pages output from PHP?

                -Bill Kearney
              • phlbbrtn
                I think this pointless discussion is 50 miles wide of the point. You don t like what I use productively every day? That s your right. Someone asked for a way
                Message 7 of 14 , Jul 29, 2003
                • 0 Attachment
                  I think this pointless discussion is 50 miles wide of the point.

                  You don't like what I use productively every day? That's your right.
                  Someone asked for a way to output RSS from PHP, and I gave my own
                  working solution.

                  Do you have anything to add to that discussion or not?


                  --- In RSS2-Support@yahoogroups.com, "Bill Kearney" <ml_yahoo@i...> wrote:

                  > They're capable of resolving URLs via ftp, http and https, how is it
                  a surprise
                  > to think they wouldn't retrieve pages output from PHP?
                  >
                  > -Bill Kearney
                • Wechsler
                  There seems to be some confusion with this issue, so I hope no-one will feel I m attempting to patronise them if I try and cover all bases. First of all, I d
                  Message 8 of 14 , Jul 29, 2003
                  • 0 Attachment
                    There seems to be some confusion with this issue, so I hope no-one will
                    feel I'm attempting to patronise them if I try and cover all bases.

                    First of all, I'd make the point that (IMHO) this isn't really an RSS
                    support issue, but a PHP one.

                    Tim Gray, formula-3000.net wrote:
                    > [...] It occurs to me that as the news stories on my sites are all dynamically
                    > indexed using php, there must be an easier way of updating that RSS feed
                    > when I add a new story, than to open the file in notepad, create a new item
                    > at the top, and delete the oldest one from the bottom, then save the file
                    > and upload it to the server.
                    >
                    > However there doesn't appear to be a way of including any php in the RSS
                    > file.

                    There is, and there isn't. PHP can create a wide range of file types
                    (including HTML, CSS, RSS, images, PDF etc), and for any text-based file
                    type, the PHP can be happily mixed with the text.

                    The reason the RSS standard says nothing about this is that it doesn't
                    care. The HTML standard doesn't, either, because in both cases the
                    receiveing agent (web browser, wget or RSS viewer) never sees the PHP
                    source - this is only ever seen, and parsed, by the server. The
                    receiving agent (client) only ever sees pure HTML (or CSS, gif, png, ...).

                    Therefore, the discussions about whether wget or any other client can
                    'handle PHP' are beside the point - on a properly configured server they
                    will never see it.

                    > Here's how the php works on the new index page of the website:

                    [source cut]

                    And this can be the model of your PHP-RSS script. You're already using
                    long php tags (<?php ), so you shouldn't have a clash with the <?xml ?>
                    declaration. All you need to do is remove all the HTML (the <head> and
                    <body> you should have outside what you pasted, and the <table> <tr>
                    <td> etc) that's print'd below, and replace it with the equivalent RSS
                    tags. See the code provided by 'phlbbrtn' for an example.

                    Your script will, of course, still end in .php (assuming that's how your
                    server is set up), not .rdf or .rss (again, you *can* set up a server to
                    allow this file extensions to include PHP, but that's beyond the scope
                    of this post).

                    Instead, you denote RSS by adding the following as the first line of
                    your PHP script:
                    header("Content-type: application/xml");
                    (is that right, folks?)

                    Beyond that, just code the PHP to output the RSS file you want - this is
                    how all my RSS feeds work (eg: http://rss.ukcycling.net/news/ )

                    Hope that helps!


                    Someone else asked if PHP doesn't have an XML library. As far as I know,
                    it doesn't have specific XML-output code; PHP lacks the wide range of
                    external libraries of, for example, Perl. Therefore the technique tends
                    to be of sequential output rather than any clever object-based stuff.

                    It does (for completeness) have XML-reading code (included in the
                    standard install); on top of this you can use the Magpie RSS library at
                    http://magpierss.sourceforge.net/ ; this is what I use for my
                    RSS-in-a-webpage script ; see: http://www.infinitepenguins.net/rss/

                    I think that covers most of it!

                    All the best,
                    Wechsler

                    --
                    http://www.fromtheshadows.com/ Fingerprint: 542C ADA9 47CE 7AA8 2274
                    PGP Key http://www.fromtheshadows.com/gpg 496A FC43 312C 3D94 2CC8
                    A mind is a terrible thing
                  • Danny Ayers
                    ... Thanks, I was wondering - seemed strange to do all the escaping etc inline in this day and age... ... Right, that sounds rather more like it. Cheers,
                    Message 9 of 14 , Jul 29, 2003
                    • 0 Attachment
                      > Someone else asked if PHP doesn't have an XML library. As far as I know,
                      > it doesn't have specific XML-output code; PHP lacks the wide range of
                      > external libraries of, for example, Perl. Therefore the technique tends
                      > to be of sequential output rather than any clever object-based stuff.

                      Thanks, I was wondering - seemed strange to do all the escaping etc inline
                      in this day and age...

                      > It does (for completeness) have XML-reading code (included in the
                      > standard install); on top of this you can use the Magpie RSS library at
                      > http://magpierss.sourceforge.net/ ; this is what I use for my
                      > RSS-in-a-webpage script ; see: http://www.infinitepenguins.net/rss/

                      Right, that sounds rather more like it.

                      Cheers,
                      Danny.
                    • trachtenberga
                      ... PHP lets you generate XML using DOM. There s no need to hand code it, if you don t want to. Whether you want to for a task like this is another question.
                      Message 10 of 14 , Jul 29, 2003
                      • 0 Attachment
                        --- In RSS2-Support@yahoogroups.com, "Danny Ayers" <danny666@v...> wrote:
                        >
                        > > Someone else asked if PHP doesn't have an XML library. As far as I know,
                        > > it doesn't have specific XML-output code; PHP lacks the wide range of
                        > > external libraries of, for example, Perl. Therefore the technique tends
                        > > to be of sequential output rather than any clever object-based stuff.
                        >
                        > Thanks, I was wondering - seemed strange to do all the escaping etc inline
                        > in this day and age...

                        PHP lets you generate XML using DOM. There's no need to hand code it, if you don't want
                        to. Whether you want to for a task like this is another question.

                        See http://www.php.net/domxml

                        -adam
                      • Danny Ayers
                        ... Thanks, I just did a bit of nosing around and found RAP RDF parser, so if anyone wants to do anything more sophisticated than a reader one possiblity would
                        Message 11 of 14 , Jul 30, 2003
                        • 0 Attachment
                          > PHP lets you generate XML using DOM. There's no need to hand code
                          > it, if you don't want
                          > to. Whether you want to for a task like this is another question.
                          >
                          > See http://www.php.net/domxml

                          Thanks, I just did a bit of nosing around and found RAP RDF parser, so if
                          anyone wants to do anything more sophisticated than a reader one possiblity
                          would be to use XSLT to convert RSS 2.0 (0.91, etc) to RSS 1.0, parse it
                          with RAP and work on it in the RDF model. (I'm using this approach in my own
                          Java app). Could be useful for extensions.

                          Going the other way should be possible too - RDF/XML -> XSLT -> RSS 2.0,
                          though I'm not sure why anyone might want to do that.

                          Cheeers,
                          Danny.
                        • ariusuk
                          ... of ... I had experimented by just modifying the php file - but that was the line I was missing. Thanks to everyone for their help. Tim
                          Message 12 of 14 , Aug 1 1:46 PM
                          • 0 Attachment
                            --- In RSS2-Support@yahoogroups.com, Wechsler <wechsler@i...> wrote:
                            >
                            >
                            > Instead, you denote RSS by adding the following as the first line
                            of
                            > your PHP script:
                            > header("Content-type: application/xml");
                            > (is that right, folks?)
                            >
                            > Beyond that, just code the PHP to output the RSS file you want -


                            I had experimented by just modifying the php file - but that was the
                            line I was missing.

                            Thanks to everyone for their help.

                            Tim
                          Your message has been successfully submitted and would be delivered to recipients shortly.