Request.php 4.4 KB

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