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?
]]>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);
$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);
if (!isset($obj['session_token'])){
//logged out right?
}
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=
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 !
]]>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
]]>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"}
]]>line 21:
$sess_token = $obj[session_token];
so, where do i define "session_token"?
]]>