Page 1 of 1

What's wrong here?

Posted: Wed Nov 28, 2012 10:44 pm
by Fidbeck
[syntax=php]<?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>[/syntax]

Reading XML/RSS files

Posted: Thu Nov 29, 2012 1:34 am
by Fidbeck
so far i've managed to extract title and date but how do I extract the body?
This is the example

[syntax=text]<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&lt;/id&gt;
<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&quot; rel="alternate" type="text/html"/>
<link href="http://www.php.net/archive/2012.php#id2012-11-22-1&quot; 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/">windows.php.net/download/</a>.</p>
<p>The list of changes are recorded in the <a href="/ChangeLog-5.php">ChangeLog</a>.</p>
</div>
</content>[/syntax]

Re: Reading XML/RSS files

Posted: Thu Nov 29, 2012 1:39 am
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
[syntax=php]print_r($item);[/syntax]
inside your loop, and maybe set $x to 1 for testing, and see what you get :)

Re: Reading XML/RSS files

Posted: Thu Nov 29, 2012 1:42 am
by Fidbeck
I get an huge array... :S

this is what I want
[syntax=text] [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 .
)[/syntax]

but how?

Re: Reading XML/RSS files

Posted: Thu Dec 06, 2012 12:41 am
by jacek
If that array is in the variable $data then you want $data['p'][0]

Re: Reading XML/RSS files

Posted: Fri Dec 07, 2012 5:46 pm
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