/
Using Scripting Languages to Connect

This version of the Ed-Fi ODS / API is no longer supported. 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/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); ?>


Related content

Using Scripting Languages to Connect
Using Scripting Languages to Connect
More like this
Using Scripting Languages to Connect
Using Scripting Languages to Connect
More like this
Using Scripting Languages to Connect
Using Scripting Languages to Connect
More like this
Using Scripting Languages to Connect
Using Scripting Languages to Connect
More like this
Using Scripting Languages to Connect
Using Scripting Languages to Connect
More like this
Using Scripting Languages to Connect
Using Scripting Languages to Connect
More like this