解析Tumblr XML打破PHP循环

I'm building a website that relies on reading through the XML produced by Tumblr for my photoblog and extracting blog post URLs and image URLs for display and linking.

I've written code in PHP that parses the XML and constructs an outer and inner loops to deal with how Tumblr limits the max number of posts to 50 on any request. The loops should process requests that return batches of 50 URLs and in the final loop only deal with the remainder.

Currently the blog has 53 posts so the code has logic that determines that 2 requests should be read in 2 loops:

In both cases the XML is loaded into the $xml variable and then parsed for specific data that is collected in an inner loop. When executed, it should just produce a list of image numbers, blog post URLs and image URLs through the entire set of loops.

When I execute this code, the loop always breaks after parsing 20 items as long as I read from XML file returned from the request. If I test the code removing references to the XML output and just letting the code run through counting loops with simple output it works.

I don't understand what in the XML file might be causing this -- through there is a subtle change in the some of the XML data after the first 20 posts. Can anyone help?

Here is the code:

 <?php 
        // Get xml file from Tumblr API and load
        $request_url = "http://abstractmueller.tumblr.com/api/read"; 
        $xml = simplexml_load_file($request_url); 
        // Get total number of posts, set max number of posts and calculate loop critical info
        $posts_data = $xml->posts[0]->attributes();
        $posts_max = 50;
        $loop_number = ceil($posts_data['total']/$posts_max);
        $loop_remainder = $posts_data['total']%$posts_max;
        echo '<p>Total posts = '.$posts_data['total'].'</p>';
        echo '<p>Loop number = '.$loop_number.'</p>';
    ?>
    <div>
    <?php
        // Start outer loops to fetch up to 50 post-related image urls each fetch
        for ($outer_loop = 1; $outer_loop <= $loop_number; $outer_loop++){
            $post_start = ($outer_loop - 1) * $posts_max;
            echo '<p>Current loop = '.$outer_loop.'</p>';
            // Branch looping so first branch loops through each batch of 50 images, then use last loop to limit looping to the remainder
            if ($outer_loop < $loop_number) {
                echo '<p>Post start = '.$post_start.'</p>';
                echo '<p>Loop end = '.($post_start + $posts_max-1).'</p>';
                $request_url = 'http://abstractmueller.tumblr.com/api/read?start='.$post_start.'&num='.$posts_max;
                echo '<p>XML URL '.$request_url.'</p>'; 
                // Get post URLs and image URLs in batches of 50
                for ($img_num = ($post_start); $img_num < ($post_start + $posts_max); $img_num++) {
                    $blog_url = $xml->posts->post[$img_num]->attributes(); 
                    $photo_url = $xml->posts->post[$img_num]->{'photo-url'}[2];
                    echo '<p>Image '.$img_num.' Blog URL '.$blog_url['url'].' Image URL '.$photo_url.'</p>';
                }
            } else {
                echo '<p>Post start = '.$post_start.'</p>';
                echo '<p>Loop end = '.($post_start + $loop_remainder - 1).'</p>';
                $request_url = 'http://abstractmueller.tumblr.com/api/read?start='.$post_start.'&num='.$loop_remainder;
                echo '<p>XML URL '.$request_url.'</p>'; 
                // Get post URLs and image URLs up to the total remainder
                for ($img_num = $post_start; $img_num <= $loop_remainder + $posts_max; $img_num++) {
                    $blog_url = $xml->posts->post[$img_num]->attributes(); 
                    $photo_url = $xml->posts->post[$img_num]->{'photo-url'}[2];
                    echo '<p>Image '.$img_num.' Blog URL '.$blog_url['url'].' Image URL '.$photo_url.'</p>';
                }
            }                            
        }
    ?>
    <div>