0

The Twilio API docs describe retrieving all messages or a particular message in PHP like bellow:

<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$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;
}

But fetching all messages with a single call and bringing it to the front end leads to heavy load on my application. So is there any way to implement pagination in a nice way to put latest 50 messages upon clicking next button next 50 and so on?

Praveen George
  • 9,237
  • 4
  • 26
  • 53

1 Answers1

0

Twilio developer evangelist here.

Rather than reading all the records, you can start by fetching a page, using $client->messages->page. This returns a page of results which you can iterate through to display on your page. It also contains some meta data, including the nextPageUrl which you can also send to your page.

When you then make the request to load 50 more results, you can pass that URL to $client->messages->getPage() and that will fetch a new page of messages.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • thanks for the on time support. Let me simplify my question. I need to have a paginated display of all messages, not all on the same page, but with a next button. My initial page should show the latest 50 messages and on clicking next it should display the next 50 messages. – Praveen George Jul 25 '17 at 09:11
  • can you provide code samples for the same, on how to pass parameters to achieve it? – Praveen George Jul 25 '17 at 09:11
  • What have you tried so far? Have you attempted to use the `page` or `getPage` methods like I described? – philnash Jul 25 '17 at 09:39
  • I didn't try anything, as I don't know how to write the proper code to achieve it. Can you please give me the code sample? – Praveen George Jul 25 '17 at 09:45
  • I can't write your app for you and I don't have a code sample ready to show you. I don't know how you are currently writing your app. You need to try to use the API first, I've given some pointers as to how, and then when you get stuck ask a question on Stack Overflow with the code you have written and the issue you are facing. – philnash Jul 25 '17 at 10:44
  • In case it helps some future visitor, [here's how I did it](https://stackoverflow.com/a/65200354/6089612). – Don't Panic Dec 08 '20 at 13:59