AnsweredAssumed Answered

Canvas API Pagination in PHP Returns Empty Link Header (first, last, next...)

Question asked by Larry Robertson on Apr 18, 2019
Latest reply on Apr 18, 2019 by Larry Robertson

I am having a problem that was discussed back in 2016 but not in the context of PHP and for that reason I have created a new question.

 

To test the API's pagination I initially use curl.exe from the command line. Downloaded from https://curl.haxx.se/windows/.

After getting the desired output in curl.exe I then try to code the equivalent in PHP.

 

Here is a simple test to show the problem, first curl.exe (This Works Great!)

  1. Copy the curl.exe, libcurl.dll and curl-ca-bundle.crt to your user folder "C:\Users\yourname" or somewhere that the path environment point to.
  2. Run the following command: curl -I -H "Authorization: Bearer <access token>" "https://canvas.instructure.com/api/v1/courses?per_page=2"

 

Output will be similar to this and includes the Link information:

HTTP/1.1 200 OK
Date: Thu, 18 Apr 2019 15:55:56 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Server: Apache
X-Request-Cost: 0.06349024999995452
X-Session-Id: cb1a15055322ef70bf5b902bbdbc41efd2
Cache-Control: max-age=0, private, must-revalidate
X-Request-Context-Id: 70b3554e6-6ff0-4ae5-91dc-83f56ff811fe5
Strict-Transport-Security: max-age=31536000
X-Rate-Limit-Remaining: 700.0
X-Canvas-Meta: q=1161;at=70006388081375;dk=170000000000016;a=10;g=ff2e57860-fa55b-012d-f7b3-123135003972;s=7;c=cluster7;z=us-east-1e;o=courses;n=index;b=1759500;m=1759500;u=0.05;y=0.01;d=0.01;
X-XSS-Protection: 1; mode=block
X-Canvas-User-Id: 0
Link: <https://canvas.instructure.com/api/v1/courses?page=1&per_page=1>; rel="current",<https://canvas.instructure.com/api/v1/courses?page=2&per_page=1>; rel="next",<https://canvas.instructure.com/api/v1/courses?page=1&per_page=1>; rel="first",<https://canvas.instructure.com/api/v1/courses?page=2&per_page=1>; rel="last"
X-UA-Compatible: IE=Edge,chrome=1
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.071511
X-Content-Type-Options: nosniff
Set-Cookie: _csrf_token=0%3D%3D; path=/; secure
Set-Cookie: log_session_id=cb1a1505322ef709bf5b902b64bdbc41efd2; path=/
Set-Cookie: canvas_session=eTGtkot3wxtZe9-05UTQbg.MJc9xp7fa94aN9pa0MZQpcvE4883i2S4b8ErHTq7MoxIozXDjDBvtLA9Xc4XQVpNu8ufGWjceNTcC3nuCqJ9K3Yey0XYSRNH1nDkRjaGpkP6fmp7v8F63UWgo7cj42TxS7kO.E2O9XaI6YnK_yy9VxcHWkbRmn60.XLieDA; path=/; secure; HttpOnly
X-Request-Processor: 03ac2639275afeb78d06
X-A11y-Ally: Dana Danger Grey
ETag: W/"a958afacfd57c80097aee24f9f2fbea869b"
Status: 200 OK
P3P: CP="None, see http://www.instructure.com/privacy-policy"
------------------------------------------------------------------------------------------------------------------------------------------------

 

Next try it in PHP

<?php

// OPTIONS:
$url = 'https://canvas.instructure.com/api/v1/courses?per_page=1';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . '<access token>',
'Content-Type: application/json'
));

//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE:
$result = curl_exec($curl);
//$i = curl_getinfo($curl);
//var_dump($i) . '<br/>';
var_dump($result) . '<br/>';

if(!$result){
die("Connection Failure");
}
curl_close($curl);

?>

 

Output will be similar to this and includes the Link information with no urls for current, first and last:

Link: ; rel="current",; rel="next",; rel="first",; rel="last"

 

I can't figure out why the urls (i.e. <https://canvas.instructure.com/api/v1/courses?page=2&per_page=1>) are not being output. Is there a curl_setopt that I'm missing?

 

Can anyone help me with this problem?

Thank you for any help you can give me.

Outcomes