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 2019-12-18 15:53:03

wtralui
Member
Registered: 2019-12-07
Posts: 25

connection with API using PHP

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

#2 2019-12-18 16:06:52

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,167

Re: connection with API using PHP

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.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#3 2019-12-18 16:25:51

wtralui
Member
Registered: 2019-12-07
Posts: 25

Re: connection with API using PHP

LaDenrée wrote:

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

#4 2019-12-18 16:34:35

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,167

Re: connection with API using PHP

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.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#5 2019-12-18 16:52:33

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,167

Re: connection with API using PHP

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.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#6 2019-12-18 16:54:11

wtralui
Member
Registered: 2019-12-07
Posts: 25

Re: connection with API using PHP

LaDenrée wrote:

is your API active ?

Yeapp, in localhost

LaDenrée wrote:

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

#7 2019-12-18 17:00:55

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,167

Re: connection with API using PHP

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.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#8 2019-12-18 17:08:59

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,167

Re: connection with API using PHP

//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.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#9 2019-12-19 15:22:45

wtralui
Member
Registered: 2019-12-07
Posts: 25

Re: connection with API using PHP

LaDenrée wrote:

//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

#10 2019-12-19 15:27:31

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,167

Re: connection with API using PHP

"name": " called open automatically via script     '.$i.'  ",


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#11 2019-12-19 15:36:39

wtralui
Member
Registered: 2019-12-07
Posts: 25

Re: connection with API using PHP

LaDenrée wrote:

"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

Board footer

Powered by FluxBB