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 ...<img src="http://feeds.feedburner.com/~r/GreenpeaceNews/~4/435979702" height="1" width="1"/></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.
< and > 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