You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2018-06-19 16:55:25

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

how to login into glpi using API Rest

hi all, i've been reading a lot, but i can´t find how to make a login page (external) and check user and password with GLPI users database. how can i make something like this?

i'll be really glad if anyone can helpme out with this.

regards!

Offline

#2 2018-06-19 17:58:44

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

first activate glpi API ( setup>general>API)

create login form  : login.form.php

<?php
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="fr" lang="fr">
<!-- la denree -2018-  -->
<head>
	<title>GLPI Login FORM</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
	<meta content="width=device-width, initial-scale=1" name="viewport" />
	<meta name="generator" content="Dev-PHP 2.6.1" />
	<meta name="Keywords" content="GLPI, API REST,LOGINFORM" />
	<meta name="Description" content="login form with GLPI" />
	<meta http-equiv="X-UA-Compatible" content="IE=8" />
	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />	

</head>';
echo '<body  style="min-height: 579px;">';

?>
			
				<form method="post"action="login.php" method="POST" name="glpi_form" enctype="multipart/form-data">
				
					<input title="type in your GLPI  Login." type="text" placeholder="LOGIN"  name="LOGIN" id="LOGIN">
				<input autocomplete="on" id="Password" name="Password" placeholder="type in your GLPI PASSWORD" type="password">

					
					<button type="submit" id="boutonSUBMIT" value="Login" title="Clic here to log" ; ><span>LOGIN</span></button>
					
					</form>
					






<?php 



echo "</body></html>";

?>

create login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- la denree -2018-  -->
<head>
	<title>TEST API LOGIN</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta name="Generator" content="Dev-PHP 2.6.1" />
	<meta name="Keywords" content="GLPI,LOGIN," />
	<meta name="Description" content="Loginform" />
	<meta http-equiv="X-UA-Compatible" content="IE=8" />
	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />


</head>
<body>
<h1>test api</h1>
<?php

	$api_url="localhost/glpi/apirest.php";
	$app_token="myAppToken";

$token=base64_encode($_POST['LOGIN'].":".$_POST['Password']
);
	$url=$api_url ."/initSession/";
	$headers = array(
	('Content-Type: application/json'),
	('App-Token: ' . $app_token),
	('Authorization: Basic '.$token)
	);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	$json = curl_exec($ch); 
	curl_close ($ch); 
	$obj = json_decode($json,true); 
	
	$sess_token = $obj['session_token'];

	$headers = array('Content-Type: application/json',
			'App-Token : '.$app_token,
			'Session-Token: '.$sess_token);
 		$_SESSION['valid'] = true;
              	$_SESSION['timeout'] = time();
		$_SESSION['headers']=$headers;
		$_SESSION['sess_token']=$sess_token;

print_r($json);
echo("<br/>");
print_r($headers);
echo("<br/>");

?>
</body>
</html>

edit  : quotes added and 2nd curl_close ($ch);  deleted

Last edited by LaDenrée (2018-06-19 23:30:00)


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#3 2018-06-19 18:55:19

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

great!..! thanks a lot! i'll try it and comment here. thanks!..!

Offline

#4 2018-06-19 22:31:25

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

so, i tried your code, and there was an error closing 2 times $ch (curl_close ($ch);) so i deleted the first one, and then gave me this error:
"Use of undefined constant session_token - assumed 'session_token' (this will throw an Error in a future version of PHP) in C:\..\login_glpi.php on line 21"

line 21:
$sess_token = $obj[session_token];

so, where do i define "session_token"?

Offline

#5 2018-06-19 23:22:22

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

sorry this was an abstract from my code  and ther might be some mistypes by simplifying it
my token where stored in session variables and I forgot to modify it in this example

the api call returns an answer ($json) converted into an array : ($obj)


so write line 21
$sess_token = $obj['session_token'];
with quotes

did you get an anwser with print_r($json) ? it should look like : {"session_token":"b6eu19j5tg6hd5pmmm48ln0lj6"}

Last edited by LaDenrée (2018-06-19 23:43:37)


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#6 2018-06-20 04:43:46

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

yes it does, so the login is correct right?

but when i enter a wrong password, gave me another error on the same line:

Undefined index: session_token in C:\..\login_glpi.php on line 21
["ERROR_GLPI_LOGIN",""]

sorry man for my noob questions hahaha. i'm new on glpi and in the "token" thing XD

Last edited by spawnsx (2018-06-20 04:53:04)

Offline

#7 2018-06-20 08:33:16

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

if the login is correct you should get a session token=> win ! you re logged in
if the login/pw is incorrect => failed ! you get an error message so you can't get  $obj['session_token'];   make test ( if  (isset( $obj['session_token']))   and diplay user friendly message or continue.

keep this session token (it is different for each user and changes at each  login)

I keep it it an array  called $headers  or in $_SESSION['headers']

$headers = array('Content-Type: application/json',
            'App-Token : '.$app_token,
            'Session-Token: '.$sess_token);

now with this $header, $api_url you can call other glpi function : get a computer, create ticket, update user, delete monitor,...

