Request.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Api\AbstractApi\Curl;
  3. use ModulesGarden\ProxmoxAddon\Core\Api\AbstractApi\Curl;
  4. use ModulesGarden\ProxmoxAddon\Core\Api\AbstractApi\Curl\Response;
  5. /**
  6. * Description of Request
  7. *
  8. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  9. */
  10. class Request extends Curl
  11. {
  12. protected $url = '';
  13. protected $lastResponse = [];
  14. protected $headers = [
  15. "Content-Type: application/x-www-form-urlencoded"
  16. ];
  17. public function getLastResponse()
  18. {
  19. return $this->lastResponse;
  20. }
  21. public function setUrl($url = "")
  22. {
  23. $this->url = $url;
  24. return $this;
  25. }
  26. public function resetHeaders()
  27. {
  28. $this->headers = [];
  29. return $this;
  30. }
  31. public function setHeaders(array $headers = [])
  32. {
  33. $this->headers = $headers;
  34. return $this;
  35. }
  36. public function addHeaders($headers)
  37. {
  38. if (is_array($headers))
  39. {
  40. $this->headers = $headers;
  41. }
  42. else
  43. {
  44. $this->headers[] = $headers;
  45. }
  46. return $this;
  47. }
  48. protected function send()
  49. {
  50. $return = parent::send();
  51. $this->close();
  52. $this->lastResponse[] = $return;
  53. return $return;
  54. }
  55. /**
  56. * @param array $data
  57. * @return string
  58. */
  59. protected function httpBuildQuery(array $data = [])
  60. {
  61. return empty($data) ? "" : http_build_query($data);
  62. }
  63. /**
  64. * @param array $data post field
  65. * @return Response
  66. */
  67. public function post($data = [])
  68. {
  69. $postvars = is_array($data) ? $this->httpBuildQuery($data) : $data;
  70. $this->open()
  71. ->setOptions(CURLOPT_SSL_VERIFYPEER, false)
  72. ->setOptions(CURLOPT_URL, $this->url)
  73. ->setOptions(CURLOPT_POSTFIELDS, $postvars)
  74. ->setOptions(CURLOPT_POST, true);
  75. if (!empty($this->headers))
  76. {
  77. $this->setOptions(CURLOPT_HTTPHEADER, $this->headers)
  78. ->setOptions(CURLOPT_HEADER, true);
  79. }
  80. return $this->send();
  81. }
  82. /**
  83. * @param array $data post field
  84. * @return Response
  85. */
  86. public function put($data = [])
  87. {
  88. $postvars = is_array($data) ? $this->httpBuildQuery($data) : $data;
  89. $this->open()
  90. ->setOptions(CURLOPT_SSL_VERIFYPEER, false)
  91. ->setOptions(CURLOPT_URL, $this->url)
  92. ->setOptions(CURLOPT_POSTFIELDS, $postvars)
  93. ->setOptions(CURLOPT_CUSTOMREQUEST, "PUT");
  94. if (!empty($this->headers))
  95. {
  96. $this->setOptions(CURLOPT_HTTPHEADER, $this->headers)
  97. ->setOptions(CURLOPT_HEADER, true);
  98. }
  99. return $this->send();
  100. }
  101. /**
  102. * @param array $data post field
  103. * @return Response
  104. */
  105. public function delete($data = [])
  106. {
  107. $deletevars = is_array($data) ? $this->httpBuildQuery($data) : $data;
  108. $this->open()
  109. ->setOptions(CURLOPT_SSL_VERIFYPEER, false)
  110. ->setOptions(CURLOPT_URL, $this->url . $deletevars)
  111. ->setOptions(CURLOPT_CUSTOMREQUEST, "DELETE");
  112. if (!empty($this->headers))
  113. {
  114. $this->setOptions(CURLOPT_HTTPHEADER, $this->headers)
  115. ->setOptions(CURLOPT_HEADER, true);
  116. }
  117. return $this->send();
  118. }
  119. /**
  120. * @param array $data post field
  121. * @return Response
  122. */
  123. public function get($data = [])
  124. {
  125. $getvars = is_array($data) ? $this->httpBuildQuery($data) : $data;
  126. $this->open()
  127. ->setOptions(CURLOPT_URL, $this->url . $getvars);
  128. if (!empty($this->headers))
  129. {
  130. $this->setOptions(CURLOPT_HTTPHEADER, $this->headers)
  131. ->setOptions(CURLOPT_HEADER, true);
  132. }
  133. return $this->send();
  134. }
  135. /**
  136. * @param array $data post field
  137. * @return Response
  138. */
  139. public function options($data = [])
  140. {
  141. $deletevars = is_array($data) ? $this->httpBuildQuery($data) : $data;
  142. $this->open()
  143. ->setOptions(CURLOPT_SSL_VERIFYPEER, false)
  144. ->setOptions(CURLOPT_URL, $this->url)
  145. ->setOptions(CURLOPT_CUSTOMREQUEST, "OPTIONS");
  146. if (!empty($this->headers))
  147. {
  148. $this->setOptions(CURLOPT_HTTPHEADER, $this->headers)
  149. ->setOptions(CURLOPT_HEADER, true);
  150. }
  151. return $this->send();
  152. }
  153. }