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 2021-11-23 12:41:49

DarkAngel
Member
Registered: 2021-10-16
Posts: 10

Add item from generic object with api rest

Hello everyone

Voici un extrait de code dont je me sers pour ajouter des objets (voitures) avec le plugin generic object :

echo "creation de l'objet Voiture  ".$leaser. " ".$immat. " ".$other. "<br/>";
    $ch = curl_init();
    $url=$api_url . "/PluginGenericobjectVoiture/";
        $fields='{
    "input": {
        "name": "' .$leaser. '","serial": "'.$immat.'",,"otherserial": "'.$other.'","location_id": "1","states_id": "1","is_helpdesk_visible": "1","plugin_genericobject_voituremodels_id": "1"
    }
}';

    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);
    $car_id=$obj['id'];
echo "<br/>$car_id<br/>";
}?>

</body>
</html>


Le résultat est que ça ne marche pas. J'ai le message d'erreur suivant:

Notice: Undefined index: id in C:\xampp\htdocs\TEST\import_voiture.php on line 73

La table mysql indique bien un id pour l'objet voiture, je ne comprends pas.

Auriez-vous une idée ?

Thanks

Last edited by DarkAngel (2021-11-23 12:44:20)

Offline

#2 2021-11-23 20:36:03

DarkAngel
Member
Registered: 2021-10-16
Posts: 10

Re: Add item from generic object with api rest

Hello everyone,

Can someone answer me please ?

I just want to import some data with the api rest in order to facilitate storage of rental cars.

The plugin genericobject allowed me to create different fields.

Here is the script that i got back in this forum. It was initially written by "la denrée":

I think the line with the url doesn't match ( $url=$api_url . "/PluginGenericobjectVoiture/"; ):


</head>
<body>
<h1>import cars from csv using glpi api rest</h1>
<?php
echo "<h2>read csv file</h2>";
//initialisation des variables

$mycsvfile="tickets.csv";
$api_url="http://192.168.1.46:8080/CAR/apirest.php";
$user_token="m7S2BrA1YIEYSEGYQ3TyqILrlOOi01FPImvNDH5s";
$app_token ="jumnEAaYdmmfBFvUFWLOza6YnszjePB0Zp7pIccD";

//initialisation api glpi
$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) 
);
if (!file_exists($mycsvfile)){
    die("le fichier n'existe pas");
}
// lecture des lignes du fichier
$ligne=1;
$fic=fopen($mycsvfile, "r");
while($tab=fgetcsv($fic,1024,','))
{
$leaser=$tab[0];
$immat=$tab[1];
$other=$tab[2];





echo "creation de l'objet Voiture  ".$leaser. " ".$immat. " ".$other. "<br/>";
    $ch = curl_init();
    $url=$api_url . "/PluginGenericobjectVoiture/";
        $fields='{
    "input": {
        "name": "' .$leaser. '","serial": "'.$immat.'",,"otherserial": "'.$other.'","locations_id": "1","states_id": "1","is_helpdesk_visible": "1"
    }
}';

    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($input);
    print_r($json);
   
   
   
   
}?>

</body>
</html>

Offline

#3 2021-11-23 21:22:12

DarkAngel
Member
Registered: 2021-10-16
Posts: 10

Re: Add item from generic object with api rest

sujet clos.
J'avais une virgule de trop....
ici
"name": "' .$leaser. '","serial": "'.$immat.'",,"otherserial": "'.$other.'","locations_id": "1","states_id":

Offline

#4 2021-11-23 21:27:42

DarkAngel
Member
Registered: 2021-10-16
Posts: 10

Re: Add item from generic object with api rest

Voici le script corrigé au complet pour l'importation d'une flotte de véhicules (GLPI sait tout faire lol !)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="">
<!-- la denree -2017-  -->
<head>
    <title>REST API</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="generator" content="Bluefish 2.2.7" />
    <meta name="Keywords" content="GLPI,import API," />
    <meta name="Description" content="imports de tickets via API REST" />
    <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>import tickets from csv using GLPI API REST</h1>
<?php
echo "<h2>read csv file</h2>";
//initialisation des variables

$mycsvfile="tickets.csv";
$api_url="http://192.168.1.46:8080/CAR/apirest.php";
$user_token="m7S2BrA1YIEYSEGYQ3TyqILrlOOi01FPImvNDH5s";
$app_token ="jumnEAaYdmmfBFvUFWLOza6YnszjePB0Zp7pIccD";

//initialisation api glpi
$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) 
);
if (!file_exists($mycsvfile)){
    die("le fichier n'existe pas");
}
// lecture des lignes du fichier
$ligne=1;
$fic=fopen($mycsvfile, "r");
while($tab=fgetcsv($fic,1024,','))
{
$leaser=$tab[0];
$immat=$tab[1];
$other=$tab[2];





echo "creation de l'objet Voiture  ".$leaser. " ".$immat. " ".$other. "<br/>";
    $ch = curl_init();
    $url=$api_url . "/PluginGenericobjectVoiture/";
        $fields='{
    "input": {
        "name": "' .$leaser. '","serial": "'.$immat.'","otherserial": "'.$other.'","locations_id": "1","states_id": "1","is_helpdesk_visible": "1"
    }
}';

    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);
    $car_id=$obj['id'];
    echo "<br/>$car_id<br/>";
   
   
   
   
}?>

</body>
</html>

Offline

Board footer

Powered by FluxBB