1

I am using the Twilio SDK to make REST requests to fetch the calls made during the last day and the amount of calls is bigger than the total amount of calls on my account. It just keeps running indefinitely.

I did make only one request to the trillionth page and I receive records.

<?php
$newClient = new \Twilio\Rest\Client(
    Config_Twilio::ACCOUNT_SID,
    Config_Twilio::AUTH_TOKEN
);

$lastNDays = 1;

$params = [
  'startTime' => date('Y-m-d', strtotime('-' . $lastNDays . ' days'))
];

#some code to make a first request here...

.
.
.


if($content['next_page_uri']) {

  do {
     $params['Page'] = $content['page']+1;
     try {
         $response = $newClient->request(
             "GET",
             Config_Twilio::DOMAIN_URL . "/" . Config_Twilio::API_VERSION . "/Accounts/" .
             Config_Twilio::ACCOUNT_SID . "/Calls.json",
             $params
         );
 
         $content = $response->getContent();
         
         if (is_array($content['calls'])) {
             $callSets = array_merge($content['calls']);
         }
     } catch (\Throwable $th) {
         //throw $th;
     }
  } while ($content['next_page_uri'] !== null);
}

How can I obtain all the records avoiding this infinite loop?

MikeVelazco
  • 885
  • 2
  • 13
  • 30
  • Basically even when `$content['next_page_uri']` is empty or there is none it seems to not equal `NULL` which may mean it's `0` or `false` or perhaps blank `''` but not quite `NULL`. Try a `var_dump` of `$content['next_page_uri']`, see what a common string that's guaranteed to be present is and do a resource-light `strpos` check for it. Or replace `while ($content['next_page_uri'] !== null);` with `while (!empty($content['next_page_uri']));`. Let me know if this helps – John Smith Jan 13 '23 at 02:42
  • It has the same effect @JohnSmith – MikeVelazco Jan 13 '23 at 14:38
  • That is odd. According to the Twilio docs (https://www.twilio.com/docs/usage/twilios-response#response-formats-list) `previous_page_uri` is shown as `null` by default but your script may be treating it as a literal string "null" and not the value. Unless Twilio for some reason doesn't replicate this behavior at the theoretical "last page" for `next_page_uri`... Replacing `null` with `'null'` does nothing? For now hardcode a `$i = 0; if ( $i > 50 ) { break; } $i++;` stop just so the script is forced to complete and `var_dump` the `$content` each loop just to see what's going on... – John Smith Jan 13 '23 at 16:10
  • Sorry, after declaring `$i=0;` at the top of your script then adding `$i++;` within the main loop replace `while ($content['next_page_uri'] !== null);` with `while ($content['next_page_uri'] !== null && $i<50);` to force the script to complete regardless after say the 50th iteration... This may even be sufficient depending on what you're working on but again with a `var_dump` of the `$content` you'll at least know what to look for. Omit sensitive information before posting. – John Smith Jan 13 '23 at 16:18
  • ~I did a hard stop at page 100, receiving 500 records, but only 50~70 unique records. I did make a request with `PageSize` equals 1000 and I did receive 1002 unique call records. It is definitively broken. – MikeVelazco Jan 13 '23 at 17:57

1 Answers1

0

I'd suggest, without having run the code, that the infinite loop is because of your loop conditions. How about using more of the Twilio Helper Library for PHP? You can find a quite concise example in the documentation.

Matthew Setter
  • 2,397
  • 1
  • 19
  • 18