This one is a bit tricky. With the initial user creation, according to the API, there are no parameters for adding the avatar. However, you can add it when editing a user. Further, the required parameters will depend upon whether you want the avatar linked remotely (from where they're currently stored) or copied to the Canvas server. Given the concept, I would assume you want them linked remotely to ensure the most recent image is always displayed (this assumes that a new image for a user is named and stored in the same place as the old, overwriting it upon creation).
Here's a quick little function for editing a user profile to set the avatar to a remote image using a PHP script:
<?php
// Domain to your Canvas instance
$server = 'canvas.instructure.com';
// Add the access token for system-level AccountAdmin here
$auth = 'Authorization: Bearer <access token goes here>';
/* BEGIN AVATAR LINKING HERE */
// Retrieve this value however you must
$userID = 1;
// Example call of the function
$oldProfile = executeCURL('/users/' . $userID, 'GET');
$newProfile = linkAvatar($userID, 'https://s-media-cache-ak0.pinimg.com/736x/a4/84/4e/a4844e1a6040040af19dfeb1a92602ac.jpg');
$success = ($oldProfile[1] == $newProfile[1]) ? true : false;
// Insert Unsuccessful Executions Here
/* END AVATAR LINKING HERE */
/**
* Formats and executes cURL for avatar linking
*/
function linkAvatar($userID, $avatarURL) {
$data = json_encode(array('user' => array('avatar' => array('url' => $avatarURL))));
$apiURL = '/users/' . $userID;
return executeCURL($apiURL, 'PUT', $data);
}
/**
* Executes cURL requests
*/
function executeCURL($apiURL, $requestType, $data = NULL) {
global $server, $auth;
$apiURL = $server . $apiURL . (($requestType == 'GET') ? (((strpos($apiURL, '?') !== false) ? '&' : '?') . 'per_page=100') : '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestType);
if($data !== NULL) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data), $auth));
$results = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($results, 0, $headerSize);
$results = json_decode(substr($results, $headerSize));
curl_close($ch);
return array($header, $results);
}
?>
This uses a generic XMLHttpRequests handling function, but your institution may have a different one. The returned data from the function is an array for the results header and body.