PHP to RSS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robertico

    PHP to RSS

    I'd like to create a simple RSS feed using some PHP code, e.g. displaying
    the remote IP and Host name.
    Who can help me with this ?

    Thanks in advance,

    Robertico



  • Curtis.DanielN@gmail.com

    #2
    Re: PHP to RSS

    You will need to set the header :
    <?php
    header("Content-Type: text/xml");
    ?>
    the header has to be set in order to specify that you will be writing
    xml.
    When you want to print something like the ip do this:
    <ipaddress><?ph p echo $_SESSION["IPADDRESS"];?></ipaddress>

    After the header is set and you escape from php you'll just be creating
    regular xml tags and printing using php when you need to....I hope that
    helps

    Daniel

    Comment

    • Andreas

      #3
      Re: PHP to RSS

      Here is the script that I used to generate RSS 2.0 feed using PHP.
      <?php

      $href="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
      $logo="http://www.embedworld. com/images/top_logo.gif";
      $title="Embedwo rld download center";
      $link="http://en.embedworld.c om";
      $copyright="Cop yright (c) 2004-2006 Embedworld.com All Rights
      Reserved.";
      $language="en";
      $title="Embedwo rld.com";
      $description="E mbedworld.com, Embedded unit the world!";

      // Don't edit the content below
      if(function_exi sts("getimagesi ze")) {
      list($width, $height, $type, $attr) = getimagesize($l ogo);
      } else {
      list($width, $height)= array(88, 31);
      }

      $lastBuildDate= date("r");

      if(1) {

      // HTTP/1.1
      header("Cache-Control: no-store, no-cache, must-revalidate");
      header("Cache-Control: post-check=0, pre-check=0", false);

      // HTTP/1.0
      header("Pragma: no-cache");

      header('Content-type: text/xml');
      print "<?xml version=\"1.0\" encoding=\"gb23 12\"?>";

      print <<<HTML

      <rss version="2.0">

      <channel>

      <title>$title </title>

      <description>$d escription</description>

      <copyright>$cop yright</copyright>

      <link>$link</link>

      <language>$lang uage</language>

      <generator>Embe dworld RSS Feed Generator 2.0</generator>

      <lastBuildDate> $lastBuildDate</lastBuildDate>

      <ttl>60</ttl>

      <image>

      <title>$title </title>

      <width>$width </width>

      <height>$height </height>

      <link>$link</link>

      <url>$logo</url>

      </image>

      HTML;

      $dbtype="mysql" ;

      include('./adodb471-1/adodb/adodb.inc.php') ;
      if($dbtype == "mysql") {
      $dbhost="localh ost";
      $dbname="test";
      $dbport="3306";
      $dbuser="root";
      $dbpass="";

      $dsn = "$dbtype://$dbuser:$dbpass @$dbhost/$dbname";
      $db = NewADOConnectio n($dsn);
      if (!$db) die("Connection failed");
      }

      if($dbtype == "access") {
      //Access test here
      $access="D:\\db _data.mdb";
      $dbuser="";
      $dbpass="";
      $db =& ADONewConnectio n('access');
      $dsn = "Driver={Micros oft Access Driver
      (*.mdb)};Dbq=$a ccess;Uid=$dbus er;Pwd=$dbpass; ";
      $db->Connect($dsn );
      if (!$db) die("Connection failed");
      }

      ///*
      if(isset($query ) && !empty($query)) {

      $sql="select * from `db_class` where nsort ='down'";
      $ADODB_FETCH_MO DE = ADODB_FETCH_ASS OC;
      $rs = $db->Execute($sql );
      if ($rs) {
      while (!$rs->EOF) {
      $data=$rs->GetAssoc();
      foreach ($data as $i =$value) {
      $sql2="select * from `db_sort` where id =$i";
      $ADODB_FETCH_MO DE = ADODB_FETCH_ASS OC;
      $rs2 = $db->Execute($sql2) ;
      if ($rs2) {
      while (!$rs2->EOF) {
      $data2=$rs2->GetAssoc();
      foreach ($data2 as $i2 =$value2) {

      print <<<HTML
      <item>

      <title>ClassI D: $i =$value </title>

      <descriptionS ub ClassID: $i2 =$value2</description>

      </item>

      HTML;
      }
      $rs2->MoveNext();
      }
      }
      }
      $rs->MoveNext();
      }
      } else {
      print "<errorErro r <br /></error>\n";
      } // End of else if ($rs)

      unset($data, $sql, $i, $value);

      print <<<HTML
      </channel>
      </rss>
      HTML;

      exit(0);
      }




      $cnt=0;

      $sql="select * from `down` where $condition";
      // print "<SQL[$sql] </SQL>\n";
      $ADODB_FETCH_MO DE = ADODB_FETCH_ASS OC;
      // $db->debug = true;
      $rs = $db->Execute($sql );
      if ($rs) {
      $data=$rs->GetRows();

      foreach ($data as $dt) {

      print <<<HTML
      <item>

      <title>$name</title>
      <link>$url</link>
      <description>$r emark</description>
      <pubDate>$tim </pubDate>
      </item>

      HTML;

      } // End of foreach
      } else {
      print "Error<br />\n";
      }// End of else if ($rs)

      print <<<HTML
      </channel>
      </rss>
      HTML;

      } // End ...

      ?>

      Hopefully it is helpful to you!

      Regards,
      Signal
      From: http://en.embedworld.com
      Curtis.DanielN@ gmail.com wrote:
      You will need to set the header :
      <?php
      header("Content-Type: text/xml");
      ?>
      the header has to be set in order to specify that you will be writing
      xml.
      When you want to print something like the ip do this:
      <ipaddress><?ph p echo $_SESSION["IPADDRESS"];?></ipaddress>
      >
      After the header is set and you escape from php you'll just be creating
      regular xml tags and printing using php when you need to....I hope that
      helps
      >
      Daniel

      Comment

      • Robertico

        #4
        Re: PHP to RSS

        Thanks Daniel and Andreas.

        Must i save it as php or xml.
        If it's xml. are there some modifications needed for my httpd.conf ?

        Regards,

        Robertico


        Comment

        • NC

          #5
          Re: PHP to RSS

          Robertico wrote:
          >
          Must i save it as php or xml.
          PHP.

          Cheers,
          NC

          Comment

          • Robertico

            #6
            Re: PHP to RSS

            I can't get i to work.
            When i saved it as php file, the php code isn't executed.

            <?xml version="1.0"?>
            <rss version="2.0">
            <channel>
            <title>MyTitl e</title>
            <link>http://www.mydomain.co m/</link>
            <description>MY Description</description>
            <item>
            <title>YourIP </title>
            <link>http://www.mydomain.co m/yourip.php</link>
            <description>Yo ur current IP</description>
            <ipaddress><?ph p echo getenv ("REMOTE_ADDR") ;?></ipaddress>
            </item>
            </channel>
            </rss>

            So, i need i little more help.

            Regards,

            Robertico


            Comment

            • andrey@tkachenko.kiev.ua

              #7
              Re: PHP to RSS

              I can't get i to work.
              When i saved it as php file, the php code isn't executed.
              >
              <?xml version="1.0"?>
              <rss version="2.0">
              <channel>
              <title>MyTitl e</title>
              <link>http://www.mydomain.co m/</link>
              <description>MY Description</description>
              <item>
              <title>YourIP </title>
              <link>http://www.mydomain.co m/yourip.php</link>
              <description>Yo ur current IP</description>
              <ipaddress><?ph p echo getenv ("REMOTE_ADDR") ;?></ipaddress>
              </item>
              </channel>
              </rss>
              What extension have your file?
              ..xml? or .php?

              Support your server PHP or NOT?

              Comment

              • NC

                #8
                Re: PHP to RSS

                Robertico wrote:
                >
                I can't get i to work.
                When i saved it as php file, the php code isn't executed.
                >
                <?xml version="1.0"?>
                <rss version="2.0">
                <channel>
                <title>MyTitl e</title>
                <link>http://www.mydomain.co m/</link>
                <description>MY Description</description>
                <item>
                <title>YourIP </title>
                <link>http://www.mydomain.co m/yourip.php</link>
                <description>Yo ur current IP</description>
                <ipaddress><?ph p echo getenv ("REMOTE_ADDR") ;?></ipaddress>
                </item>
                </channel>
                </rss>
                OK, let's make a checklist:

                1. Does your server execute code in other PHP scipts?

                2. Is this script saved with a *.php extension?

                3. What exactly do you see in the output that makes
                you think PHP code is not executed?

                4. What happens if you replace

                <?xml version="1.0"?>

                with

                <?php
                header('Content-Type: text/xml');
                echo '<?xml version="1.0"?> ';
                ?>

                Cheers,
                NC

                Comment

                • Robertico

                  #9
                  Re: PHP to RSS

                  1. Does your server execute code in other PHP scipts?
                  - Yes
                  2. Is this script saved with a *.php extension?
                  - Yes
                  3. What exactly do you see in the output that makes you think PHP code is
                  not executed?
                  - My RSS reader (SharpReader, Feedreader) doesn't display the IP address
                  (php code). It displays only the description (tag).
                  4. What happens if you replace
                  >
                  <?xml version="1.0"?>
                  >
                  with
                  >
                  <?php
                  header('Content-Type: text/xml');
                  echo '<?xml version="1.0"?> ';
                  ?>
                  - The same result in my RSS reader. (SharpReader, Feedreader)
                  - But when open it in a browser, it's xml formatted with the current IP
                  address between the <ipaddresstag s.
                  So the php code is executed.
                  My RSS reader (SharpReader, Feedreader) doesn't display the IP address
                  (php code). It displays only the description (tag).
                  It seems that it can't handle the <ipaddresstag .

                  Regards,

                  Robertico


                  Comment

                  • NC

                    #10
                    Re: PHP to RSS

                    Robertico wrote:
                    >
                    - The same result in my RSS reader. (SharpReader, Feedreader)
                    - But when open it in a browser, it's xml formatted with the current
                    IP address between the <ipaddresstag s.
                    So the php code is executed.
                    My RSS reader (SharpReader, Feedreader) doesn't display the
                    IP address (php code). It displays only the description (tag).
                    It seems that it can't handle the <ipaddresstag .
                    No, and you shouldn't expect it to. This is not an element defined in
                    any RSS specification I know of...

                    Cheers,
                    NC

                    Comment

                    • Robertico

                      #11
                      Re: PHP to RSS

                      No, and you shouldn't expect it to. This is not an element defined in
                      any RSS specification I know of...
                      It was an example from Daniel (Curtis.DanielN @gmail.com ) in his reply.
                      Now I added a <categorytag for the 'discription' and placed the php-code
                      between the <descriptiontag .
                      Now everything works fine :-)

                      Thanks anyway !!

                      Robertico


                      Comment

                      • Andreas

                        #12
                        Re: PHP to RSS

                        haha, very good!

                        Robertico wrote:
                        No, and you shouldn't expect it to. This is not an element defined in
                        any RSS specification I know of...
                        >
                        It was an example from Daniel (Curtis.DanielN @gmail.com ) in his reply.
                        Now I added a <categorytag for the 'discription' and placed the php-code
                        between the <descriptiontag .
                        Now everything works fine :-)
                        >
                        Thanks anyway !!
                        >
                        Robertico

                        Comment

                        Working...