问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/45295566/creating-a-paginated-display-of-twillio-sent-messages