RestFullApi.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services;
  3. class RestFullApi
  4. {
  5. const HTTP_GET = 'GET';
  6. const HTTP_PUT = 'PUT';
  7. const HTTP_POST ='POST';
  8. const HTTP_DELETE ='DELETE';
  9. protected $host;
  10. protected $headers = [];
  11. protected $response;
  12. protected $parsedResponse;
  13. protected $requestUrl;
  14. protected $actionPath;
  15. protected $method;
  16. protected $request;
  17. private $curl;
  18. private $logger;
  19. protected $curlInfo;
  20. protected $apiUri;
  21. /**
  22. * RestFullApi constructor.
  23. * @param $host
  24. * @param $port
  25. * @param $protocol
  26. */
  27. public function __construct($host)
  28. {
  29. $this->host = $host;
  30. }
  31. public function useJson(){
  32. $this->headers[]= "Content-Type: application/json";
  33. $this->headers[]= "Accept: application/json";
  34. }
  35. private function getRequestUrl(){
  36. return $this->host.$this->apiUri;
  37. }
  38. public function post($path, $request=[]){
  39. $this->method = self::HTTP_POST;
  40. $this->actionPath = $path;
  41. $this->request = $request;
  42. return $this->sendRequest();
  43. }
  44. public function get($path, $request=[]){
  45. $this->method = self::HTTP_GET;
  46. $this->actionPath = $path;
  47. $this->request = $request;
  48. return $this->sendRequest();
  49. }
  50. public function put($path, $request=[]){
  51. $this->method = self::HTTP_PUT;
  52. $this->actionPath = $path;
  53. $this->request = $request;
  54. return $this->sendRequest();
  55. }
  56. public function delete($path){
  57. $this->method = self::HTTP_DELETE;
  58. $this->actionPath = $path;
  59. $this->request = null;
  60. return $this->sendRequest();
  61. }
  62. private function sendRequest(){
  63. $this->requestUrl = $this->getRequestUrl().$this->actionPath;
  64. $this->curl = curl_init();
  65. switch ($this->method)
  66. {
  67. case self::HTTP_GET:
  68. $this->requestUrl .="?". http_build_query($this->request);
  69. break;
  70. case self::HTTP_POST:
  71. if(is_array($this->request)){
  72. $request = json_encode($this->request);
  73. }else{
  74. $request = $this->request;
  75. }
  76. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST , 'POST');
  77. curl_setopt($this->curl, CURLOPT_POST, true);
  78. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request);
  79. break;
  80. case self::HTTP_PUT:
  81. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, self::HTTP_PUT);
  82. if(is_array($this->request)){
  83. $request = json_encode($this->request);
  84. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request);
  85. }
  86. break;
  87. case self::HTTP_DELETE:
  88. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, self::HTTP_DELETE);
  89. break;
  90. }
  91. curl_setopt($this->curl, CURLOPT_URL, $this->requestUrl);
  92. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  93. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  94. curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
  95. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  96. return $this->parseResponse()->processResponse();
  97. }
  98. public function setLogger($logger)
  99. {
  100. $this->logger = $logger;
  101. }
  102. private function debug(){
  103. if (!$this->logger || !method_exists($this->logger, 'log'))
  104. {
  105. return;
  106. }
  107. $action = sprintf("GET: %s\r\nPOST: %s\r\n", $this->requestUrl, print_r($this->request, true));
  108. if($this->method == self::HTTP_GET){
  109. $action = sprintf("GET: %s\r\nPOST: %s\r\n", $this->requestUrl, null);
  110. }
  111. $response = sprintf("HTTP %s %s", $this->curlInfo['http_code'], print_r($this->parsedResponse, true));
  112. $this->logger->log($this->method, $action, $this->response, $response);
  113. }
  114. private function parseResponse(){
  115. $this->response = curl_exec($this->curl);
  116. $this->curlInfo = curl_getinfo($this->curl);
  117. $this->parsedResponse = json_decode($this->response , true);
  118. $this->debug();
  119. if (!$this->response)
  120. {
  121. $error = curl_error($this->curl);
  122. curl_close($this->curl);
  123. if($error ){
  124. throw new \Exception('cURL Error: ' . $error);
  125. }
  126. }
  127. curl_close($this->curl);
  128. return $this;
  129. }
  130. private function processResponse(){
  131. if($this->parsedResponse['errors']){
  132. $errors =[];
  133. foreach ($this->parsedResponse['errors'] as $error){
  134. if(is_array($error)){
  135. foreach ($error as $e){
  136. $errors[]= $e;
  137. }
  138. }elseif (is_string($error)){
  139. $errors[]= $error;
  140. }
  141. }
  142. $message = implode(" ",$errors);
  143. throw new \Exception( $message, $this->curlInfo['http_code']);
  144. }
  145. if($this->curlInfo['http_code'] >= 400 ){
  146. throw new \Exception(sprintf('Loging to host: %s failed ', $this->host));
  147. }
  148. return $this->parsedResponse;
  149. }
  150. }