Twilio 如何对消息进行分页?

Twilio How to do pagination with messages?

我能够从新的 php 客户端获取消息。如何对消息进行分页?如何获取next_uri、first_uri、page_size参数?

 <?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;
 }
 ?>

这里是 Twilio 开发人员布道者。

您可以使用 stream() 而不是 read(),这将 return 是您消息的迭代器。您可以给 stream() 一个限制,但默认情况下它没有限制,并且会遍历您的所有消息。

 <?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;
 }
 ?>

pagination information itself is returned in each request. You can see an example of a call to the Calls resource in the documentation 和分页信息将与 Messages 相同。

这是使用分页获取消息历史记录的Node.js代码。您可以使用参数 pageSize 指定单个页面中应包含多少项目,并使用 limit 参数限制要显示的页面数量

              client.messages.each({
                dateSent: new Date(date.toDateString()),
                from: event.To,
                to: event.From,
                pageSize: 1,
                limit:1,
                done: function(done) {//this is the call back for the for each loop. this will get fired even if no messages found.
                    console.log('completed for each');
                }
            }, (messages) => {//each message can handle here.
                console.log("message body:", messages.body);
            }, (Error) => {
                console.log('err');
     });

我在这上面浪费了几个小时。如果它可以节省一些未来的人一些时间,这就是我所做的。我正在使用 Laravel 但你明白了:

在你的控制器中:

// If no pagination info has been specified, get the first page of data
// using page().  If there is pagination info in the request, use it with
// getPage()

if (! $request->page) {
    $messages = $client->messages->page([], 30);
} else {
    $messages = $client->messages->getPage($request->page);
}

那么,在你看来(Laravel/blade伪代码):

@foreach ($messages as $message)
    $message->body
    // ... etc
@endforeach

// Next page link
?page={{ urlencode($messages->getNextPageUrl()) }}

// Prev page link
?page={{ urlencode($messages->getPreviousPageUrl()) }}

Docs for page() and getPage().