Glpi.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Permet la communication avec l'API REST de glpi
  4. * @author Valentin CARRUESCO
  5. * @category Rest/External
  6. * @license MIT
  7. */
  8. class GLPI{
  9. public $url,$token = '';
  10. function __construct($url){
  11. $this->url = $url;
  12. }
  13. //Connexion login/mdp et récuperaiton du token
  14. public function connect($login,$password){
  15. $response = $this->rest('/initSession/',$login,$password);
  16. $this->token = isset($response['session_token']) && !is_null($response['session_token']) ? $response['session_token'] : '';
  17. }
  18. //Récuperation d'un utilisateur GLPI par son login
  19. public function userByLogin($login){
  20. $criteria = 'criteria[0][field]=2&criteria[0][searchtype]=2&criteria[0][value]=&criteria[1][link]=AND&criteria[1][field]=1&criteria[1][searchtype]=2&criteria[1][value]='.$login;
  21. $response = $this->rest('/search/User/?'.$criteria,$this->token);
  22. $user = array();
  23. if(isset($response['data'][0])){
  24. $user = array(
  25. 'id' => $response['data'][0][2],
  26. 'name' => $response['data'][0][34],
  27. 'firstname' => $response['data'][0][9],
  28. 'mail' => $response['data'][0][5],
  29. 'phone' => $response['data'][0][6],
  30. 'mobile' => $response['data'][0][11]
  31. );
  32. }
  33. return $user;
  34. }
  35. //Récuperation d'un ticket glpi par son id
  36. public function ticketById($id){
  37. $response = $this->rest('/Ticket/'.$id.'?expand_dropdowns=true',$this->token);
  38. return $response;
  39. }
  40. //Récuperation des tickets par criteres
  41. public function searchTickets($criteria){
  42. $response = $this->rest('/search/Ticket/?'.$criteria,$this->token);
  43. $tickets = array();
  44. if(isset($response['data'])){
  45. foreach ($response['data'] as $ticket) {
  46. $tickets[] = array(
  47. 'id' => $ticket[2],
  48. 'url' => $this->url.'/front/ticket.form.php?id='.$ticket[2],
  49. 'subject' => $ticket[1],
  50. 'date' => $ticket[15],
  51. 'user' => $ticket[64],
  52. 'firm' => $ticket[80],
  53. );
  54. }
  55. }
  56. return $tickets;
  57. }
  58. //Décrit les champs d'un item de glpi
  59. public function searchOptions($itemType = 'Ticket'){
  60. return $this->rest('/listSearchOptions/'.$itemType,$this->token);
  61. }
  62. //Récuperation des tickets assigné à un utilisateur
  63. public function userTickets($id,$criteria = null){
  64. $criteria = isset($criteria) ? $criteria: 'criteria[0][field]=5&criteria[0][searchtype]=equals&criteria[0][value]='.$id.'&criteria[1][link]=AND&criteria[1][field]=15&criteria[1][searchtype]=equals&criteria[1][value]=TODAY';
  65. $response = $this->rest('/search/Ticket/?'.$criteria,$this->token);
  66. $tickets = array();
  67. if(isset($response['data'])){
  68. foreach ($response['data'] as $ticket) {
  69. $tickets[] = array(
  70. 'id' => $ticket[2],
  71. 'url' => 'https://glpi.kiss.fr/front/ticket.form.php?id='.$ticket[2],
  72. 'subject' => $ticket[1],
  73. 'date' => $ticket[15],
  74. 'firm' => $ticket[80],
  75. );
  76. }
  77. }
  78. return $tickets;
  79. }
  80. //Requete rest
  81. public function rest($action,$login,$password=null,$headers=array()){
  82. $url = $this->url.'/apirest.php'.$action;
  83. $ch = curl_init();
  84. curl_setopt($ch, CURLOPT_URL, $url);
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  86. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  87. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  88. if($password!=null) curl_setopt($ch, CURLOPT_USERPWD, $login . ":" . $password);
  89. $headers = array();
  90. if($password==null) $headers[] = 'Session-Token: '.$login;
  91. $headers[] = 'Content-Type: application/json';
  92. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  93. $response = curl_exec($ch);
  94. curl_close($ch);
  95. return json_decode($response,true);
  96. }
  97. }
  98. ?>