Curl.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\Core\Api\AbstractApi;
  3. use \ModulesGarden\Servers\ZimbraEmail\Core\Api\AbstractApi\Curl\Response;
  4. use \ModulesGarden\Servers\ZimbraEmail\Core\DependencyInjection;
  5. use \ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\Exceptions\Exception;
  6. use \ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  7. /**
  8. * Description of Curl
  9. *
  10. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  11. */
  12. abstract class Curl
  13. {
  14. private $curl;
  15. private $options = [
  16. CURLOPT_TIMEOUT => 30,
  17. CURLOPT_HEADER => false,
  18. CURLOPT_RETURNTRANSFER => true,
  19. CURLINFO_HEADER_OUT => true
  20. ];
  21. protected $curlParser;
  22. public function setCurlParser($curlParser)
  23. {
  24. $this->curlParser = $curlParser;
  25. return $this;
  26. }
  27. public function setOptions($options, $value)
  28. {
  29. $this->options[$options] = $value;
  30. return $this;
  31. }
  32. protected function open()
  33. {
  34. $this->curl = curl_init();
  35. return $this;
  36. }
  37. protected function close()
  38. {
  39. curl_close($this->curl);
  40. return $this;
  41. }
  42. protected function unsetOptions($options)
  43. {
  44. if (is_array($options))
  45. {
  46. foreach ($options as $option)
  47. {
  48. if (isset($this->options[$option]))
  49. {
  50. unset($this->options[$option]);
  51. }
  52. }
  53. }
  54. else
  55. {
  56. unset($this->options[$options]);
  57. }
  58. return $this;
  59. }
  60. /**
  61. * @return Response
  62. * @throws \ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\Exceptions\Exception
  63. */
  64. protected function send()
  65. {
  66. $this->includeOptions();
  67. if (($head = $this->execute()) === false)
  68. {
  69. throw new Exception(ErrorCodesLib::CORE_CURL_000001, ['lastCurlError' => $this->getLastErrorWithCurl()]);
  70. }
  71. if ($errno = $this->getLastErrorNumber())
  72. {
  73. throw new Exception(ErrorCodesLib::CORE_CURL_000002, ['curlError' => $this->getLastError($errno)]);
  74. }
  75. list($header, $body) = $this->curlParser->rebuild($head, $this->getHeaderSize());
  76. return DependencyInjection::create(Response::class)
  77. ->setRequest($this->getHeaderOut())
  78. ->setHeader($header)
  79. ->setCode($this->getHttpCode())
  80. ->setBody($body);
  81. }
  82. private function execute()
  83. {
  84. return curl_exec($this->curl);
  85. }
  86. private function getLastErrorNumber()
  87. {
  88. return curl_errno($this->curl);
  89. }
  90. /**
  91. * (PHP 5 &gt;= 5.5.0, PHP 7)<br/>
  92. * Return string describing the given error code
  93. * @link http://php.net/manual/en/function.curl-strerror.php
  94. * @param int $errornum <p>
  95. * One of the cURL error codes constants.
  96. * </p>
  97. * @return string error description or <b>NULL</b> for invalid error code.
  98. */
  99. private function getLastError($errmo)
  100. {
  101. return curl_strerror($errmo);
  102. }
  103. /**
  104. * (PHP 4 &gt;= 4.0.3, PHP 5, PHP 7)<br/>
  105. * Return a string containing the last error for the current session
  106. * @link http://php.net/manual/en/function.curl-error.php
  107. * @param resource $ch
  108. * @return string the error message or '' (the empty string) if no
  109. * error occurred.
  110. */
  111. private function getLastErrorWithCurl()
  112. {
  113. return curl_error($this->curl);
  114. }
  115. private function getHeaderSize()
  116. {
  117. return curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
  118. }
  119. private function getHeaderOut()
  120. {
  121. return curl_getinfo($this->curl, CURLINFO_HEADER_OUT);
  122. }
  123. private function getHttpCode()
  124. {
  125. return curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
  126. }
  127. private function includeOptions()
  128. {
  129. curl_setopt_array($this->curl, $this->options);
  130. return $this;
  131. }
  132. }