Speeding up Canvas API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm having some trouble with some development work for our classes using the Canvas API that is likely a beginner's issue.
The gist of the problem centers on my requests timing out. This could be a very newbie error, but it seems like my calls to the API (for current grade, missing assignments, last activity), bring in way more data that I need. I am not sure if this is what is slowing it down but it doesn't seem that I can even limit my calls to simply return the single fields I need (e.g. current_score). I use a Curl GET function that was posted here in the community a while back.
I am currently running PHP scripts via cron each morning. The load is about 40 classes and 600 students but I'm even at the point where I'm running 10 classes/150 students.
Any thoughts would be appreciated.
// The following function covers any API request using GET
function curlGet($url) {
global $canvasURL, $tokenHeader;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, 'https://' . $canvasURL . '/api/v1/' . $url);
// echo "https://$canvasURL/api/v1/$url";
curl_setopt ($ch, CURLOPT_HTTPHEADER, $tokenHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // ask for results to be returned
curl_setopt($ch, CURLOPT_VERBOSE, 1); //Requires to load headers
curl_setopt($ch, CURLOPT_HEADER, 1); //Requires to load headers
$result = curl_exec($ch);
// var_dump($result);
#Parse header information from body response
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
$data = json_decode($body);
curl_close($ch);
#Parse Link Information
$header_info = http_parse_headers($header);
if(isset($header_info['Link'])){
$links = explode(',', $header_info['Link']);
foreach ($links as $value) {
if (preg_match('/^\s*<(.*?)>;\s*rel="(.*?)"/', $value, $match)) {
$links[$match[2]] = $match[1];
}
}
}