Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Code Block
languagephp
titlePHP Script Example
 <html><html>
   	<head>
 		      <title>PHP Test</title>
   	</head>
 <body><body>
 
 <?php
      // TODO: Replace URL, Client ID, and Client Secret with your values
     $edfiBaseUrl$edfiBaseUrl = "https://api.ed-fi.org/v5.3/api"; // NOTE: No trailing slash!
 $edfiClientId    $edfiClientId = "RvcohKz9zHI4";
     $edfiClientSecret$edfiClientSecret = "E1iEFusaNf81xzCxwHfbolkC";

      functionfunction getAuthCode($edfiBaseUrledfiApiGet($token, $edfiResourceUrl, $edfiClientId$data){
  
 	$edfiApiCodeUrl
   
        $authorization = "$edfiBaseUrl/oauth/authorize"; 
 	$data = "Client_id=$edfiClientId&Response_type=code";
 	$urlWithData = "$edfiApiCodeUrl?$data";
 
 	Authorization: Bearer $token";
       
        $curl = curl_init();
 
        
         	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 	
        curl_setopt($curl, CURLOPT_URLHTTPHEADER, $edfiApiCodeUrl);
 	curl_setopt($curl, CURLOPT_POST, 1);
 	array('Content-Type: application/json' , $authorization ));
        curl_setopt($curl, CURLOPT_POSTFIELDSURL, "$edfiResourceUrl?$data");
 
	
         
         // Receive server response.
 	
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
           
         	$result = curl_exec($curl);
 
 	
       
        $jsonResult = json_decode($result);
 
 	
       
        curl_close($curl);
 
 	
       
        return $jsonResult->code;
  }   }

    
      functionfunction getAuthToken($edfiBaseUrl, $edfiClientId, $edfiClientSecret,$authCode){
    
         	$edfiApiTokenUrl = "$edfiBaseUrl/oauth/token";
 	
        $paramsToPost = "Client_id=$edfiClientId&Client_secret=$edfiClientSecret&Code=$authCode&Grant_type=authorizationclient_codecredentials";
         
         	$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 $response = 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","");
 $jsonStudentsnew 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/apidata/v2.0/2017v3/ed-fi/students","limit=10");
 ?>
 
 <table>
 	<thead>
    	<tr>
      		<th>FirstName</th>
      		<th>Middle</th>
      		<th>Last</th>
    	</tr>
  	</thead>
  	<tbody>
	<?php 
 	;

        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>
<?php echo json_encode($jsonStudents); ?>