Twilio如何对邮件进行分页?

时间:2017-01-09 07:27:16

标签: pagination twilio twilio-php twilio-api

我能够从新的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;
 }
 ?>

3 个答案:

答案 0 :(得分:1)

我为此浪费了时间。为了节省一些未来的人的时间,这就是我所做的。我正在使用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()

答案 1 :(得分:0)

Twilio开发者传道者在这里。

您可以使用stream()而不是使用read(),它将返回您的消息的迭代器。您可以给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本身。您可以看到example of a call to the Calls resource in the documentationMessages的分页信息也是相同的。

答案 2 :(得分: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');
     });