cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
fabiocunha
Community Participant

How to deal with pagination in Canvas using PHP

Jump to solution

I was trying to use pagination in my requests, I just try to show the Header that contains the "Link" values.

I use Guzzle with PHP

$res_aluno_pag = $client->request('GET', 'courses/'.$course_id.'/enrollments?type[]=StudentEnrollment&state[]=active&per_page=1000', ['headers' => $headers
]);
var_export( $res_aluno_pag->getHeader("Link") );

This is the what appear in my screen

array ( 0 => '; rel="current",; rel="next",; rel="first",; rel="last"', )

But if I use postman, I can see the following result inside the "Headers" tab

<https://pucminas.instructure.com/api/v1/courses/5465/enrollments?role%5B%5D=StudentEnrollment&state%5B%5D=active&page=2&per_page=100>; rel="current",
<https://pucminas.instructure.com/api/v1/courses/5465/enrollments?role%5B%5D=StudentEnrollment&state%5B%5D=active&page=1&per_page=100>; rel="prev",
<https://pucminas.instructure.com/api/v1/courses/5465/enrollments?role%5B%5D=StudentEnrollment&state%5B%5D=active&page=1&per_page=100>; rel="first",
<https://pucminas.instructure.com/api/v1/courses/5465/enrollments?role%5B%5D=StudentEnrollment&state%5B%5D=active&page=2&per_page=100>; rel="last"

How can I use PHP and Guzzle to see the complete text/string that is inside the "Link" Header?

Labels (1)
1 Solution

Accepted Solutions
robotcars
Community Champion

 @fabiocunha 

I have not used Guzzle, but community developers have created a very nice resource for https://community.canvaslms.com/thread/1500 in various languages. If you figure out how to handle the pagination with Guzzle we'd love to see an example.

View solution in original post

4 Replies
robotcars
Community Champion

 @fabiocunha 

I have not used Guzzle, but community developers have created a very nice resource for https://community.canvaslms.com/thread/1500 in various languages. If you figure out how to handle the pagination with Guzzle we'd love to see an example.

View solution in original post

fabiocunha
Community Participant

Hi Robert, thank for your help. I see how to implement pagination now.

But I found an easier way using the code below

$res_course = $client->request('GET', 'courses/'.$course_id.'?include[]=total_students', ['headers' => $headers ]);
$array_course = json_decode(($res_course->getBody()), True);

$number_of_pages= ceil($array_course["total_students"]/100) ;

I get the number of pages(to be used in pagination) based on the number of active students.

jsavage2
Community Contributor

HI  @fabiocunha ‌,

That will probably work most of the time, but the behavior isn't guaranteed: the pagination limit for any given call is arbitrary, and the response isn't always what you request with the per_page parameter. Parsing the Link header is definitely a safer option.

For parsing the headers with Guzzle, see the Guzzle documentation on Parsing complex headers (Guzzle and PSR-7 — Guzzle Documentation ). You need to import the Psr7 libraries and call the Psr7 parse_header function on the return value from getHeader. try: 

$links = Psr7\parse_header($res_aluno_pag->getHeader("Link") )

var_export($links)

James
Community Champion

Like  @jsavage2  said, this is a bad idea. He worded it nicer, but you definitely want to parse the link header and not rely on there being 100 items per page. That number isn't guaranteed and Canvas can lower it if necessary. Some of the calls use bookmarks instead of pages and this approach won't work at all.