0

im trying to loop through some XML data using PHP. Currently it just only brings back one, but i want it to bring back all the data.

<?php
$request_url = "http://finlay.tumblr.com/api/read";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'regular-title'};
$post = $xml->posts->post->{'regular-body'};
$link = $xml->posts->post['url'];
$small_post = substr($post,0,320);
echo '<h1>'.$title.'</h1>';
echo '<p>'.$small_post.'</p>';
echo "...";
echo "</br><a target=frame2 href='".$link."'>Read More</a>"; 
?>
1
  • Be careful, if the xml is large, you may want to look into XMLReader (us3.php.net/manual/en/class.xmlreader.php), not as easy to use but will be far less of a memory footprint. Commented Nov 9, 2011 at 21:27

1 Answer 1

1
$request_url = "http://finlay.tumblr.com/api/read";
$xml = simplexml_load_file($request_url);
foreach($xml->posts->post as $post)
{
    $title = $post->{'regular-title'};
    $post = $post->{'regular-body'};
    $link = $post['url'];
    $small_post = substr($post,0,320);
    echo '<h1>'.$title.'</h1>';
    echo '<p>'.$small_post.'</p>';
    echo "...";
    echo "</br><a target=frame2 href='".$link."'>Read More</a>"; 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.