2011-11-09 90 views
0

即時通訊嘗試循環使用PHP的一些XML數據。目前它只是帶回一個,但我希望它帶回所有的數據。PHP中的SimpleXML循環

<?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>"; 
?> 
+0

你的循環在哪裏? – hakre

+0

要小心,如果xml很大,你可能想看看XMLReader(http://us3.php.net/manual/en/class.xmlreader.php),不是很容易使用,但會少得多內存佔用。 –

回答

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>"; 
}