Reading XML/RSS files

Ask about a PHP problem here.
Post Reply
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

What's wrong here?

Post by Fidbeck »

<?php
function php_net_feed($limit){
	$output = array();
	$feed_url = 'http://php.net/feed.atom';
	$feed = simplexml_load_file($feed_url);
	$x = 1;

	foreach ($feed->entry as $item) {
		if ($x <= $limit){
			$title = $item->title;
			$url = $item->id;

			$output[] = array(
				'title' = $title, 
				'url' = $url
			);
		}
		$x++;
	}

	return $output;
}

$feed = php_net_feed(5);

?>
<ul>

	<?php
		foreach ($feed as $item) {
			echo '<li>', $item['title'], '</li>';
		}
	?>

</ul>
Last edited by jacek on Thu Nov 29, 2012 1:35 am, edited 1 time in total.
Reason: code tags...
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Reading XML/RSS files

Post by Fidbeck »

so far i've managed to extract title and date but how do I extract the body?
This is the example
<entry xmlns="http://www.w3.org/2005/Atom" xml:base="entries/2012-11-22-1.xml">
<title>PHP 5.4.9 and PHP 5.3.19 released!</title>
<id>http://www.php.net/archive/2012.php#id2012-11-22-1</id>
<published>2012-11-22T17:54:29+01:00</published>
<updated>2012-11-22T17:54:29+01:00</updated>
<category term="frontpage" label="PHP.net frontpage news"/>
<category term="releases" label="New PHP release"/>
<link href="http://www.php.net/index.php#id2012-11-22-1" rel="alternate" type="text/html"/>
<link href="http://www.php.net/archive/2012.php#id2012-11-22-1" rel="via" type="text/html"/>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>The PHP development team announces the immediate availability of PHP 5.4.9 and PHP 5.3.19. These releases fix over 15 bugs. All users of PHP are encouraged to upgrade to PHP 5.4.9, or at least 5.3.19.</p>
<p>For source downloads of PHP 5.4.9 and PHP 5.3.19 please visit our <a href="/downloads.php">downloads page</a>, Windows binaries can be found on <a href="http://windows.php.net/download/">windo ... d/</a>.</p>
<p>The list of changes are recorded in the <a href="/ChangeLog-5.php">ChangeLog</a>.</p>
</div>
</content>
Last edited by jacek on Thu Nov 29, 2012 1:37 am, edited 1 time in total.
Reason: code tags...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Reading XML/RSS files

Post by jacek »

I usually work these things out by looking at the result of the parsing, it's not always obvious from the XML directly.

Try adding
print_r($item);
inside your loop, and maybe set $x to 1 for testing, and see what you get :)
Image
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Reading XML/RSS files

Post by Fidbeck »

I get an huge array... :S

this is what I want
  [p] => Array
                        (
                            [0] => The PHP development team announces the immediate availability of PHP 5.4.9 and PHP 5.3.19. These releases fix over 15 bugs. All users of PHP are encouraged to upgrade to PHP 5.4.9, or at least 5.3.19.
                            [1] => For source downloads of PHP 5.4.9 and PHP 5.3.19 please visit our , Windows binaries can be found on .
                            [2] => The list of changes are recorded in the .
                        )
but how?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Reading XML/RSS files

Post by jacek »

If that array is in the variable $data then you want $data['p'][0]
Image
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Reading XML/RSS files

Post by Fidbeck »

I want 0 1 and 2, are the different <p> in the <content> section, but nevermind, I already have the case solved
Changed the code a bit.
Thanks for your help
Post Reply