The issue is that you need to loop over the posts... you were very close:
Code: Select all
<?php
$request_url = "http://wineinsights.tumblr.com/api/read?type=post&start=0&num=3";
$xml = simplexml_load_file($request_url);
// In the object $xml, [b]$xml->posts->post[/b] is actually an array of posts, which you need to loop over
foreach($xml->posts->post as $oPost) {
$title = $oPost->{'regular-title'};
$post = $oPost->{'regular-body'};
$small_post = substr($post,0,320);
echo '<h1>'.$title.'</h1>';
echo '<p>'.$small_post.'</p>';
echo "…\n\n";
}
Whenever you are working with XML object, and you are not sure what is what, after you load it into
$xml do a
var_dump($xml) (you will want to do View->Source in your browsers, as it will most likely not be too readable since the xml contains html and the browser will try to parse it.
On another note, the XML already contains <P> ... </P> tags around the post, so you do not need to add them when echoing out. However, you are limiting the size of the post, so you will want to check to see if need to add the ending one back.
Code: Select all
foreach($xml->posts->post as $oPost) {
$title = $oPost->{'regular-title'};
$post = $oPost->{'regular-body'};
$small_post = substr($post,0,320);
echo '<h1>'.$title.'</h1>';
echo $small_post;
if (substr($small_post,-4)!='</p>') { echo '</p>'; }
echo "…\n\n";
}