You are not logged in.
Pages: 1
Hiiiiiiiiiiii bro's \o/
The point is, I'm trying to make the connection to the GLPI API, I already managed using curl, I could create calls, leave them as solved and etc .. However I am now wanting to pass this to PHP, and based on the script I tried initialize the session but returns me an error.
<?php
$api_url="http://172.16.1.193/apirest.php";
$user_token="pwcugR7VROoAP2YQCLf0upZaLsGgQe6vAq214wv7";
$app_token="1jDOUGrIqydC77gZVJloEcEG46EW63iuauwWh";
$ch = curl_init();
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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
);
?>
The code returns this to me:
PHP Notice: Undefined index: session_token in /home/xx/test.php on line 12
the line 12 is "$sess_token = $obj['session_token']; "
): help me pls,
any solution or alternative?
Last edited by wtralui (2019-12-18 15:54:10)
Offline
somethings is missing in URL :
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$
should be :
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$app_token&user_token=$user_token
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
somethings is missing in URL :
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$
should be :
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$app_token&user_token=$user_token
oh ok
I left the code as follows
<?php
$api_url="http://172.16.1.193/glpi/apirest.php";
$user_token="pwcugR7VROoAP2YQCLf0upZaLsGgQe6vAq214wv7";
$app_token="1jDOUGrIqydC77gZVJloEcEG46EW63iuauwWh";
$ch = curl_init();
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$app_token ."&user_token=".$user_token;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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
);
?>
when executing it does not return anything to me, you know how to do it for me to return the visibility session token
I tried something like:
print $sess_token;
print $obj;
however still does not return anything to me.
I have another question, regarding the creation of ticket with the code in PHP, will I send it here or create another ticket?
Offline
is your API active ?
in your browser type : http://172.16.1.193/glpi/apirest.php => it should return API help page.
add "print_r($json)" in your code, it might return some error code..
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
for debug you can also add :
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print_r($httpcode);
before "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
is your API active ?
Yeapp, in localhost
add "print_r($json)" in your code, it might return some error code..
thanks, return for me "{"session_token":"faop3893q369ukpsof44diqkai"}"
would you tell me a way to pass this script (ticket creation and session closing) to PHP (I'm not so connected to php, so the difficulty)?
curl -H POST \
'http://172.16.1.193/apirest.php/Ticket/' \
-H 'App-Token: 1jDOUGrIqydC77gZVJloEcEG46EW63iuauwWhImE' \
-H 'Content-Type: application/json' \
-H 'Cookie: glpi_token' \
-H 'Host: localhost' \
-H 'Session-Token: '${APISESSION}'' \
-d '{
"input": {
"name": "1",
"content": "testeeeedasdeeeeeeeeeeee 1",
"status":"5",
"urgency":"1",
"priority":"4",
"resquesttypes_id":"5",
"itilcategories_id":"93",
"_users_id_requester":"43"
}
}'
curl -X GET \
-H 'Content-Type: application/json' \
-H 'Session-Token: '${APISESSION}'' \
-H "App-Token: 1jDOUGrIqydC77gZVJloEcEG46EW63iuauwWhImE" \
'http://172.16.1.193/apirest.php/killSession'
Offline
thanks, return for me "{"session_token":"faop3893q369ukpsof44diqkai"}" => it means it works.
and print_r($headers); should return an array.
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
//first initsession to get session token assuming that users profile allows ticket creation;
$api_url="http://172.16.1.193/glpi/apirest.php";
$user_token="pwcugR7VROoAP2YQCLf0upZaLsGgQe6vAq214wv7";
$app_token="1jDOUGrIqydC77gZVJloEcEG46EW63iuauwWh";
$ch = curl_init();
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$app_token ."&user_token=".$user_token;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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
);
//once $header is set, create ticket
$ch = curl_init();
$url=$api_url . "/Ticket/";
$fields={
"input": {
"name": "1",
"content": "testeeeedasdeeeeeeeeeeee 1",
"status":"5",
"urgency":"1",
"priority":"4",
"resquesttypes_id":"5",
"itilcategories_id":"93",
"_users_id_requester":"43"
}
}';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$request_result = curl_exec($ch);
curl_close ($ch);
$obj = json_decode($request_result,true);
print_r($obj);
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
//first initsession to get session token assuming that users profile allows ticket creation;
$api_url="http://172.16.1.193/glpi/apirest.php";
$user_token="pwcugR7VROoAP2YQCLf0upZaLsGgQe6vAq214wv7";
$app_token="1jDOUGrIqydC77gZVJloEcEG46EW63iuauwWh";
$ch = curl_init();
$url=$api_url . "/initSession?Content-Type=%20application/json&app_token=".$app_token ."&user_token=".$user_token;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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
);
//once $header is set, create ticket$ch = curl_init();
$url=$api_url . "/Ticket/";
$fields={
"input": {
"name": "1",
"content": "testeeeedasdeeeeeeeeeeee 1",
"status":"5",
"urgency":"1",
"priority":"4",
"resquesttypes_id":"5",
"itilcategories_id":"93",
"_users_id_requester":"43"
}
}';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$request_result = curl_exec($ch);
curl_close ($ch);$obj = json_decode($request_result,true);
print_r($obj);
Thank you so much for the time you have taken to answer me, I had managed to create the script with such a function to open the call before it even answered me .. however, there is a new problem regarding php
in this line of code:
$input='{ "input": {
"name": "test",
"content": "testeeePHP",
"status":"5",
"urgency":"1",
"priority":"4",
"resquesttypes_id":"5",
"itilcategories_id":"93",
"_users_id_requester":"20"
In the case at the beginning of the code I put a while:
$i = 6;
while($i < 10){
[...]
$i++;
}
because I want to open more than one call at a time (so while)
however I want to be counted by name, example:
called 1: called open automatically via script "1"
called 2: called open automatically via script "2"
called 3: called open automatically via script "3"
So I did as follows:
Since while counting from $ i (starting from 0), I changed the code as follows:
$input='{ "input": {
"name": '.$i.',
"content": "testeee",
"status":"5",
[...]
So the title of the call will always be the numbering equivalent to the value of $ i, so far so good, the problem is that I could not add a text to be something like "title called test 1", "title called test 2"
would you have any idea how this could be accomplished in PHP? ):
Sorry for bad english, i'm using google translator :x
Offline
"name": " called open automatically via script '.$i.' ",
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
"name": " called open automatically via script '.$i.' ",
I remember making a similar attempt, so simple and silly that I don't know why I couldn't, I think I was having a problem where I didn't have
but anyway, thanks for clearing up any doubts <3
appreciate it
Offline
Pages: 1