Ā <html>
<head>
Ā <title>PHP Test</title>
Ā </head>
Ā <body>
Ā
<?php
Ā
Ā // TODO: Replace URL, Client ID, and Client Secret with your values
Ā $edfiBaseUrl = "https://api.ed-fi.org/api"; // NOTE: No trailing slash!
Ā $edfiClientId = "RvcohKz9zHI4";
Ā $edfiClientSecret = "E1iEFusaNf81xzCxwHfbolkC";
Ā
Ā function getAuthCode($edfiBaseUrl, $edfiClientId){
Ā
Ā $edfiApiCodeUrl = "$edfiBaseUrl/oauth/authorize";Ā
Ā $data = "Client_id=$edfiClientId&Response_type=code";
Ā $urlWithData = "$edfiApiCodeUrl?$data";
Ā
Ā $curl = curl_init();
Ā
Ā curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
Ā curl_setopt($curl, CURLOPT_URL, $edfiApiCodeUrl);
Ā curl_setopt($curl, CURLOPT_POST, 1);
Ā curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
Ā
// Receive server response.
Ā curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Ā Ā
Ā $result = curl_exec($curl);
Ā
Ā $jsonResult = json_decode($result);
Ā
Ā curl_close($curl);
Ā
Ā return $jsonResult->code;
Ā }
Ā
Ā function getAuthToken($edfiBaseUrl,$edfiClientId,$edfiClientSecret,$authCode){
Ā
Ā $edfiApiTokenUrl = "$edfiBaseUrl/oauth/token";
Ā $paramsToPost = "Client_id=$edfiClientId&Client_secret=$edfiClientSecret&Code=$authCode&Grant_type=authorization_code";
Ā
Ā $curl = curl_init();
Ā
Ā curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
Ā curl_setopt($curl, CURLOPT_URL, "$edfiApiTokenUrl");
Ā curl_setopt($curl, CURLOPT_POST, 1);
Ā curl_setopt($curl, CURLOPT_POSTFIELDS, $paramsToPost);
Ā
// Receive server response
Ā curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Ā Ā
Ā $result = curl_exec($curl);
Ā
Ā $jsonResult = json_decode($result);
Ā
Ā curl_close($curl);
Ā
Ā return $jsonResult->access_token;
Ā }
Ā
Ā function edfiApiAuthenticate($edfiBaseUrl,$edfiClientId,$edfiClientSecret,$authCode){
Ā $authCode = getAuthCode($edfiBaseUrl, $edfiClientId);
Ā $accessToken = getAuthToken($edfiBaseUrl,$edfiClientId,$edfiClientSecret,$authCode);
Ā return $accessToken;
Ā }
Ā
Ā function edfiApiGet($token,$edfiResourceUrl,$data){
Ā
Ā $authorization = "Authorization: Bearer $token";
Ā
Ā $curl = curl_init();
Ā
Ā curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
Ā curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
Ā curl_setopt($curl, CURLOPT_URL, "$edfiResourceUrl?$data");
Ā
// Receive server response
Ā Ā Ā curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Ā Ā
Ā $result = curl_exec($curl);
Ā
Ā $jsonResult = json_decode($result);
Ā
Ā curl_close($curl);
Ā
Ā return $jsonResult;
Ā }
Ā ?>
Ā
<?php
Ā $accessToken = edfiApiAuthenticate($edfiBaseUrl,$edfiClientId,$edfiClientSecret,$authCode);
Ā echo "<p>token: $accessToken</p>";
Ā
Ā //$jsonStudents = edfiApiGet($accessToken,"https://api.ed-fi.org/api/api/v2.0/2017/students","");
Ā $jsonStudents = edfiApiGet($accessToken,"https://api.ed-fi.org/api/api/v2.0/2017/students","limit=10");
Ā ?>
Ā
Ā <table>
Ā <thead>
Ā Ā Ā <tr>
Ā Ā Ā Ā Ā <th>FirstName</th>
Ā Ā Ā Ā Ā <th>Middle</th>
Ā Ā Ā Ā Ā <th>Last</th>
Ā Ā Ā </tr>
Ā </thead>
Ā <tbody>
<?php
Ā foreach ($jsonStudents as $student => $s) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Output a row
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $row = "<tr>";
Ā Ā Ā $row .= "<td>$s->firstName</td>";
Ā Ā Ā $row .= "<td>$s->middleName</td>";
Ā Ā Ā $row .= "<td>$s->lastSurname</td>";
Ā Ā Ā $row .= "</tr>";
Ā Ā Ā echo $row;
Ā Ā Ā Ā Ā Ā Ā }?>
Ā </tbody>
</table>
<hr>
<?php echo json_encode($jsonStudents); ?>