Creating a Paginated display of Twillio Sent Messages

南楼画角 提交于 2019-12-25 18:15:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!