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 2024-04-17 10:38:15

RIMuser
Member
Registered: 2024-02-23
Posts: 6

Associate an uploaded Document to a Ticket

Hello,

I've read for hours this forum and couldn't find any reply to my problem.
In PHP, I can create a ticket, upload a Document. But when I try to attach it, it doesn't work.

I've been through many option but I'm stuck on error ERROR_GLPI_ADD. Here is my code :

I believe this is a problem with url ?

// Création du fichier (enregistrement dans Documents)
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uploadUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "session-token: $sessionToken",
            "App-Token: $appToken"
        ));
        $filePath = realpath($fileTmpName);

        $postFields = array(
            'uploadManifest' => json_encode([
                'input' => [
                    'name' => $fileName,
                    // 'documentcategories_id' => 5, // Set appropriate category ID
                    'tickets_id' => $ticketId // Attach to the newly created ticket
                ]
            ]),
            'filename[0]' => new CURLFile($filePath, $fileType, $fileName)
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $uploadResponse = curl_exec($ch);
        curl_close($ch);

        $documentId = json_decode($uploadResponse)->id;


        // Création de la relation ticket / document
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $createUrl.'Ticket/'.$ticketId.'/Document_Item');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "session-token: $sessionToken",
            "App-Token: $appToken",
            "Content-Type: application/json"
        ));

        $associationData = json_encode([
            'input' => [
                'documents_id' => $documentId,
                'itemtype' => 'Ticket',
                'items_id' => $ticketId
            ]
        ]);
        echo json_encode(['contenu attached' => $associationData]);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $associationData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);

        echo json_encode(['Réponse attached' => $response]);

Last edited by RIMuser (2024-04-17 11:29:36)

Offline

#2 2024-04-17 15:33:21

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,424
Website

Re: Associate an uploaded Document to a Ticket

Pouvez-vous essayer $createUrl.'Document_Item' au lieu de $createUrl.'Ticket/'.$ticketId.'/Document_Item' ?

De plus, y a-t-il des informations dans les journaux GLPI (dossier files/_log) au moment de l'erreur ?


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

Board footer

Powered by FluxBB