|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * HTTP Client library |
| 4 | + * |
| 5 | + * PHP version 5.2 |
| 6 | + * |
| 7 | + * @author Matt Bernier <[email protected]> |
| 8 | + * @author Elmer Thomas <[email protected]> |
| 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 | +/** |
| 17 | + * Holds the response from an API call. |
| 18 | + */ |
| 19 | +class Response |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Setup the response data |
| 23 | + * |
| 24 | + * @param int $status_code the status code. |
| 25 | + * @param array $response_body the response body as an array. |
| 26 | + * @param array $response_headers an array of response headers. |
| 27 | + */ |
| 28 | + function __construct($status_code = null, $response_body = null, $response_headers = null) |
| 29 | + { |
| 30 | + $this->_status_code = $status_code; |
| 31 | + $this->_response_body = $response_body; |
| 32 | + $this->_response_headers = $response_headers; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * The status code |
| 37 | + * |
| 38 | + * @return integer |
| 39 | + */ |
| 40 | + public function statusCode() |
| 41 | + { |
| 42 | + return $this->_status_code; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * The response body |
| 47 | + * |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + public function responseBody() |
| 51 | + { |
| 52 | + return $this->_response_body; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * The response headers |
| 57 | + * |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + public function responseHeaders() |
| 61 | + { |
| 62 | + return $this->_response_headers; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Quickly and easily access any REST or REST-like API. |
| 68 | + */ |
| 69 | +class Client |
| 70 | +{ |
| 71 | + |
| 72 | + public |
| 73 | + $host, |
| 74 | + $request_headers, |
| 75 | + $version, |
| 76 | + $url_path, |
| 77 | + $methods; |
| 78 | + |
| 79 | + /** |
| 80 | + * Initialize the client |
| 81 | + * |
| 82 | + * @param string $host the base url (e.g. https://api.sendgrid.com) |
| 83 | + * @param array $request_headers global request headers |
| 84 | + * @param string $version api version (configurable) |
| 85 | + * @param array $url_path holds the segments of the url path |
| 86 | + */ |
| 87 | + function __construct($host, $request_headers = null, $version = null, $url_path = null) |
| 88 | + { |
| 89 | + $this->host = $host; |
| 90 | + $this->request_headers = ($request_headers ? $request_headers : []); |
| 91 | + $this->version = $version; |
| 92 | + $this->url_path = ($url_path ? $url_path : []); |
| 93 | + // These are the supported HTTP verbs |
| 94 | + $this->methods = ['delete', 'get', 'patch', 'post', 'put']; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Make a new Client object |
| 99 | + * |
| 100 | + * @param string $name name of the url segment |
| 101 | + * |
| 102 | + * @return Client object |
| 103 | + */ |
| 104 | + private function _buildClient($name = null) |
| 105 | + { |
| 106 | + if(isset($name)) { |
| 107 | + array_push($this->url_path, $name); |
| 108 | + } |
| 109 | + $url_path = $this->url_path; |
| 110 | + $this->url_path = []; |
| 111 | + return new Client($this->host, $this->request_headers, $this->version, $url_path); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Subclass this function for your own needs. |
| 116 | + * Or just pass the version as part of the URL |
| 117 | + * (e.g. client._('/v3')) |
| 118 | + * |
| 119 | + * @param string $url URI portion of the full URL being requested |
| 120 | + * |
| 121 | + * @return string |
| 122 | + */ |
| 123 | + private function _buildVersionedUrl($url) |
| 124 | + { |
| 125 | + return sprintf("%s%s%s", $this->host, $this->version, $url); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Build the final URL to be passed |
| 130 | + * |
| 131 | + * @param array $query_params an array of all the query parameters |
| 132 | + * |
| 133 | + * @return string |
| 134 | + */ |
| 135 | + private function _buildUrl($query_params = null) |
| 136 | + { |
| 137 | + $url = '/'.implode('/', $this->url_path); |
| 138 | + if (isset($query_params)) { |
| 139 | + $url_values = http_build_query($query_params); |
| 140 | + $url = sprintf('%s?%s', $url, $url_values); |
| 141 | + } |
| 142 | + if (isset($this->version)) { |
| 143 | + $url = $this->_buildVersionedUrl($url); |
| 144 | + } else { |
| 145 | + $url = sprintf('%s%s', $this->host, $url);; |
| 146 | + } |
| 147 | + return $url; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Make the API call and return the response. This is separated into |
| 152 | + * it's own function, so we can mock it easily for testing. |
| 153 | + * |
| 154 | + * @param array $method the HTTP verb |
| 155 | + * @param string $url the final url to call |
| 156 | + * @param array $request_body request body |
| 157 | + * @param array $request_headers any additional request headers |
| 158 | + * |
| 159 | + * @return Response object |
| 160 | + */ |
| 161 | + public function makeRequest($method, $url, $request_body = null, $request_headers = null) |
| 162 | + { |
| 163 | + $curl = curl_init($url); |
| 164 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 165 | + curl_setopt($curl, CURLOPT_HEADER, 1); |
| 166 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
| 167 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
| 168 | + if(isset($request_body)) { |
| 169 | + $request_body = json_encode($request_body); |
| 170 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); |
| 171 | + $content_length = array('Content-Length: ' . strlen($request_body)); |
| 172 | + } |
| 173 | + if(isset($request_headers)) { |
| 174 | + $this->request_headers = array_merge($this->request_headers, $request_headers); |
| 175 | + } |
| 176 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->request_headers); |
| 177 | + $curl_response = curl_exec($curl); |
| 178 | + $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
| 179 | + $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 180 | + $response_body = substr($curl_response, $header_size); |
| 181 | + $response_header = substr($curl_response, 0, $header_size); |
| 182 | + |
| 183 | + curl_close($curl); |
| 184 | + |
| 185 | + return new Response($status_code, $response_body, $response_header); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Add variable values to the url. |
| 190 | + * (e.g. /your/api/{variable_value}/call) |
| 191 | + * Another example: if you have a PHP reserved word, such as and, |
| 192 | + * in your url, you must use this method. |
| 193 | + * |
| 194 | + * @param string $name name of the url segment |
| 195 | + * |
| 196 | + * @return Client object |
| 197 | + */ |
| 198 | + public function _($name = null) |
| 199 | + { |
| 200 | + return $this->_buildClient($name); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Dynamically add method calls to the url, then call a method. |
| 205 | + * (e.g. client.name.name.method()) |
| 206 | + * |
| 207 | + * @param string $name name of the dynamic method call or HTTP verb |
| 208 | + * @param array $args parameters passed with the method call |
| 209 | + * |
| 210 | + * @return Client or Response object |
| 211 | + */ |
| 212 | + public function __call($name, $args) |
| 213 | + { |
| 214 | + if($name == 'version') { |
| 215 | + $this->version = $args[0]; |
| 216 | + return $this->_(); |
| 217 | + } |
| 218 | + |
| 219 | + if (in_array($name, $this->methods)) { |
| 220 | + $query_params = ((count($args) >= 2) ? $args[1] : null); |
| 221 | + $url = $this->_buildUrl($query_params); |
| 222 | + $request_body = ($args ? $args[0] : null); |
| 223 | + $request_headers = ((count($args) == 3) ? $args[2] : null); |
| 224 | + return $this->makeRequest($name, $url, $request_body, $request_headers); |
| 225 | + } |
| 226 | + |
| 227 | + return $this->_($name); |
| 228 | + } |
| 229 | +} |
| 230 | +?> |
0 commit comments