A newer version of the Ed-Fi ODS / API is now available. See the Ed-Fi Technology Version Index for a link to the latest version.

Using Scripting Languages to Connect

The API surface of an Ed-Fi ODS / API is platform neutral, meaning that client systems can be written in a variety of languages.

This documentation primarily covers examples using Java and C# libraries. However, scripting languages such as Ruby or PHP can also be used. The following code snippet provides a simple example in PHP showing authentication followed by a GET to populate a simple list of Student entities.

PHP Script Example
<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/v5.3/api"; // NOTE: No trailing slash!
    $edfiClientId = "RvcohKz9zHI4";
    $edfiClientSecret = "E1iEFusaNf81xzCxwHfbolkC";

    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;
    }

    
    function getAuthToken($edfiBaseUrl, $edfiClientId, $edfiClientSecret){
   
        $edfiApiTokenUrl = "$edfiBaseUrl/oauth/token";
        $paramsToPost = "Client_id=$edfiClientId&Client_secret=$edfiClientSecret&Grant_type=client_credentials";
        
        $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);

        $response = new stdClass();
        $response -> success = true;
        if(property_exists($jsonResult, 'error')){
            $response->success = false;
            $response->errorMessage = "Invalid Credentials";
        }
        else
            $response->token = $jsonResult->access_token;
   
        return $response;
    }
?>

<?php
    $getTokenResult = getAuthToken($edfiBaseUrl, $edfiClientId, $edfiClientSecret);
    if(!($getTokenResult->success)){
        echo "<p>Error Message: $getTokenResult->errorMessage</p>";
    }
    else {
        $accessToken = $getTokenResult->token;
        echo "<p>Access Token: $accessToken </p>";

        $jsonStudents = edfiApiGet($accessToken,"https://api.ed-fi.org/v5.3/api/data/v3/ed-fi/students","limit=10");

        echo "List of Students: \n\n";
    }
?>

<table>
   <thead>
     <tr>
        <th>FirstName</th>
        <th>Middle</th>
        <th>Last</th>
     </tr>
   </thead>
   <tbody>
    <?php
        if($getTokenResult->success){
            foreach ($jsonStudents as $student => $s) {
                // Output a row
                $row = "<tr>";
                $row .= "<td>$s->firstName</td>";
                if(property_exists($s, 'middleName'))
                    $row .= "<td>$s->middleName</td>";
                $row .= "<td>$s->lastSurname</td>";
                $row .= "</tr>";
                echo $row;
            }
        }
    ?>
   </tbody>
</table>
<hr>