GitHub
Setup
Insert OneRoster.php into your project.
NOTE: OneRoster.php uses cURL, which may require setup on Windows.
Usage
First, import OneRoster.
include('OneRoster.php');
Next, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.
$oneRoster = new OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX')
You are now able to make requests to a specified URL. This is done using the method makeRosterRequest($url), which takes in the URL and returns an array.
$oneRoster = new OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX')
$response = $oneRoster->makeRosterRequest("https://example.com/users");
The response is made up of "status_code", which contains the status code, and "response", which contains the JSON response as a string.
echo $response["status_code"]; // The status code
echo $response["response"]; // The JSON response
Example
Example
Print all students' names
<?php
// Include OneRoster
include('OneRoster.php');
function getUsers($url) {
// Create new OneRoster object with the client_id and client_secret
$oneRoster = new OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX');
// Make the request to the given url and store the array with status_code and response
$response = $oneRoster->makeRosterRequest($url);
// Decode the response
$decoded = json_decode($response["response"], true);
// If status_code is 200, create array of users from response, otherwise print message and return null
switch ($response["status_code"]) {
case 200:
return $decoded["users"];
case 401:
echo "Unauthorized Request\n" . $response["response"];
break;
case 404:
echo "Not Found\n" . $response["response"];
break;
case 500:
echo "Server Error\n" . $response["response"];
break;
default:
echo "Something Went Wrong, status code " . $response["status_code"] . $response["response"];
break;
}
return null;
}
$users = getUsers("https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'");
// Print users if not null
if ($users != null) {
foreach ($users as $user) {
echo $user["givenName"] . " " . $user["familyName"] . "\n";
}
}