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