You are not logged in.
Pages: 1
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
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...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
great!..! thanks a lot! i'll try it and comment here. thanks!..!
Offline
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
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...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
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
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...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
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
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
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
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...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
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
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...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
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
Sorry i can not help . I dont use this plugin.
Anyone else?
Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
well, meanwhile, how do i know the ID of the data saved? with $httpcode['id']?
Offline
ID of the created object is stored in $obj['id']
Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1 Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9
Offline
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
Pages: 1