Twilio 如何对消息进行分页?

问题描述 投票:0回答:4

我能够从新的 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;
 }
 ?>
pagination twilio twilio-php twilio-api
4个回答
2
投票

这里是 Twilio 开发者布道者。

您可以使用

read()
 而不是使用 
stream()
,它将返回消息的迭代器。您可以给
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;
 }
 ?>

每次请求都会返回分页信息本身。您可以在文档中看到调用

Calls
资源的示例,并且分页信息与
Messages
相同。


2
投票

我在这上面浪费了几个小时。 如果它可以节省一些未来的人的时间,这就是我所做的。 我正在使用 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()) }}

page()
getPage()
的文档。


0
投票

这里是使用分页获取消息历史记录的 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');
     });


0
投票

对于任何在 2025 年末看到这一点的人来说, 您可以使用 .nextPage() 获取下一页数据。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.