title: API call limited to 500 results - how to iterate through to get all results?
body: <p>I am developing a web app which iterates through API results to insert into a MySQL database.</p>
  
  <p>The API I am using limits records returned to a maximum of 500 per call. </p>
  
  <p>They have a parameter where you can choose which "page" to return results from, so for 1504 (current number of records) records, I could call page 2 and return results from 500-999.</p>
  
  <p>I am semi novice at best, and this is my first foray into working with APIs...I am struggling to get my head around how I could gather all 1504 results.</p>
  
  <p>The API documentation for this is: <a href="https://developer.trademe.co.nz/api-reference/search-methods/rental-search/" rel="nofollow noreferrer">https://developer.trademe.co.nz/api-reference/search-methods/rental-search/</a></p>
  
  <p>I can use the "page" parameter to choose which page of results I want.</p>
  
  <pre><code>$url = "https://api.tmsandbox.co.nz/v1/Search/Property/Rental.json";
  
  $headr = array();
  $headr[] = 'Content-length: 0';
  $headr[] = 'Authorization: OAuth oauth_consumer_key="xxx", oauth_signature_method="PLAINTEXT", oauth_signature="xxx"';
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_TIMEOUT, 120);
  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_HTTPHEADER,$headr);
  $pageSize = 10;
  $data = array(
      "rows" =&gt; $pageSize,
      "sort_order" =&gt; "ExpiryDesc",
      "page" =&gt; 1
  );
  $totalPages = PHP_INT_MAX;
  $result = [];
  do {
      curl_setopt($curl, CURLOPT_URL, $url."?".http_build_query ($data));
      $resulta = curl_exec($curl);
  
      $array = json_decode($resulta, true);
      array_merge($result, $array);
      $data['page']++;
      if ( $totalPages == PHP_INT_MAX )   {
          $totalPages = floor($array['TotalCount']/$pageSize);
      }
  
  }
  while ( $totalPages &lt; $data['page']);
  
  $properties = $array['List'];
  $prop_count = $array['TotalCount'];
  curl_close($curl);
  </code></pre>
  
  <p>This is the code I am using to return the 500 results. $prop_count holds the total number of rows (1504) and $pages holds the number of pages (in this case, 3.08).</p>
  
  <p>Can anyone please give me a head start on the logical order to implement some kind of loop to return all results? I'm sitting here and racking my brain trying to get started.</p>