API

Installazione, configurazione generale, notifiche, API, lingue, problemi server
NewWay
FormaLms User
Posts: 32
Joined: Sat Mar 08, 2014 12:56 pm

API

Post by NewWay »

Salve,
sapreste darmi indicazioni precise su come utilizzare le api?

ho già dato un'occhiata al sito di https://www.docebo.com/it/lms-docebo-ap ... tegration/ ma non mi è chiaro come ci si fa ad autenticare.
User avatar
max
FormaLms Guru
Posts: 2735
Joined: Thu Mar 01, 2012 10:41 am
Version: forma.lms 2.4
Contact:

Re: API

Post by max »

Ciao,
quelle che hai linkato sono le API di Docebo Cloud...
---------------------
Massimiliano Ferrari
Elearnit - Elearning e Knowledge Management
https://www.elearnit.net
https://www.linkedin.com/in/massimilianoferrari
m.ferrari[at]elearnit.net
Skype: m_ferrari_it
NewWay
FormaLms User
Posts: 32
Joined: Sat Mar 08, 2014 12:56 pm

Re: API

Post by NewWay »

Grazie max... e quindi ? :cry:
User avatar
max
FormaLms Guru
Posts: 2735
Joined: Thu Mar 01, 2012 10:41 am
Version: forma.lms 2.4
Contact:

Re: API

Post by max »

E quindi ti servono le API di Forma. Abbiamo ns documentazione appunto sulle API di Forma, stiamo verificando di
- aggiornarla
- pubblicarla sul forum
---------------------
Massimiliano Ferrari
Elearnit - Elearning e Knowledge Management
https://www.elearnit.net
https://www.linkedin.com/in/massimilianoferrari
m.ferrari[at]elearnit.net
Skype: m_ferrari_it
NewWay
FormaLms User
Posts: 32
Joined: Sat Mar 08, 2014 12:56 pm

Re: API

Post by NewWay »

Grazie mille poi magari scrivi qui la url almeno chi passa per qua trova tutto :) ;)
User avatar
alberto
FormaLms Guru
Posts: 1134
Joined: Fri Mar 02, 2012 9:18 am
Contact:

Re: API

Post by alberto »

Ciao, ecco QUI un po' di documentazione


a presto!
NewWay
FormaLms User
Posts: 32
Joined: Sat Mar 08, 2014 12:56 pm

Re: API

Post by NewWay »

Grazie milleee!!!

Anche se non è abbastanza :P dopo aver seguito la guida alla semplice richiesta /api/user/checkUsername con un username valido farmalms mi da come risposta:

'error' => string ' code: ; rest_module: user; funct: checkUsername'

me lo da in formato xml ovviamente ma qui ve l'ho riportato in formato stringa

questo è il mio api.rest.php

Code: Select all

//print("auth: " . $auth_code . "; rest_module: " . $rest_module . "; func: " . $rest_function );
$res = API::Execute($auth_code, $rest_module, $rest_function, $rest_subparams);

if (!$res['success']) {
	$err_msg = $res['message'] ." code: " . $auth_code . "; rest_module: " . $rest_module . "; funct: " . $rest_function ; <-- questa stringa l'ho inserita io per capire cosa prendeva.
	rest_cout(RestAPI::HandleError($err_msg, $GLOBALS['REST_API_ACCEPT']));
} else {
	rest_cout(RestAPI::HandleOutput($res, $GLOBALS['REST_API_ACCEPT']));
}
NewWay
FormaLms User
Posts: 32
Joined: Sat Mar 08, 2014 12:56 pm

Re: API

Post by NewWay »

Risolto!

Come si usano le API:
Vi mando la mia configurazione e codice di base poi ognuno può gestirsela come meglio necessita.

In formalms dal lato admin andare alla voce di menù: principale > configurazione > configurazione e nel menù a sinistra cliccare su
"forma.lms API and SSO"

e Qui mettere:
Abilita le funzionalità delle API --> si
Metodo di autenticazione: --> Generazione di un Token ad ogni connessione
Codice unico di accesso: --> scegliereUnCodiceDiAccesso
Tempo di vita del token: --> 999

Ripristina tempo di vita del token ad ogni connessione -> si
Aut. Chiave API: scegliereunaKEY --> d' ora in poi la chiamerò KEY
Aut. Codice segreto API: scegliereUnaSECRET --> d' ora in poi la chiamerò SECRET

Ora in PHP:
La prima cosa da preparare è una funzione che in base ai parametri che la chiamata api richiede (ad esempio: user/checkUsername richiede il parametro "userid") e la concatenazione di KEY e SECRET crea una stringa codificata con l'algoritmo sha1 e a sua volta codificata in base64.

