<?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>
Reading XML/RSS files
What's wrong here?
Last edited by jacek on Thu Nov 29, 2012 1:35 am, edited 1 time in total.
Reason: code tags...
Reason: code tags...
Reading XML/RSS files
so far i've managed to extract title and date but how do I extract the body?
This is the example
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...
Reason: code tags...
Re: Reading XML/RSS files
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
Try adding
print_r($item);inside your loop, and maybe set $x to 1 for testing, and see what you get
Re: Reading XML/RSS files
I get an huge array... :S
this is what I want
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?
Re: Reading XML/RSS files
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
Changed the code a bit.
Thanks for your help