enjoy !


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#8 2018-06-20 10:17:49

orthagh
Administrator
From: TECLIB - CAEN
Registered: 2010-11-30
Posts: 548
Website

Re: how to login into glpi using API Rest

Here is some resources on my personal space:
- https://github.com/orthagh/glpi_boostrap_api
- https://github.com/orthagh/glpi_api_upload

The two uses guzzle library to send http queries.

Additionally, some people contributes to official organization with language libraries:
https://github.com/glpi-project?utf8=%E … &language=

Offline

#9 2018-06-27 23:09:09

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

first, thanks for all your help. really. thanks. can i ask one more question? how do i know that i logout the user? i'm using killsession, but how do i get the response of killsession?maybe:

if (!isset($obj['session_token'])){
//logged out right?
}

Offline

#10 2018-06-28 09:17:38

orthagh
Administrator
From: TECLIB - CAEN
Registered: 2010-11-30
Posts: 548
Website

Re: how to login into glpi using API Rest

for KillSession, you need to check the return code of the http answer:
- 200 -> ok, session killed
- 400 -> not ok, bad request, the only way to failed (apart a bug) is to have missed to fill the session token or sending a bad session token.

Offline

#11 2018-07-06 19:44:07

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

to get http answer :

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	$json = curl_exec($ch); 

      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	curl_close ($ch);
 

Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#12 2018-07-30 22:32:12

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

hi, it's me again. so, i can login, but fails to understand the rest of api usage. can you give me an example for adding an active? or a contract? using API please.

Offline

#13 2018-07-31 09:46:46

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

once you have logged in and stored your  session headers ($headers) :

echo "create contract<br/>";
$fields='{"input": {"entities_id":2
		,"is_recursive":0
		,"name":" MyNewContractByAPI"
		,"num":10.341
		,"contracttypes_id":14
		,"begin_date":"2017-12-08"
		,"duration":48
		,"notice":1
		,"periodicity":12
		,"billing":12
		,"comment":""
		,"accounting_number":""
		,"is_deleted":0
		,"week_begin_hour":"00:00:00"
		,"week_end_hour":"00:00:00"
		,"saturday_begin_hour":"00:00:00"
		,"saturday_end_hour":"00:00:00"
		,"use_saturday":0
		,"monday_begin_hour":"00:00:00"
		,"monday_end_hour":"00:00:00"
		,"use_monday":0
		,"max_links_allowed":0
		,"alert":12
		,"renewal":2
		,"template_name":null
		,"is_template":0
		}
	}';

$url = $api_url . "/Contract/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);

$json = curl_exec($ch);
curl_close ($ch);

$obj = json_decode($json,true);
print_r($json);

Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#14 2018-08-01 18:51:34

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

great. thanks for the example. i understand now. but i have one more question. if i created an active like "CCTV" with the plugin "generic object creator". how do i use the api? on the url i mean, because the $fields i have to provide them.
will it be something like... "$api_url."/CCTV/" or "$api_url."plugin_genericobject_cctv""??

Offline

#15 2018-08-01 22:22:02

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

Sorry  i can not help . I dont use this plugin.
Anyone else?


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#16 2018-08-02 23:58:12

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

well, meanwhile, how do i know the ID of the data saved? with $httpcode['id']?

Offline

#17 2018-08-03 08:28:20

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,150

Re: how to login into glpi using API Rest

ID of the created object is stored in $obj['id']


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.1  en test (ubuntu 16.04 mariadb 10.2.4)

Offline

#18 2018-08-03 17:01:13

spawnsx
Guest
Registered: 2018-05-08
Posts: 10

Re: how to login into glpi using API Rest

thanks for your answer. its really helpfull :-).

i've tried to create a contract, but gave me "error 400", don't know why. here's my code: (i've checked the "num" column is varchar, so it should be correct with the "")

$url = $api_url."/Contract/";

$fields = '{ "input": {"entities_id":0,"is_recursive":0,"name":"prueba","num":"12345678888","contracttypes_id":0,"begin_date":"2017-11-03",
                                "duration":30,"notice":0,"periodicity":0,"billing":0,"is_deleted":0,"week_begin_hour":"00:00:00"
                                ,"week_end_hour":"00:00:00"
                                ,"saturday_begin_hour":"00:00:00"
                                ,"saturday_end_hour":"00:00:00"
                                ,"use_saturday":0
                                ,"monday_begin_hour":"00:00:00"
                                ,"monday_end_hour":"00:00:00"
                                ,"use_monday":0
                                ,"max_links_allowed":0
                                ,"alert":0
                                ,"renewal":0
                                ,"template_name":null
                                ,"is_template":0
                            }
                        }';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $_SESSION['headers']);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

        $json = curl_exec($ch);
        $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
       
        curl_close($ch);
        $obj = json_decode($json,TRUE);

the error says "your browser sent a request that this server could not understand." error code 400. so maybe is the $url?

Last edited by spawnsx (2018-08-03 17:31:02)

Offline

Board footer

Powered by FluxBB