API Call - PHP Example

Integrating a PHP application with EduBrite

You can integrate any PHP application with EduBrite by adding code in your PHP application. Integration could be as simple as single sign on, or more sophisticated which shows data pulled from EduBrite by making specific API calls.

For making API calls using PHP, follow the below examples.

EBCommon class

This class provides the common method call to make the HTTP call, and perform cookie and header handling.

<?php
class EBCommon{
    public function call($sessionId, $sessionInfo, $realUser, $url, $parameters)
    {
        $apiUrl = "http://mycompany.edubrite.com/oltpublish/site/";
        $curl_request = curl_init();
   
        curl_setopt($curl_request, CURLOPT_URL, $apiUrl . $url);
        curl_setopt($curl_request, CURLOPT_HEADER, 1);
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $parameters);
       
        if($sessionId != null){
            $cookieStr = "SESSION_ID=" . $sessionId;
            if($sessionInfo != null){
                $cookieStr .= "; SESSION_INFO=" . $sessionInfo;
            }
            //print($cookieStr . "\n");
            curl_setopt($curl_request, CURLOPT_COOKIE, $cookieStr);
           
            if($realUser != null){
                $headerStr = array("REAL_UNAME: ".$realUser);
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, $headerStr);
            }
        }
   
        $response = curl_exec($curl_request);
        //print($response);
        $error = curl_error($curl_request);
        $result = array(
                         'body' => '',
                         'error' => '',
                         'http_code' => '',
                         'session_info' => '',
                         'session_id' => ''
                         );
        if ( $error != "" )
        {
            $result['error'] = $error;
            return $result;
        }
      
        $header_size = curl_getinfo($curl_request,CURLINFO_HEADER_SIZE);
        $header = substr($response, 0, $header_size);
        $result['body'] = substr( $response, $header_size );
        $result['http_code'] = curl_getinfo($curl_request,CURLINFO_HTTP_CODE);
        curl_close($curl_request);   
       
        preg_match_all('/Set-Cookie:\s{0,}(?P<name>[^=]*)=(?P<value>[^;]*).*?$/im', $header, $cookies, PREG_SET_ORDER);
        foreach ($cookies as $match) {
            if($match["name"] == "SESSION_ID"){
                $result['session_id'] = $match["value"];
            }
            if($match["name"] == "SESSION_INFO"){
                $result['session_info'] = $match["value"];
            }
        }
        return $result;
    }   
}
?>



EBApiClient Class

This class implements the methods for each specific API. Application can call method of this class 

<?php 
include 'eb_common.php';
class EBApiClient{
	
	public function init(){
		$result = EBCommon::call(null, null, null, "connect.do", null);
		return $result;
	}

	public function connect($sessionId){
		$apiUsername = "api_e0100cf8-125a-11e2-a0d0-ca9aa7745035";
		$apiPassword = "-QT_dtJjMdan6CbXEs_CSZXPhqe0XfW0YTEwvOoUju8GtcwSIbetdd4HBQkMVesZG-YUkELI59Tnqj1TZ6FYGJ2WrL24r8vUP-xDDBUkMQk*|404668472000";
		
		$parameters = array(
		     "username" => $apiUsername,
		     "password" => $apiPassword,
		     "dispatch" => "connect"
		);
		$result = EBCommon::call($sessionId, null, null, "connect.do", $parameters);
		return $result;
	}
	

	public function getTestHistory($connection, $userName){
		if($connection["session_id"] == null && $connection["session_info"] == null && $userName == null){
			return null;
		}
		$parameters = array(
		     "pageSize" => 10,
		     "xml" => true,
		     "dispatch" => "list"
		);
		$result = EBCommon::call($connection["session_id"], $connection["session_info"], $userName, "testhistory.do", $parameters);
		//print($result['body'] . "\n");
		if($result['body'] != null){
			$xmlStr = $result['body'];		
			//print($xmlStr);
			$simpleXml = simplexml_load_string($xmlStr);
			$attrs = $simpleXml->attributes();
			print("Total test records = " . $attrs[numItems] . "\n");
		}	
	}


	public function createUserSession($connection, $userName){
		if($connection["session_id"] == null && $connection["session_info"] == null && $userName == null){
			return null;
		}
		$parameters = array(
		     "pageSize" => 10,
		     "xml" => true,
		     "dispatch" => "createSessionApi"
		);
		$result = EBCommon::call($connection["session_id"], $connection["session_info"], $userName, "signinService.do", $parameters);
		//print($result['body'] . "\n");
		if($result['body'] != null){
			$xmlStr = $result['body'];		
			//print($xmlStr);
			$simpleXml = simplexml_load_string($xmlStr);
			print("\nUser session id = " . $simpleXml->data->session->sessionId . "\n");
			print("\nUser session info = " . $simpleXml->data->session->sessionInfo . "\n");
			$userSessionVars = array(
				"session_id" => $simpleXml->data->session->sessionId,
				"session_info" => $simpleXml->data->session->sessionInfo,
			);
			return $userSessionVars;
		}	
	}
}
?>
 

Usage inside Application

Example code to make API calls.  

<?php
include 'eb_api.php';

//First step is to get the session id,
$result = EBApiClient::init();

//Next step is to login as the API user and get the session info
$result = EBApiClient::connect($result['session_id']);
print($result['session_id'] . "\n");
print($result['session_info'] . "\n");   
//Now we can make calls to other APIs. Second parameter indicates the application user (effective user) which is making the API calls.

//Get test history of user dev2
EBApiClient::getTestHistory($apiConnect, "dev2");

//Create user session for user dev1,
$userSessionVars = EBApiClient::createUserSession($apiConnect, "dev1");
//We can use $userSessionVars["session_id"] and $userSessionVars["session_info"] to construct the iframe url of the course player for this user

// iframe_url = "coursePlayer.do?id=course_id&&dispatch=embed&sessionId=" . $userSessionVars["session_id"] . "&sInfo=" . $userSessionVars["session_info"]

// iframe_url = "coursePlayer.do?courseSessionId=course_session_id&&dispatch=embed&sessionId=" . $userSessionVars["session_id"] . "&sInfo=" . $userSessionVars["session_info"]

?>

Passing User's Email and Fullname

You can also pass a concatenated string in the username parameter mentioned above, instead of passing just the username. The concatenated string must be in this format

username|email|fullname

Full name should be first name followed by space followed by last name.

Single Sign on Usage

To create single sign on experience into EduBrite from a PHP application, you need to simply create a edubrite launch url with sessionId and sInfo parameters after getting them by making api calls. Then you just open that url in a new window. On first load of the new window, EduBrite will drop necessary session cookies in the browser and subsequent user interactions will work normally.

Auto Create User (Auto Provisioning)

In EduBrite, you can enable Auto Create User by API in Site Admin->Site Details->Integration to allow your PHP application users to start accessing EduBrite as soon as they launch it first time. The createSession API call will ensure a new user gets created in LMS if doesn't already exist by matching the username.

 

 


Rating: