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