twilio调用日志laravel的分页功能不正常。

问题描述 投票:0回答:1
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use App\Model\User;
use Illuminate\Support\Facades\Validator;
use Auth;
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;

class TwilioController extends ApiController
{
    public function __construct(){
        $sid = env('TWILIO_SID');
        $token = env('TWILIO_TOKEN');
        $this->twilio = new Client($sid, $token);
        if (request('lang'))
            \App::setLocale(request('lang'));
    }

    public function callLogs(Request $request){
        try{
            $twilioNumber = Auth::user()->twilio_number;
            $calls = $this->twilio->calls
                ->read([], 20);
                $data = [];
                $i = 0;
            foreach($calls as $call){
                $data[$i]['from'] = $call->from;
                $i++;
            }
            $responseData = array('status'=>true, 'message'=>'Data has been returned successfully!', 'data'=>$data);
        } catch (\Exception $e) {
            $responseData = array('status'=>false, 'message'=>$e->getMessage(), 'data'=>[]);
        }
        $res = json_encode($responseData);
        print $res;

    }

当我在twilio中使用laravel rest api获取调用历史记录时,分页不能正常工作,当我使用页面参数时,分页不能正常工作,它给我的是保存第一页的输出。Postman参数 - Page:2

谢谢你

twilio twilio-api twilio-php twilio-twiml
1个回答
1
投票

我觉得Twilio的 page() 功能将帮助你。像 read()它接受一系列的选项来缩小你的搜索范围,并有一个 pageSize 参数,默认为每页50次调用。我相信最大值是1000)。

下面是一个例子。

//Get all call logs. 20 per page
$calls = $twilio->calls->page( [], 20 );

$data = [];

foreach($calls as $call){
    $data[] = $call->sid;
}

//You can access the previous and next page urls using the resource functions in the calls object return from twilio.

return response()->json(["prev" => $calls->getPreviousPageUrl(), "next" => $calls->getNextPageUrl(), "calls" => $data]);
© www.soinside.com 2019 - 2024. All rights reserved.