Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:16:10+00:00 2026-06-18T08:16:10+00:00

Below is a simple example of code to fetch results from a REST XML

  • 0

Below is a simple example of code to fetch results from a REST XML API.

This is just a small portion I have extracted from my real PHP class for this question.

in the API URL which returns an XML document, I am curious about how I could fetch all the results from 1 page and then move on to fetch from the next page.

$this->api_page sets the page that the API returns data from.

Looking at the basic code below using SimpleXMLElement how could I for example return the data from 10 pages or all pages in the API starting at a page number, loading the results for that page and then fetching the next page an moving on.

Right now I am doing it with JavaScript and PHP by passing a Page number in the URL to my script using $_GET['page'] the problem with this is it requires a user to load the page and it’s kind of sloppy.

My real API script will be ran from a Cron job on the server, so with that in mind, how could I fetch all pages?

I ask this question based on this example code below but also because it is a task that I often have to do on other projects and I don’t know a good way of doing this?

<?php

$this->api_url = 'http://api.rescuegroups.org/rest/?key=' .$this->api_key.
'&type=animals&limit=' .$this->api_limit.
'&startPage='. $this->api_page;

$xmlObj = new SimpleXMLElement($this->api_url, NULL, TRUE); 

foreach($xmlObj->pet as $pet){

    echo $pet->animalID;
    echo $pet->orgID;
    echo $pet->status;

    // more fields from the  Pet object that is returned from the API call

    // Save results to my own Database

}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:16:11+00:00Added an answer on June 18, 2026 at 8:16 am

    Based on the assumption that you run on a pretty stable environment you could loop through the pages like this:

    <?php
    $this->base_url = 'http://api.rescuegroups.org/rest/?key=' .$this->api_key.
    '&type=animals&limit=' .$this->api_limit.
    '&startPage=';
    $start_page = $this->api_page;
    $end_page = 10; //If you have a value for max pages.
    // sometimes you might get the number of pages from the first returned XML and then you could update the $end_page inside the loop.
    
    for ($counter = $start_page; $counter <= $end_page; $counter++) {
        try {
            $xmlObj = new SimpleXMLElement($this->base_url . $counter, NULL, TRUE); 
    
            foreach($xmlObj->pet as $pet){
    
                echo $pet->animalID;
                echo $pet->orgID;
                echo $pet->status;
    
                // more fields from the  Pet object that is returned from the API call
    
                // Save results to my own Database
    
            }
    
    
        } catch (Exception $e) {
            // Something went wrong, possibly no more pages?
            // Please Note! You wil also get an Exception if there is an error in the XML
            // If so you should do some extra error handling
            break;
        }
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two pages - a simple HTML code example page from a jQuery
Below just a simple race between 2 button in 2 threads, and this will
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I am testing this simple code below and it works... $(document).ready( function() { $('.show-modal').click(
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Below is a simple example of my PHP code which (I hope so) is
Beej's Simple Client example code iterates over all IP addresses returned from getaddrinfo(), until
Consider the simple example below of implementing a method in an Enum. One problem
The line below is completely failing. template: _.template($('#test').html()), Trying to follow a simple example
The code pasted below is a simple JSF program, with an idea of having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.