• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 654
  • Last Modified:

PHP RSS problems

I have an RSS feed and an RSS Parser, yet for some reason my page come up blank. I was wondering if someone could point out what I am doing wrong here.

Thanks!

----------------------------------------------------------------------[rss code]-----------------------------------------------------------------------------------------

 <?xml version="1.0" encoding="ISO-8859-1" ?>
- <rss version="2.0">
- <channel>
  <title>Gamespot Recent Updates [News]</title>
  <category>News</category>
  <description>Latest updates from Gamespot.com</description>
  <language>en-us</language>
  <copyright>Copyright &copy;1995-2004 CNET Networks, Inc. All rights reserved.</copyright>
  <link>http://www.gamespot.com</link>
  <ttl>20</ttl>
  <lastBuildDate>Fri, 01 Oct 2004 23:40:11 -0700</lastBuildDate>
  <generator>GameSpot</generator>
- <image>
  <title>Gamespot.com</title>
  <url>http://img.gamespot.com/gamespot/shared/promos/misc/gs_logo.gif</url>
  <link>http://www.gamespot.com/</link>
  <width>135</width>
  <height>40</height>
  </image>
- <item>
  <pubDate>Fri, 1 Oct 2004 18:16:40 GMT</pubDate>
  <title>Rumor Control: Half-Life 2 plot thickens, Molyneux's mea culpa [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109442.html?part=rss&tag=gs_news&subj=6109442</link>
  <description>To say nothing of the next Xbox Next spec that's sure to be packed into the upcoming console.</description>
  <guid>http://www.gamespot.com/6109442</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 18:16:40 GMT</pubDate>
  <title>Rumor Control: Half-Life 2 plot thickens, Molyneux's mea culpa [Xbox]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109442.html?part=rss&tag=gs_news&subj=6109442</link>
  <description>To say nothing of the next Xbox Next spec that's sure to be packed into the upcoming console.</description>
  <guid>http://www.gamespot.com/6109442</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 17:38:15 GMT</pubDate>
  <title>Vampire soundtrack locked and loaded [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109476.html?part=rss&tag=gs_news&subj=6109476</link>
  <description>Ministry's Al Jorgensen headlines, contributes original tune to upcoming PC title.</description>
  <guid>http://www.gamespot.com/6109476</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 17:21:25 GMT</pubDate>
  <title>Sam and Max team forms new studio [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109473.html?part=rss&tag=gs_news&subj=6109473</link>
  <description>Telltale Games opens doors in northern California, pledges to resurrect adventure game genre. GameSpot has an exclusive Q&A with CEO Dan Connors.</description>
  <guid>http://www.gamespot.com/6109473</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 17:15:09 GMT</pubDate>
  <title>Halo 2 'basically done' [Xbox]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109470.html?part=rss&tag=gs_news&subj=6109470</link>
  <description>Bungie to gamers: Halo 2 is gold. Time to break out the bubbly (and sushi) in Redmond.</description>
  <guid>http://www.gamespot.com/6109470</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 14:02:49 GMT</pubDate>
  <title>Secret of the Silver Earring ships [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109427.html?part=rss&tag=gs_news&subj=6109427</link>
  <description>Ubisoft's Sherlock Holmes crime-investigation adventure in stores now.</description>
  <guid>http://www.gamespot.com/6109427</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 12:44:01 GMT</pubDate>
  <title>Majesco's first two DS titles to ship next year [DS]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109415.html?part=rss&tag=gs_news&subj=6109415</link>
  <description>Shin'en-developed Nanostray joins previously announced Moonlight Fables for release in 2005.</description>
  <guid>http://www.gamespot.com/6109415</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 12:36:16 GMT</pubDate>
  <title>Codename: Panzers rolls out into retail [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109410.html?part=rss&tag=gs_news&subj=6109410</link>
  <description>CDV's World War II real-time combat strategy game available in stores now. UPDATE: Check out GameSpot's review.</description>
  <guid>http://www.gamespot.com/6109410</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 11:53:12 GMT</pubDate>
  <title>SuperPower 2 powers off to factory [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109402.html?part=rss&tag=gs_news&subj=6109402</link>
  <description>DreamCatcher's game of geopolitical savvy and world domination has gone gold.</description>
  <guid>http://www.gamespot.com/6109402</guid>
  </item>
- <item>
  <pubDate>Fri, 1 Oct 2004 10:50:33 GMT</pubDate>
  <title>MP Men of Valor demo now premiering on DLX [PC]</title>
  <link>http://www.gamespot.com/news/2004/10/01/news_6109256.html?part=rss&tag=gs_news&subj=6109256</link>
  <description>Two maps from VU Games and 2015's Vietnam-set shooter have made their debuts on GameSpot's download service.</description>
  <guid>http://www.gamespot.com/6109256</guid>
  </item>
  </channel>
  </rss>

----------------------------------------------------------------[/rss code]------------------------------------------------------------------------------------

here is the parser code, this may be something stupid...

----------------------------------------------------------------[parser code]----------------------------------------------------------------------------------

<?php
/*
======================================================================
lastRSS usage DEMO 2
----------------------------------------------------------------------
This example shows, how to
    - create lastRSS object
    - set transparent cache
    - get RSS file from URL
    - access and show fields of the result
======================================================================
*/

// include lastRSS
include "./lastRSS.php";

// Create lastRSS object
$rss = new lastRSS;

// Set cache dir and cache time limit (1200 seconds)
// (don't forget to chmod cahce dir to 777 to allow writing)
$rss->cache_dir = './temp';
$rss->cache_time = 1200;

// Try to load and parse RSS file
if ($rs = $rss->get('http://www.landofbob.com/newstest.php')) {
    // Show website logo (if presented)
    if ($rs[image_url] != '') {
        echo "<a href=\"$rs[image_link]\"><img src=\"$rs[image_url]\" alt=\"$rs[image_title]\" vspace=\"1\" border=\"0\" /></a><br />\n";
        }
    // Show clickable website title
    echo "<big><b><a href=\"$rs[link]\">$rs[title]</a></b></big><br />\n";
    // Show website description
    echo "$rs[description]<br />\n";
    // Show last published articles (title, link, description)
    echo "<ul>\n";
    foreach($rs['items'] as $item) {
        echo "\t<li><a href=\"$item[link]\">".$item['title']."</a><br />".$item['description']."</li>\n";
        }
    echo "</ul>\n";
    }
else {
    echo "Error: It's not possible to reach RSS file...\n";
}
?>

----------------------------------------------------------------[/parser code]----------------------------------------------------------------------------------

0
astrohelp
Asked:
astrohelp
  • 12
  • 12
  • 7
  • +2
4 Solutions
 
hernst42Commented:
Try setting the error_reporting to report all errors in you script after the beginninh <?php

<?php
error_reporting(E_ALL)
/*

As the code for the last RSS is not available we can not test it by our self to see what the error might be. So setting the error level might show you where the problem is.
0
 
php-webdesignCommented:
what if you put:

<?
error_reporting(E_ALL);
?>

on the top of your page. This way we can see if it generates error messages and debug this app easier
0
 
astrohelpAuthor Commented:
Parse error: parse error in /home/bob/landofbob.com/html/demo.php on line 17

include "./lastRSS.php";

that is line 17...
I am pretty new to php, but that looks like a valid include statement, that file is in the same directory.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
astrohelpAuthor Commented:
www.landofbob.com/demo.php

this is the link to the code in question.
0
 
astrohelpAuthor Commented:
<?php
/*
 ======================================================================
 lastRSS 0.9

 Simple yet powerfull PHP class to parse RSS files.

 by Vojtech Semecky, webmaster @ webdot . cz

 Latest version, features, manual and examples:
       http://lastrss.webdot.cz/

 ----------------------------------------------------------------------
 LICENSE

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License (GPL)
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 To read the license please visit http://www.gnu.org/copyleft/gpl.html
 ======================================================================
*/

/**
* lastRSS
* Simple yet powerfull PHP class to parse RSS files.
*/
class lastRSS {
      // -------------------------------------------------------------------
      // Public properties
      // -------------------------------------------------------------------
      var $default_cp = 'UTF-8';
      var $CDATA = 'nochange';
      var $cp = '';
      var $items_limit = 0;
      var $stripHTML = False;
      var $date_format = '';

      // -------------------------------------------------------------------
      // Private variables
      // -------------------------------------------------------------------
      var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs');
      var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
      var $imagetags = array('title', 'url', 'link', 'width', 'height');
      var $textinputtags = array('title', 'description', 'name', 'link');

      // -------------------------------------------------------------------
      // Parse RSS file and returns associative array.
      // -------------------------------------------------------------------
      function Get ($rss_url) {
            // If CACHE ENABLED
            if ($this->cache_dir != '') {
                  $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
                  $timedif = @(time() - filemtime($cache_file));
                  if ($timedif < $this->cache_time) {
                        // cached file is fresh enough, return cached array
                        $result = unserialize(join('', file($cache_file)));
                        // set 'cached' to 1 only if cached file is correct
                        if ($result) $result['cached'] = 1;
                  } else {
                        // cached file is too old, create new
                        $result = $this->Parse($rss_url);
                        $serialized = serialize($result);
                        if ($f = @fopen($cache_file, 'w')) {
                              fwrite ($f, $serialized, strlen($serialized));
                              fclose($f);
                        }
                        if ($result) $result['cached'] = 0;
                  }
            }
            // If CACHE DISABLED >> load and parse the file directly
            else {
                  $result = $this->Parse($rss_url);
                  if ($result) $result['cached'] = 0;
            }
            // return result
            return $result;
      }

      // -------------------------------------------------------------------
      // Modification of preg_match(); return trimed field with index 1
      // from 'classic' preg_match() array output
      // -------------------------------------------------------------------
      function my_preg_match ($pattern, $subject) {
            // start regullar expression
            preg_match($pattern, $subject, $out);

            // if there is some result... process it and return it
            if(isset($out[1])) {
                  // Process CDATA (if present)
                  if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag)
                        $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
                  } elseif ($this->CDATA == 'strip') { // Strip CDATA
                        $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
                  }

                  // If code page is set convert character encoding to required
                  if ($this->cp != '')
                        //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
                        $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
                  // Return result
                  return trim($out[1]);
            } else {
            // if there is NO result, return empty string
                  return '';
            }
      }

      // -------------------------------------------------------------------
      // Replace HTML entities &something; by real characters
      // -------------------------------------------------------------------
      function unhtmlentities ($string) {
            $trans_tbl = get_html_translation_table (HTML_ENTITIES);
            $trans_tbl = array_flip ($trans_tbl);
            return strtr ($string, $trans_tbl);
      }


      // -------------------------------------------------------------------
      // Parse() is private method used by Get() to load and parse RSS file.
      // Don't use Parse() in your scripts - use Get($rss_file) instead.
      // -------------------------------------------------------------------
      function Parse ($rss_url) {
            // Open and load RSS file
            if ($f = @fopen($rss_url, 'r')) {
                  $rss_content = '';
                  while (!feof($f)) {
                        $rss_content .= fgets($f, 4096);
                  }
                  fclose($f);

                  // Parse document encoding
                  $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
                  // if document codepage is specified, use it
                  if ($result['encoding'] != '')
                        { $this->rsscp = $result['encoding']; } // This is used in my_preg_match()
                  // otherwise use the default codepage
                  else
                        { $this->rsscp = $this->default_cp; } // This is used in my_preg_match()

                  // Parse CHANNEL info
                  preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
                  foreach($this->channeltags as $channeltag)
                  {
                        $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
                        if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
                  }
                  // If date_format is specified and lastBuildDate is valid
                  if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
                                    // convert lastBuildDate to specified date format
                                    $result['lastBuildDate'] = date($this->date_format, $timestamp);
                  }

                  // Parse TEXTINPUT info
                  preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
                        // This a little strange regexp means:
                        // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
                  if (isset($out_textinfo[2])) {
                        foreach($this->textinputtags as $textinputtag) {
                              $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
                              if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
                        }
                  }
                  // Parse IMAGE info
                  preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
                  if (isset($out_imageinfo[1])) {
                        foreach($this->imagetags as $imagetag) {
                              $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
                              if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
                        }
                  }
                  // Parse ITEMS
                  preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
                  $rss_items = $items[2];
                  $i = 0;
                  $result['items'] = array(); // create array even if there are no items
                  foreach($rss_items as $rss_item) {
                        // If number of items is lower then limit: Parse one item
                        if ($i < $this->items_limit || $this->items_limit == 0) {
                              foreach($this->itemtags as $itemtag) {
                                    $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
                                    if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
                              }
                              // Strip HTML tags and other bullshit from DESCRIPTION
                              if ($this->stripHTML && $result['items'][$i]['description'])
                                    $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
                              // Strip HTML tags and other bullshit from TITLE
                              if ($this->stripHTML && $result['items'][$i]['title'])
                                    $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
                              // If date_format is specified and pubDate is valid
                              if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
                                    // convert pubDate to specified date format
                                    $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
                              }
                              // Item counter
                              $i++;
                        }
                  }

                  $result['items_count'] = $i;
                  return $result;
            }
            else // Error in opening return False
            {
                  return False;
            }
      }
}

?>
0
 
php-webdesignCommented:
try:

include("./lastRSS.php");
0
 
astrohelpAuthor Commented:
nothing different

I have added the code for lastRSS if it will help any.
0
 
php-webdesignCommented:
no it won't help me...

it is an include problem

replace the line

include "./lastRSS.php";

with

include('./lastRSS.php');

OR

include('lastRSS.php');
0
 
astrohelpAuthor Commented:
neither of those is working at all...
0
 
php-webdesignCommented:
then tell me what error you get now
0
 
astrohelpAuthor Commented:
same as before

Parse error: parse error in /home/bob/landofbob.com/html/demo.php on line 17
0
 
Diablo84Commented:
I don't think the problem is the include line, the syntax is fine.

Are there any lines of code before your include line?

Also this line is potentially errornous

if ($rs = $rss->get('http://www.landofbob.com/newstest.php')) {

unless im mistaken it shold be the comparrison operator (== instead of =)
0
 
astrohelpAuthor Commented:
i think you are right about the == but I changed it and nothing changed. There is no code before that, everything is displayed there.
0
 
Diablo84Commented:
forget that misread the code, still looking through script.
0
 
Diablo84Commented:
Just tested your code and it works fine without error for me

The only thing i see is a notice because of slightly incorrect array index referencing

eg.

if ($rs[image_url] != '') {

should be

if ($rs['image_url'] != '') {

And the same within the echo statements

0
 
Diablo84Commented:
Referencing the arrays correctly this section of code should be:

// Try to load and parse RSS file
if ($rs = $rss->get('test.rss')) {
    // Show website logo (if presented)
    if ($rs[image_url] != '') {
        echo "<a href=\"".$rs['image_link']."\"><img src=\"".$rs['image_url']."\" alt=\"".$rs['image_title']."\" vspace=\"1\" border=\"0\" /></a><br />\n";
        }
    // Show clickable website title
    echo "<big><b><a href=\"".$rs['link']."\">".$rs['title']."</a></b></big><br />\n";
    // Show website description
    echo $rs['description']."<br />\n";
    // Show last published articles (title, link, description)
    echo "<ul>\n";
    foreach($rs['items'] as $item) {
        echo "\t<li><a href=\"".$item['link']."\">".$item['title']."</a><br />".$item['description']."</li>\n";
        }
    echo "</ul>\n";
    }
else {
    echo "Error: It's not possible to reach RSS file...\n";
}


(you can optionally use curly brackets instead of concatenating them)

However that would not likely cause a parse error, as i said above i do not see the same problem as you are experiencing so unless there is some extra code in one of your files that shouldnt be there causing the problem...
0
 
Diablo84Commented:
Missed one, this line too

if ($rs['image_url'] != '') {
0
 
php-webdesignCommented:
shouldn't

    if ($rs[image_url] != '') {

be

    if ($rs['image_url'] != '') {

Just to be sure :)
0
 
php-webdesignCommented:
but strange that it gives an parse error... maybe something in PHP.INI set wrong

magic_quotes or something...
0
 
hernst42Commented:
I took your code and pated it into a php-file and it works without any parse-error. Which PHP-version are you using.
0
 
astrohelpAuthor Commented:
4.1.2
0
 
hernst42Commented:
Even with that version I don't get an parse error with the original code you posted. The code you posted is from the file demo.php ??
0
 
Diablo84Commented:
Same here as i said which is why i wonder if there was some stranded code in the file that shouldn't be there.
0
 
Diablo84Commented:
I tihkn i know what it is, its this line in the RSS file, PHP is trying to parse it

<?xml version="1.0" encoding="ISO-8859-1" ?>

You could work around it by turning short tags off in your php.ini file (preferable) or prehaps somethng like this

<?php
echo "<?";
echo 'xml version="1.0" encoding="ISO-8859-1"';
echo "?>";
?>
0
 
Diablo84Commented:
Note: drew this conclusion by viewing the file you was trying to run through the RSS parser (http://www.landofbob.com/newstest.php) which has an error on line 1 - which would be the XML tag
0
 
php-webdesignCommented:
but http://www.landofbob.com/demo.php gives a different line error
0
 
astrohelpAuthor Commented:
yeah, its still choking on the include, even after the xml workaround
0
 
Diablo84Commented:
hmm, have you modified the lastRSS class at all from what is shown above? i would imagine not
and there is definitely nothing else in demo.php other then what you have posted here?

Trying to think of something else that you may have done which would explain why its working fine here and not for you.
0
 
Diablo84Commented:
Meant to say, other fix would be giving the RSS file a non php extension if it doesnt need to be parsed.
0
 
astrohelpAuthor Commented:
I havent made any changes, but I will copy all of the info here into my pages. Everything I have is as you see it here. I will also try giving it a non php extension.
0
 
Diablo84Commented:
>>  Meant to say, other fix would be giving the RSS file a non php extension if it doesnt need to be parsed.

Damn thats weird, i tried posting that awhile back directly after the note about the xml tag but EE froze so i gave up and now it mysteriously appears out of context. One of those days.

Don't worry about that anyway, it doesn't seem to be the issue as i first thought.
0
 
astrohelpAuthor Commented:
I have tried copying all of the code that I had posted previously and reloading it all, no luck at all... this is going to drive me nuts!
0
 
frugleCommented:
demo.php and lastRSS.php code works as pasted in top of question.

returns:

<big><b><a href=""></a></b></big><br />
<br />
<ul>
</ul>

so, it's doing something strange...


copied rss code REMOVING THE ERRONEOUS -'s from the start of each <rss> <image> and <item> and it works perfectly, including pulling the gamespot logos out.

Mike
0
 
frugleCommented:
also - having your RSS feed as .php is parsing the file - trying to execute <?xml version="1.0" encoding="ISO-8859-1" ?> as php code (as Diablo Mentioned)

Mike
0
 
astrohelpAuthor Commented:
I accepted your comments as joint answers because you all helped me track down the problem in some way. As it turns out, I was trying to access the rss file as if it was in a file directory, I needed to just give it the straight URL.

if ($rs = $rss->get('http://www.landofbob.com/newstest.php')) {

thank you all for your help, I knew this problem would be a stupid error on my part, but I hope this can help someone else later!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 12
  • 12
  • 7
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now