paginationtwiliotwilio-phptwilio-api

Twilio How to do pagination with messages?


I am able to get the messages from the new php client. How do I do pagination with messages? How to get next_uri, first_uri, page_size parameters ?

 <?php 
 require_once '/Twilio/autoload.php'; // Loads the library

 use Twilio\Rest\Client;

 // Your Account Sid and Auth Token from twilio.com/user/account
 $sid = "xxx"; 
 $token = "xxx";
 $client = new Client($sid, $token);

 // Loop over the list of messages and echo a property for each one
 foreach ($client->messages->read() as $message) {
 echo $message->body;
 }
 ?>

Solution

  • Twilio developer evangelist here.

    Instead of using read() you can use stream() which will return an iterator for your messages. You can give stream() a limit, but by default it has no limit and will iterate over all of your messages.

     <?php 
     require_once '/Twilio/autoload.php'; // Loads the library
    
     use Twilio\Rest\Client;
    
     // Your Account Sid and Auth Token from twilio.com/user/account
     $sid = "xxx"; 
     $token = "xxx";
     $client = new Client($sid, $token);
    
     // Loop over the list of messages and echo a property for each one
     foreach ($client->messages->stream() as $message) {
     echo $message->body;
     }
     ?>
    

    The pagination information itself is returned in each request. You can see an example of a call to the Calls resource in the documentation and the pagination information will be the same for Messages.