/libs/sendgrid-php/vendor/sendgrid/php-http-client/lib/Client.php

https://github.com/sendtogeo/Seo-Panel · PHP · 218 lines · 104 code · 24 blank · 90 comment · 6 complexity · 0c1b887befd3195f738220dc010d6869 MD5 · raw file

  1. <?php
  2. /**
  3. * HTTP Client library
  4. *
  5. * PHP version 5.4
  6. *
  7. * @author Matt Bernier <dx@sendgrid.com>
  8. * @author Elmer Thomas <dx@sendgrid.com>
  9. * @copyright 2016 SendGrid
  10. * @license https://opensource.org/licenses/MIT The MIT License
  11. * @version GIT: <git_id>
  12. * @link http://packagist.org/packages/sendgrid/php-http-client
  13. */
  14. namespace SendGrid;
  15. /**
  16. * Quickly and easily access any REST or REST-like API.
  17. */
  18. class Client
  19. {
  20. /** @var string */
  21. protected $host;
  22. /** @var array */
  23. protected $headers;
  24. /** @var string */
  25. protected $version;
  26. /** @var array */
  27. protected $path;
  28. /** @var array */
  29. protected $curlOptions;
  30. /** @var array */
  31. private $methods;
  32. /**
  33. * Initialize the client
  34. *
  35. * @param string $host the base url (e.g. https://api.sendgrid.com)
  36. * @param array $headers global request headers
  37. * @param string $version api version (configurable)
  38. * @param array $path holds the segments of the url path
  39. * @param array $curlOptions extra options to set during curl initialization
  40. */
  41. public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null)
  42. {
  43. $this->host = $host;
  44. $this->headers = $headers ?: [];
  45. $this->version = $version;
  46. $this->path = $path ?: [];
  47. $this->curlOptions = $curlOptions ?: [];
  48. // These are the supported HTTP verbs
  49. $this->methods = ['delete', 'get', 'patch', 'post', 'put'];
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function getHost()
  55. {
  56. return $this->host;
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getHeaders()
  62. {
  63. return $this->headers;
  64. }
  65. /**
  66. * @return string|null
  67. */
  68. public function getVersion()
  69. {
  70. return $this->version;
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getPath()
  76. {
  77. return $this->path;
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function getCurlOptions()
  83. {
  84. return $this->curlOptions;
  85. }
  86. /**
  87. * Make a new Client object
  88. *
  89. * @param string $name name of the url segment
  90. *
  91. * @return Client object
  92. */
  93. private function buildClient($name = null)
  94. {
  95. if (isset($name)) {
  96. $this->path[] = $name;
  97. }
  98. $client = new Client($this->host, $this->headers, $this->version, $this->path);
  99. $this->path = [];
  100. return $client;
  101. }
  102. /**
  103. * Build the final URL to be passed
  104. *
  105. * @param array $queryParams an array of all the query parameters
  106. *
  107. * @return string
  108. */
  109. private function buildUrl($queryParams = null)
  110. {
  111. $path = '/' . implode('/', $this->path);
  112. if (isset($queryParams)) {
  113. $path .= '?' . http_build_query($queryParams);
  114. }
  115. return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);
  116. }
  117. /**
  118. * Make the API call and return the response. This is separated into
  119. * it's own function, so we can mock it easily for testing.
  120. *
  121. * @param string $method the HTTP verb
  122. * @param string $url the final url to call
  123. * @param array $body request body
  124. * @param array $headers any additional request headers
  125. *
  126. * @return Response object
  127. */
  128. public function makeRequest($method, $url, $body = null, $headers = null)
  129. {
  130. $curl = curl_init($url);
  131. curl_setopt_array($curl, [
  132. CURLOPT_RETURNTRANSFER => true,
  133. CURLOPT_HEADER => 1,
  134. CURLOPT_CUSTOMREQUEST => strtoupper($method),
  135. CURLOPT_SSL_VERIFYPEER => false,
  136. ] + $this->curlOptions);
  137. if (isset($headers)) {
  138. $this->headers = array_merge($this->headers, $headers);
  139. }
  140. if (isset($body)) {
  141. $encodedBody = json_encode($body);
  142. curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedBody);
  143. $this->headers = array_merge($this->headers, ['Content-Type: application/json']);
  144. }
  145. curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
  146. $response = curl_exec($curl);
  147. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  148. $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  149. $responseBody = substr($response, $headerSize);
  150. $responseHeaders = substr($response, 0, $headerSize);
  151. $responseHeaders = explode("\n", $responseHeaders);
  152. curl_close($curl);
  153. return new Response($statusCode, $responseBody, $responseHeaders);
  154. }
  155. /**
  156. * Add variable values to the url.
  157. * (e.g. /your/api/{variable_value}/call)
  158. * Another example: if you have a PHP reserved word, such as and,
  159. * in your url, you must use this method.
  160. *
  161. * @param string $name name of the url segment
  162. *
  163. * @return Client object
  164. */
  165. public function _($name = null)
  166. {
  167. return $this->buildClient($name);
  168. }
  169. /**
  170. * Dynamically add method calls to the url, then call a method.
  171. * (e.g. client.name.name.method())
  172. *
  173. * @param string $name name of the dynamic method call or HTTP verb
  174. * @param array $args parameters passed with the method call
  175. *
  176. * @return Client or Response object
  177. */
  178. public function __call($name, $args)
  179. {
  180. $name = strtolower($name);
  181. if ($name === 'version') {
  182. $this->version = $args[0];
  183. return $this->_();
  184. }
  185. if (in_array($name, $this->methods, true)) {
  186. $body = isset($args[0]) ? $args[0] : null;
  187. $queryParams = isset($args[1]) ? $args[1] : null;
  188. $url = $this->buildUrl($queryParams);
  189. $headers = isset($args[2]) ? $args[2] : null;
  190. return $this->makeRequest($name, $url, $body, $headers);
  191. }
  192. return $this->_($name);
  193. }
  194. }