Forum

How to add a new oj...
 
Notifications
Retirer tout

How to add a new oject with the API

2 Posts
1 Utilisateurs
0 Reactions
1,232 Vu
(@caie_guens)
Posts: 10
Active Member
Début du sujet
 
[#9764]

Hi all,
I tested many things but all never do what I want.
I want to Client with the API, client come from another DB but I' made a script test
$apiUrl = 'http://serveur/projeqtor/api/Client'; // Données du nouveau client
$data = array('name' => 'ZORRO','description' => 'Description du client','email' => 'email@client.com'); // Convertir les données en JSON
$jsonData = json_encode($data); // Clé de cryptage (doit être de 32 octets pour AES-256)
$encryptionKey = '4c91fbdc52cb573f6625adb2a407dbe3'; // Générer un IV (Initialisation Vector) aléatoire
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); // Chiffrer les données
$encryptedData = openssl_encrypt($jsonData, 'aes-256-cbc', $encryptionKey, 0, $iv); // Encoder les données chiffrées et l'IV en base64 pour les transmettre
$encryptedData = base64_encode($encryptedData);$iv = base64_encode($iv); // Préparer les données à envoyer
$postData = json_encode(array('data' => $encryptedData,'iv' => $iv)); // Identifiants API
$apiUser = 'projapi';$apiPassword = 'i5yMUwLdeYD356';
 // Initialiser cURL
$ch = curl_init($apiUrl); 
// Configurer les options cURLcurl_
setopt
($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);curl_setopt($ch, CURLOPT_USERPWD, "$apiUser:$apiPassword"); // Exécuter la requête et obtenir la réponse$response = curl_exec($ch); // Vérifier les erreurs cURLif ($response === false) {die('Erreur cURL : ' . curl_error($ch));} // Fermer cURLcurl_close($ch); // Afficher la réponseecho 'Réponse de l'API : ' . $response; 

where is the problem ?
Thanks
Guénolé


 
Posté : 27/02/2025 4:29 pm
(@caie_guens)
Posts: 10
Active Member
Début du sujet
 

Hi,
Iwant to add a ticket for example with the API.
This is the return :
root@vm-dolibarr:/home/http/API_ProjeqtorV1# php projATicket.php
Données avant chiffrement : {"id":"1", "name":"name to be changed for Ticket 1"}

Données après chiffrement : HAPtP0atymdmP0NsWMm00LuBzh/4/eVf/JNqbZPAjE1alzxmYa3KHYY07hggQpaOH+ciAv7Gtk0nd8Spt3acZ+x3qQEgIg==

Réponse décryptée : "{"id":"1", "name":"name to be changed for Ticket 1"}"
Json Payload : {"data":"HAPtP0atymdmP0NsWMm00LuBzh/4/eVf/JNqbZPAjE1alzxmYa3KHYY07hggQpaOH+ciAv7Gtk0nd8Spt3acZ+x3qQEgIg=="}
*   Trying 192.168.12.10:80...
* TCP_NODELAY set
* Connected to 192.168.12.10 (192.168.12.10) port 80 (#0)
* Server auth using Basic with user 'projapi'
> POST /projeqtor/api/Ticket HTTP/1.1
Host: 192.168.12.10
Authorization: Basic cHJvamFwaTppNXlNVXdMZGVZRDM1Ng==
Accept: */*
Content-Type: application/json
Content-Length: 110

* upload completely sent off: 110 out of 110 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Fri, 07 Mar 2025 08:24:38 GMT
< Server: Apache/2.4.41 (Ubuntu)
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
< Set-Cookie: PHPSESSID=p3e8v1e9tpdn365tjj0bq5uusu; path=/
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=UTF-8
<
* Connection #0 to host 192.168.12.10 left intact
Réponse de l'API : {"status":"success","message":"Donnu00e9es reu00e7ues","data":"{"id":"1", "name":"name to be changed for Ticket 1"}"}{"error":"invalid API query", "message":""'data' is not correctly encoded for method POST. Request for correct API KEY""}

Could someone help please ?
Thanks
Guenole


 
Posté : 07/03/2025 11:29 am
Share:
Retour en haut