Code: Select all

	
class FormaLMS {
/* function to make the hash*/
static public function getHash($params) {
	
		$key =  [b]KEY[/b];
		$secret_key = [b]SECRET[/b];
		
		$res =array('sha1'=>'', 'x_auth'=>'');

		$res['sha1']=sha1(implode(',', $params) . ',' . $secret_key);

		$res['x_auth']=base64_encode($key . ':' . $res['sha1']);
		
		return $res;
	}
Come seconda cosa da preparare è una funzione che ci prepara il protocollo HTTP. Attenzione a x_auth che sarà l'hash creato dalla precendente funzione.

Code: Select all

	static private function getDefaultHeader($x_auth) {
		 
		
		$cloudUrl =  'http://www.myplatformformalms.com/api/rest.php?q=' ;
		
		if(stripos($cloudUrl, 'http://')===false && stripos($cloudUrl, 'https://')===false)
				$cloudUrl = 'http://'.$cloudUrl;

		if(!empty($cloudUrl)){
			$host = parse_url($cloudUrl, PHP_URL_HOST);
			
			return array(
					"Host: " . ($host ? $host : ''),
					"Content-Type: multipart/form-data",
					'X-Authorization: FormaLMS ' . $x_auth
			);
		}
		
		return array();
		
	}
e infine creiamo una funzione che generi le chiamate

Code: Select all

	static public function call($action, $data_params) {

		// Import the URL from the plugin settings or from this file otherwise
		$cloudUrl = 'http://www.myplatformformalms.com/api/rest.php?q=' ;
		
		if(!$cloudUrl){
			return false;
		}else{
			$cloudUrl = trim($cloudUrl);
		}
		
		$cloudUrl = str_ireplace('http://', '', $cloudUrl);
		$cloudUrl = str_ireplace('https://', '', $cloudUrl);

		$curl = curl_init();

		$hash_info = self::getHash($data_params);
		$http_header =self::getDefaultHeader($hash_info['x_auth']);

		$opt = array(
			CURLOPT_URL=>$cloudUrl . '/api/' . $action,
			CURLOPT_RETURNTRANSFER=>1,
			CURLOPT_HTTPHEADER=>$http_header,
			CURLOPT_POST=>0,
			CURLOPT_POSTFIELDS=>$data_params,
			CURLOPT_CONNECTTIMEOUT=>5, // Timeout to 5 seconds
			CURLOPT_SSL_VERIFYPEER=>false,
			CURLOPT_SSL_VERIFYHOST=>false,
		);
		 
		curl_setopt_array($curl, $opt);

		// $output contains the output string
		$output = curl_exec($curl);
		 
		// it closes the session
		curl_close($curl);
		 
		return $output;
	}
}//end of class
Adesso...

Autentichiamoci prima sulla nostra piattaforma formaLMS, poi eseguiamo la chiamata alla api che ci interessa passandogli come parametro "auth" = token ricevuto da auth/authenticate:

Code: Select all

$testApiCall = new SimpleXMLElement(FormaLMS::call('auth/authenticate', array('username'=>'admin','password'=>'admin')));
$token = false; 

if (isset($testApiCall->success))
 {	
	echo "[+] User logged<br>";
	$token = $testApiCall->token;
	echo "[+] Set token: $token<br>";
	echo "[+] Try to gettin info with api ... ";
						 
	$testApiCall = new SimpleXMLElement(DoceboApi::call('user/checkUsername', array('userid'=>'admin','auth'=>$token)));
	if ( isset($testApiCall->success) && $testApiCall->success == true ) {
			 echo "<span style='color:green'><b>RIUSCITO</b></span><br>";
	} else {
			echo "<span style='color:red'>FALLITO</span><br>";
	}
}

Sarete felici di vedere con un var_dump($testApiCall) che oltre il success = true, c'è l'informazione che volevate ricevere :)

Adesso si che è chiaro come si fa ;)
User avatar
alberto
FormaLms Guru
Posts: 1134
Joined: Fri Mar 02, 2012 9:18 am
Contact:

Re: API

Post by alberto »

Grazie NewWay :)
GiuseppeB
Newbie
Posts: 23
Joined: Mon Aug 01, 2016 6:34 pm
Version: forma.lms 1.4.2

Re: API

Post by GiuseppeB »

La guida non è chiara, potrei avere maggiori delucidazioni?

Grazie 1000
Saluti,
GiuseppeB
Post Reply