├── .gitignore ├── public ├── image-1.png ├── image-2.png ├── image-3.png ├── image-4.png └── image.png ├── src ├── files │ └── support3.png └── GLPIApi.php ├── composer.json ├── index.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | teste.php 2 | vendor 3 | composer.lock -------------------------------------------------------------------------------- /public/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri-spm/php-api-glpi/HEAD/public/image-1.png -------------------------------------------------------------------------------- /public/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri-spm/php-api-glpi/HEAD/public/image-2.png -------------------------------------------------------------------------------- /public/image-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri-spm/php-api-glpi/HEAD/public/image-3.png -------------------------------------------------------------------------------- /public/image-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri-spm/php-api-glpi/HEAD/public/image-4.png -------------------------------------------------------------------------------- /public/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri-spm/php-api-glpi/HEAD/public/image.png -------------------------------------------------------------------------------- /src/files/support3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri-spm/php-api-glpi/HEAD/src/files/support3.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "config": { 3 | "vendor-dir": "vendor" 4 | }, 5 | "autoload": { 6 | "psr-4": { 7 | "src\\": "src/" 8 | } 9 | }, 10 | "require": { 11 | "guzzlehttp/guzzle": "^7.9" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | initSession(); 16 | 17 | ob_start(); 18 | var_dump($sessionData); 19 | $varDumpOutput = ob_get_clean(); 20 | 21 | GLPIApi::render($varDumpOutput); 22 | 23 | 24 | //Find Ticket 25 | 26 | $findTicket = $glpiApi->requestItem('Ticket',9); 27 | ob_start(); 28 | var_dump($findTicket); 29 | $findTicketOutput = ob_get_clean(); 30 | 31 | GLPIApi::render($findTicketOutput); 32 | 33 | //Create Ticket 34 | $data = [ 35 | "input" => [ 36 | "entities_id" => 0, 37 | "name" => "Titulo do meu chamado Criado pela API", 38 | "content" => "Titulo do meu chamado Criado pela API", 39 | "_users_id_requester" => 1, 40 | ] 41 | ]; 42 | $createTicket = $glpiApi->addItem('Ticket', $data); 43 | ob_start(); 44 | var_dump($createTicket); 45 | $createTicketOutput = ob_get_clean(); 46 | 47 | GLPIApi::render($createTicketOutput); 48 | 49 | 50 | //Create Ticket Anonymus Ticket 51 | $data = [ 52 | "input" => [ 53 | "name" => "Abertura de chamado anonimo", 54 | "content" => "Abertura de chamado anonimo", 55 | "_skip_auto_assign" => true, 56 | "requesttypes_id" => 1, 57 | "type" => 2, 58 | "itilcategories_id" => 2, 59 | "_actors" => [ 60 | "requester" => [ 61 | [ 62 | "itemtype" => "User", 63 | "use_notification" => 1, 64 | "alternative_email" => "teste2@gmail.com" 65 | ] 66 | ] 67 | ] 68 | ] 69 | ]; 70 | 71 | 72 | $createTicket = $glpiApi->addItem('Ticket', $data); 73 | ob_start(); 74 | var_dump($createAnonymusTicket); 75 | $createTicketOutput = ob_get_clean(); 76 | 77 | GLPIApi::render($createAnonymusTicket); 78 | 79 | //Update Ticket 80 | $data = [ 81 | "input" => [ 82 | "_users_id_requester" => 6 83 | ] 84 | ]; 85 | $updateTicket = $glpiApi->updateItem('Ticket',201, $data); 86 | ob_start(); 87 | var_dump($updateTicket); 88 | $updateTicketOutput = ob_get_clean(); 89 | 90 | GLPIApi::render($updateTicketOutput); 91 | 92 | //Delete Ticket 93 | $deleteTicket = $glpiApi->deleteItem('Ticket', 2406200096); 94 | ob_start(); 95 | var_dump($deleteTicket); 96 | $deleteTicketOutput = ob_get_clean(); 97 | 98 | GLPIApi::render($deleteTicketOutput); 99 | 100 | //Purge Ticket 101 | $purgeTicket = $glpiApi->purgeItem('Ticket', 2406200096); 102 | ob_start(); 103 | var_dump($purgeTicket); 104 | $purgeTicketOutput = ob_get_clean(); 105 | 106 | GLPIApi::render($purgeTicketOutput); 107 | 108 | //Send File 109 | $file = 'src/files/support3.png'; 110 | $sendDocuments = $glpiApi->sendDocuments('Document', $file, 'Suporte.png'); 111 | 112 | ob_start(); 113 | var_dump($sendDocuments); 114 | $sendDocumentsOutput = ob_get_clean(); 115 | 116 | GLPIApi::render($sendDocumentsOutput); 117 | 118 | //Kill Session 119 | $killSessionData = $glpiApi->killSession(); 120 | ob_start(); 121 | var_dump($killSessionData); 122 | $killSessionOutput = ob_get_clean(); 123 | 124 | GLPIApi::render($killSessionOutput); 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Integration Code with GLPi API 🚀 2 | 3 | If you liked the repository, please add a star to help out. 4 | 5 | This test program was designed to perform various essential functionalities using the GLPI API. 6 | 7 | ## ➡️ Installation 8 | 9 | 1. Install the program inside the /var/www/html folder or analyze the directory where PHP is installed for rendering. 10 | 2. Execute the composer dump-autoload command to download the necessary libraries. 11 | 12 | ```shell 13 | composer dump-autoload 14 | ``` 15 | 16 | 17 | ## ➡️ Code Functionalities 18 | 19 | 1. **Initiate session:** initSession(); 20 | 21 | ```php 22 | $apiUrl = 'url'; 23 | $userToken = 'userToken'; 24 | $appToken = 'appToken'; 25 | 26 | 27 | $glpiApi = new GLPIApi($apiUrl, $userToken, $appToken); 28 | $sessionData = $glpiApi->initSession(); 29 | 30 | ob_start(); 31 | var_dump($sessionData); 32 | $varDumpOutput = ob_get_clean(); 33 | 34 | GLPIApi::render($varDumpOutput); 35 | ``` 36 | 2. **End sessão:** killSession() 37 | 38 | ```php 39 | $killSessionData = $glpiApi->killSession(); 40 | ob_start(); 41 | var_dump($killSessionData); 42 | $killSessionOutput = ob_get_clean(); 43 | 44 | GLPIApi::render($killSessionOutput); 45 | ``` 46 | 47 | 3. **Request:** requestItem() 48 | 49 | ```php 50 | $findTicket = $glpiApi->requestItem('Ticket',9); 51 | ob_start(); 52 | var_dump($findTicket); 53 | $findTicketOutput = ob_get_clean(); 54 | 55 | GLPIApi::render($findTicketOutput); 56 | ``` 57 | 58 | 4. **Add:** addItem() 59 | 60 | ```php 61 | $data = [ 62 | "input" => [ 63 | "entities_id" => 0, 64 | "name" => "Titulo do meu chamado Criado pela API", 65 | "content" => "Titulo do meu chamado Criado pela API", 66 | "_users_id_requester" => 1, 67 | ] 68 | ]; 69 | $createTicket = $glpiApi->addItem('Ticket', $data); 70 | ob_start(); 71 | var_dump($createTicket); 72 | $createTicketOutput = ob_get_clean(); 73 | 74 | GLPIApi::render($createTicketOutput); 75 | ``` 76 | 77 | 78 | 5. **Update:** updateItem() 79 | ```php 80 | $data = [ 81 | "input" => [ 82 | "_users_id_requester" => 6 83 | ] 84 | ]; 85 | $updateTicket = $glpiApi->updateItem('Ticket',2406200096, $data); 86 | ob_start(); 87 | var_dump($updateTicket); 88 | $updateTicketOutput = ob_get_clean(); 89 | 90 | GLPIApi::render($updateTicketOutput); 91 | ``` 92 | 93 | 6. **Soft delete (move to trash):** deleteItem() 94 | ```php 95 | $deleteTicket = $glpiApi->deleteItem('Ticket', 2406200096); 96 | ob_start(); 97 | var_dump($deleteTicket); 98 | $deleteTicketOutput = ob_get_clean(); 99 | 100 | GLPIApi::render($deleteTicketOutput); 101 | ``` 102 | 7. **Hard delete (permanetly):** purgeItem() 103 | ```php 104 | $purgeTicket = $glpiApi->purgeItem('Ticket', 2406200096); 105 | ob_start(); 106 | var_dump($purgeTicket); 107 | $purgeTicketOutput = ob_get_clean(); 108 | 109 | GLPIApi::render($purgeTicketOutput); 110 | ``` 111 | 8. **Document submission:** sendDocuments() 112 | ```php 113 | $file = 'src/files/support3.png'; 114 | $sendDocuments = $glpiApi->sendDocuments('Document', $file, 'Suporte.png'); 115 | 116 | ob_start(); 117 | var_dump($sendDocuments); 118 | $sendDocumentsOutput = ob_get_clean(); 119 | 120 | GLPIApi::render($sendDocumentsOutput); 121 | ``` 122 | 123 | * Bonus anonymus ticket 124 | 125 | ```php 126 | $data = [ 127 | "input" => [ 128 | "name" => "Abertura de chamado anonimo", 129 | "content" => "Abertura de chamado anonimo", 130 | "_skip_auto_assign" => true, 131 | "requesttypes_id" => 1, 132 | "type" => 2, 133 | "itilcategories_id" => 2, 134 | "_actors" => [ 135 | "requester" => [ 136 | [ 137 | "itemtype" => "User", 138 | "use_notification" => 1, 139 | "alternative_email" => "teste2@gmail.com" 140 | ] 141 | ] 142 | ] 143 | ] 144 | ]; 145 | 146 | 147 | $createTicket = $glpiApi->addItem('Ticket', $data); 148 | ob_start(); 149 | var_dump($createAnonymusTicket); 150 | $createTicketOutput = ob_get_clean(); 151 | 152 | GLPIApi::render($createAnonymusTicket); 153 | ```` 154 | 155 | 156 | 157 | 158 | #glpi #api #php 159 | -------------------------------------------------------------------------------- /src/GLPIApi.php: -------------------------------------------------------------------------------- 1 | apiUrl = $apiUrl; 18 | $this->userToken = $userToken; 19 | $this->appToken = $appToken; 20 | $this->sessionToken = $sessionToken; 21 | } 22 | 23 | 24 | public function sendRequest($method, $endpoint, $headers = [], $body = null) 25 | { 26 | $client = new Client(); 27 | $url = $this->apiUrl . $endpoint; 28 | 29 | $options = [ 30 | 'headers' => $headers 31 | ]; 32 | 33 | if ($body) { 34 | $options['body'] = json_encode($body); 35 | } 36 | 37 | try { 38 | $response = $client->request($method, $url, $options); 39 | $responseBody = json_decode($response->getBody()); 40 | 41 | return $responseBody; 42 | } catch (RequestException $e) { 43 | if ($e->hasResponse()) { 44 | return json_decode($e->getResponse()->getBody()->getContents()); 45 | } 46 | return json_decode($e->getMessage()); 47 | } 48 | } 49 | 50 | 51 | public function initSession() 52 | { 53 | $client = new Client(); 54 | 55 | $headers = [ 56 | 'Content-Type' => 'application/json', 57 | ]; 58 | 59 | $body = json_encode([ 60 | 'app_token' => $this->appToken, 61 | 'user_token' => $this->userToken 62 | ]); 63 | 64 | $uri = $this->apiUrl . '/initSession'; 65 | 66 | try { 67 | $request = new Request('POST', $uri, $headers, $body); 68 | $response = $client->send($request); 69 | $responseBody = json_decode($response->getBody()->getContents(), true); 70 | 71 | if (isset($responseBody['session_token']) && !empty($responseBody['session_token'])) { 72 | $this->sessionToken = $responseBody['session_token']; 73 | 74 | return json_encode([ 75 | 'status' => 'success', 76 | 'session_token' => $this->sessionToken, 77 | 'message' => 'Conectado com sucesso', 78 | ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 79 | } else { 80 | return json_encode([ 81 | 'status' => 'error', 82 | 'message' => 'Falha ao obter session_token', 83 | ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 84 | } 85 | } catch (RequestException $e) { 86 | if ($e->hasResponse()) { 87 | $response = $e->getResponse(); 88 | return json_encode([ 89 | 'status' => 'error', 90 | 'message' => $response->getBody()->getContents() 91 | ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 92 | } else { 93 | return json_encode([ 94 | 'status' => 'error', 95 | 'message' => $e->getMessage(), 96 | ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 97 | } 98 | } 99 | } 100 | 101 | 102 | 103 | public function killSession() 104 | { 105 | $headers = [ 106 | 'app-token' => $this->appToken, 107 | 'Session-Token' => $this->sessionToken, 108 | ]; 109 | 110 | return $this->sendRequest('GET', '/killSession', $headers); 111 | } 112 | 113 | public function requestItem($item, $params = null) 114 | { 115 | $headers = [ 116 | 'Session-Token' => $this->sessionToken, 117 | 'app-token' => $this->appToken, 118 | 'Authorization' => 'Basic Z2xwaTouQURNX1MzcnYxYzMu' 119 | ]; 120 | 121 | return $this->sendRequest('GET', '/' . $item . '/' . ($params ?? ''), $headers); 122 | } 123 | 124 | 125 | public function addItem($item, $params = []) 126 | { 127 | $headers = [ 128 | 'Session-Token' => $this->sessionToken, 129 | 'app-token' => $this->appToken, 130 | 'Content-Type' => 'application/json', 131 | 'Authorization' => 'Basic Z2xwaTouQURNX1MzcnYxYzMu' 132 | ]; 133 | 134 | return $this->sendRequest('POST', '/' . $item, $headers, $params); 135 | } 136 | 137 | public function updateItem($item, $id, $params) 138 | { 139 | $headers = [ 140 | 'Session-Token' => $this->sessionToken, 141 | 'app-token' => $this->appToken, 142 | 'Content-Type' => 'application/json', 143 | 'Authorization' => 'Basic Z2xwaTouQURNX1MzcnYxYzMu' 144 | ]; 145 | 146 | return $this->sendRequest('PUT', '/' . $item . '/' . $id, $headers, $params); 147 | } 148 | 149 | public function deleteItem($item, $id) 150 | { 151 | $headers = [ 152 | 'Session-Token' => $this->sessionToken, 153 | 'app-token' => $this->appToken, 154 | 'Content-Type' => 'application/json', 155 | 'Authorization' => 'Basic Z2xwaTouQURNX1MzcnYxYzMu' 156 | ]; 157 | 158 | return $this->sendRequest('DELETE', '/' . $item . '/' . $id, $headers); 159 | } 160 | 161 | public function purgeItem($item, $id) 162 | { 163 | $headers = [ 164 | 'Session-Token' => $this->sessionToken, 165 | 'app-token' => $this->appToken, 166 | 'Content-Type' => 'application/json', 167 | 'Authorization' => 'Basic Z2xwaTouQURNX1MzcnYxYzMu' 168 | ]; 169 | 170 | return $this->sendRequest('DELETE', '/' . $item . '/' . $id . '?force_purge=true', $headers); 171 | } 172 | 173 | public function sendDocuments($item, $filename, $name) 174 | { 175 | if (!file_exists($filename)) { 176 | return json_encode([ 177 | 'status' => 'error', 178 | 'message' => 'Arquivo não encontrado' 179 | ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 180 | } 181 | 182 | $headers = [ 183 | 'Session-Token' => $this->sessionToken, 184 | 'app-token' => $this->appToken, 185 | ]; 186 | 187 | $multipart = [ 188 | [ 189 | 'name' => 'uploadManifest', 190 | 'contents' => json_encode([ 191 | 'input' => [ 192 | 'name' => $name, 193 | '_filename' => [$filename] 194 | ] 195 | ]) 196 | ], 197 | [ 198 | 'name' => 'filename', 199 | 'contents' => Utils::tryFopen($filename, 'r'), 200 | 'filename' => basename($filename), 201 | ], 202 | ]; 203 | 204 | return $this->sendRequest('POST', '/' . $item, $headers, ['multipart' => $multipart]); 205 | } 206 | 207 | public static function render($result) 208 | { 209 | echo "
" . $result . "