The Elearning Community • API
Page 1 of 4

API

Posted: Wed Feb 24, 2016 10:03 am
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.

Re: API

Posted: Wed Feb 24, 2016 6:45 pm
by max
Ciao,
quelle che hai linkato sono le API di Docebo Cloud...

Re: API

Posted: Thu Feb 25, 2016 12:53 pm
by NewWay
Grazie max... e quindi ? :cry:

Re: API

Posted: Thu Feb 25, 2016 1:06 pm
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

Re: API

Posted: Fri Feb 26, 2016 10:22 am
by NewWay
Grazie mille poi magari scrivi qui la url almeno chi passa per qua trova tutto :) ;)

Re: API

Posted: Wed Mar 02, 2016 2:42 pm
by alberto
Ciao, ecco QUI un po' di documentazione


a presto!

Re: API

Posted: Mon Mar 14, 2016 12:20 pm
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']));
}

Re: API

Posted: Tue Mar 15, 2016 11:12 am
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 ;)

Re: API

Posted: Sat Mar 19, 2016 8:14 am
by alberto
Grazie NewWay :)

Re: API

Posted: Thu Apr 06, 2017 11:13 am
by GiuseppeB
La guida non è chiara, potrei avere maggiori delucidazioni?

Grazie 1000

This site uses cookies.

Some of the cookies we use are essential for parts of the site to operate and have already been set. We also use Google Analytics scripts, which all use cookies.
You may delete or block all cookies from this site in your browser options.