Hello everybody,
I'm trying to write a php script to read rss feeds. Since I began, I found it is much harder than I expected, because the xml structures are very different, and they need to be interpreted differently everytime.

I'm using

simplexml_load_file()

to read the xml code easily and using if() conditions in order to automate different interpretations for different xml structures. I'm still a neophite to web programming, and I hope this is a correct approach, at least.

I'm having some problems for reading a feed in particular, the one from Greenpeace.
Structure is as follows:

<channel>
  <title>Greenpeace News</title>
  //some more tags I don't need
    <item>
      <title>Armada of activists descends on Kingsnorth </title>
      <link>http://feeds.feedburner.com/~r/GreenpeaceNews/~3/435979702/armada-kingsnorth-coal291008</link>
      <description>A nine-boat protest armada ...&lt;img src="http://feeds.feedburner.com/~r/GreenpeaceNews/~4/435979702" height="1" width="1"/&gt;</description>
      //some more tags I don't need
    </item>
    //some more items
</channel>

As you can see, the image tag is inside the "description" tags.
&lt; and &gt; are used instead of < and >.
What happens when the code is read by my script and reproduced on the page is:
- image is not visible.
- last line of text has a greater lineheight than the others.

Here is the page where you can see the result (it uses a javascript for accordion effect, by dezinerfolio):
http://me.itgoesgreen.org/rss/
(just look at the Greenpeace section, the other ones are working)

the php script I use, is:

<?php
	function curl_session($url){
		$feed = 0;
		$ch = curl_init($url);
		$fp = fopen("rss.xml", "w");
		curl_setopt($ch, CURLOPT_FILE, $fp);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_exec($ch);
		curl_close($ch);
		fclose($fp);
	}
	
	function feed($url,$maxfeeds,$source=''){
		curl_session($url);
		$xml = simplexml_load_file('rss.xml');
		if($source!=''){
			print '<div id="'.$source.'-header" class="accordion_headings" >'.$source.'</div>';
			print '<div id="'.$source.'-content">';
			print '<div class="accordion_content">';
		} else {
			print "<ul>";
		}
		if (('rss.xml') &&($xml->entry)){
			foreach ($xml->entry as $item){
				if ($feed < $maxfeeds){
					if($source!=''){
						print '<div class="post">';
						print '<h2 class="title"><a href="';
					} else {
						print '<li><a href="';
					}
					foreach($item->link[0]->attributes() as $a => $b){
						if($a=="href"){ echo $b; }
					}
					print '">';
					print "$item->title</a>";
					if($source!=''){
						print "</h2>";
						print '<div class="entry">';
						print "<p>$item->description</p>";
						print "</div></div>";
					} else {
						print "</li>";
					}
					$feed += 1;
				}
			}
		} else if (('rss.xml') && ($xml->channel->item)){
			foreach ($xml->channel->item as $item){
				if ($feed < $maxfeeds){
					if($source!=''){
						print '<div class="post">';
						print '<h2 class="title"><a href="';
					} else {
						print '<li><a href="';
					}
					print $item->link.'">';
					print "$item->title</a>";
					if($source!=''){
						print "</h2>";
						print '<div class="entry">';
						print "<p>$item->description</p>";
						print "</div></div>";
					} else {
						print "</li>";
					}
					$feed += 1;
				}
			}
		} else {
			echo "<p>i feed rss non sono disponibili al momento.</p>";
		}
		if($source!=''){
			print "</div></div>";
		} else {
			print "<ul>";
		}
	}
?>

curl session is used because my host doesn't allow to use the simplexml_load_file() with external urls, for secutity issues.

Does anyone can explain me why image is not shown, even if it appears in the resulting page's code? Why the lineheight error takes place, as well? I guess both the problems are linked, but I don't understand why and how...

If you need further infos, please let me know.

Thanks everybody in advance, and sorry for having been so long,
s

Sorry, I just figured out that the error depends on the css of the page I use to display the rss and the <img> is not actually a picture, but something like a 1x1 pixel marker.

My fault, then. How can I delete the thread? I wouldn't pollute this forum with useless questions...

Sorry again,
s

Don't delete your thread dude.
It's very informational ,exebuting a rss or xml is not so easy.
I found it interesting and good stuff.
Would like to read more of your threads.
Thanks

Hi everybody and glad, Jimmy03, to hear that this thread can be interesting to you.
I'll keep on threading here about my experiments with rss, then.
Here's my next problem: how can I exclude specific tags while reading an xml file with simplexml_load_file()?
Here's an example (http://me.itgoesgreen.org/rss), where I've added feeds from Direct2Dell.
Now, those feeds contain a lot of tags I do not want, such as <img> <strong> <sup> and styling <div>.
Let's say I just want to keep the <p> tags and have unformatted plain text for all the rest. Is there a way to do that without parsing? Maybe via simplexml_load_file() and if() conditions?
And a very last question (by now, at least):
which is the fastest way to have the text cropped after a fixed number of words or, better, after the closing of the first <p> tag?

Thanks everybody for your attention.
Best,
s

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.