1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,228 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+class Phaxio |
|
4 |
+{ |
|
5 |
+ private $debug = false; |
|
6 |
+ private $api_key = null; |
|
7 |
+ private $api_secret = null; |
|
8 |
+ private $host = "https://api.phaxio.com/v2.1/"; |
|
9 |
+ |
|
10 |
+ public function __construct($api_key = null, $api_secret = null, $host = null) |
|
11 |
+ { |
|
12 |
+ $this->api_key = $api_key ? $api_key : $this->getApiKey(); |
|
13 |
+ $this->api_secret = $api_secret ? $api_secret : $this->getApiSecret(); |
|
14 |
+ if ($host != null) { |
|
15 |
+ $this->host = $host; |
|
16 |
+ } |
|
17 |
+ } |
|
18 |
+ |
|
19 |
+ public function faxes() |
|
20 |
+ { |
|
21 |
+ return new Phaxio\Faxes($this); |
|
22 |
+ } |
|
23 |
+ |
|
24 |
+ public function phoneNumbers() |
|
25 |
+ { |
|
26 |
+ return new Phaxio\PhoneNumbers($this); |
|
27 |
+ } |
|
28 |
+ |
|
29 |
+ public function phaxCodes() |
|
30 |
+ { |
|
31 |
+ return new Phaxio\PhaxCodes($this); |
|
32 |
+ } |
|
33 |
+ |
|
34 |
+ public function public() |
|
35 |
+ { |
|
36 |
+ return new Phaxio\PhaxioPublic($this); |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ public function account() { |
|
40 |
+ return new Phaxio\Account($this); |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ # Convenience methods |
|
44 |
+ |
|
45 |
+ public function sendFax($params) { |
|
46 |
+ return $this->faxes()->create($params); |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+ public function initFax($id) { |
|
50 |
+ return $this->faxes()->init($id); |
|
51 |
+ } |
|
52 |
+ |
|
53 |
+ public function retriveFaxFile($id, $params = array()) { |
|
54 |
+ return $this->initFax($id)->getFile()->retrieve($params); |
|
55 |
+ } |
|
56 |
+ |
|
57 |
+ public function listFaxes($params = array()) { |
|
58 |
+ return $this->faxes()->getList($params); |
|
59 |
+ } |
|
60 |
+ |
|
61 |
+ public function retrieveDefaultPhaxCode($getMetadata = false) |
|
62 |
+ { |
|
63 |
+ return Phaxio\PhaxCode::init($this)->retrieve($getMetadata); |
|
64 |
+ } |
|
65 |
+ |
|
66 |
+ public function stringUpload($str, $ext = 'txt') { |
|
67 |
+ return new Phaxio\StringUpload($str, $ext); |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ # API client methods |
|
71 |
+ |
|
72 |
+ public function getApiKey() |
|
73 |
+ { |
|
74 |
+ return $this->api_key; |
|
75 |
+ } |
|
76 |
+ |
|
77 |
+ public function getApiSecret() |
|
78 |
+ { |
|
79 |
+ return $this->api_secret; |
|
80 |
+ } |
|
81 |
+ |
|
82 |
+ public function doRequest($method, $path, $params = array(), $wrapInPhaxioOperationResult = true) |
|
83 |
+ { |
|
84 |
+ $address = $this->host . $path; |
|
85 |
+ |
|
86 |
+ $response = $this->curlRequest($method, $address, $params); |
|
87 |
+ |
|
88 |
+ if ($this->debug) { |
|
89 |
+ echo "Response: \n\n"; |
|
90 |
+ var_dump($response); |
|
91 |
+ echo "\n\n"; |
|
92 |
+ } |
|
93 |
+ |
|
94 |
+ if ($wrapInPhaxioOperationResult || $response['status'] != 200) { |
|
95 |
+ $result = json_decode($response['body'], true); |
|
96 |
+ } |
|
97 |
+ |
|
98 |
+ switch ($response['status']) { |
|
99 |
+ case 401: |
|
100 |
+ throw new Phaxio\Error\AuthenticationException($result['message']); |
|
101 |
+ break; |
|
102 |
+ case 404: |
|
103 |
+ throw new Phaxio\Error\NotFoundException($result['message']); |
|
104 |
+ break; |
|
105 |
+ case 422: |
|
106 |
+ throw new Phaxio\Error\InvalidRequestException($result['message']); |
|
107 |
+ break; |
|
108 |
+ case 429: |
|
109 |
+ throw new Phaxio\Error\RateLimitException($result['message']); |
|
110 |
+ break; |
|
111 |
+ } |
|
112 |
+ |
|
113 |
+ if ($response['status'] >= 500 || (isset($result['success']) && $result['success'] != true)) { |
|
114 |
+ throw new Phaxio\Error\GeneralException($result['message']); |
|
115 |
+ } |
|
116 |
+ |
|
117 |
+ if ($wrapInPhaxioOperationResult) { |
|
118 |
+ $opResult = new Phaxio\OperationResult($result['success'], $result['message'], isset($result['data']) ? $result['data'] : null, isset($result['paging']) ? $result['paging'] : null); |
|
119 |
+ } else { |
|
120 |
+ $opResult = $response; |
|
121 |
+ } |
|
122 |
+ |
|
123 |
+ return $opResult; |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ private function curlRequest($method, $address, $params = array()) |
|
127 |
+ { |
|
128 |
+ $handle = curl_init($address); |
|
129 |
+ |
|
130 |
+ curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method); |
|
131 |
+ curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
|
132 |
+ |
|
133 |
+ # Authentication |
|
134 |
+ curl_setopt($handle, CURLOPT_USERPWD, $this->getApiKey() . ':' . $this->getApiSecret()); |
|
135 |
+ |
|
136 |
+ if ($this->debug) { |
|
137 |
+ echo "Requested resource: $method $address\n\n"; |
|
138 |
+ echo "Authentication: " . $this->getApiKey() . ':' . $this->getApiSecret() . "\n\n"; |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ $this->curlSetoptCustomPostfields($handle, $params); |
|
142 |
+ $result = curl_exec($handle); |
|
143 |
+ |
|
144 |
+ if ($result === false) { |
|
145 |
+ throw new Phaxio\Error\APIConnectionException('Curl error: ' . curl_error($handle)); |
|
146 |
+ } |
|
147 |
+ |
|
148 |
+ $contentType = curl_getinfo($handle, CURLINFO_CONTENT_TYPE); |
|
149 |
+ $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
150 |
+ |
|
151 |
+ curl_close($handle); |
|
152 |
+ |
|
153 |
+ return array('status' => $status, 'contentType' => $contentType, 'body' => $result); |
|
154 |
+ } |
|
155 |
+ |
|
156 |
+ private function curlSetoptCustomPostfields($ch, $postfields, $headers = null) |
|
157 |
+ { |
|
158 |
+ $algos = hash_algos(); |
|
159 |
+ $hashAlgo = null; |
|
160 |
+ |
|
161 |
+ foreach (array('sha1', 'md5') as $preferred) { |
|
162 |
+ if (in_array($preferred, $algos)) { |
|
163 |
+ $hashAlgo = $preferred; |
|
164 |
+ break; |
|
165 |
+ } |
|
166 |
+ } |
|
167 |
+ if ($hashAlgo === null) { |
|
168 |
+ list($hashAlgo) = $algos; |
|
169 |
+ } |
|
170 |
+ $boundary = |
|
171 |
+ '----------------------------' . |
|
172 |
+ substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12); |
|
173 |
+ |
|
174 |
+ $body = array(); |
|
175 |
+ $crlf = "\r\n"; |
|
176 |
+ $fields = array(); |
|
177 |
+ foreach ($postfields as $key => $value) { |
|
178 |
+ if (is_array($value)) { |
|
179 |
+ foreach ($value as $idx => $v) { |
|
180 |
+ $fields[] = array($key . "[" . (is_int($idx) ? '' : $idx) . "]", $v); |
|
181 |
+ } |
|
182 |
+ } else { |
|
183 |
+ $fields[] = array($key, $value); |
|
184 |
+ } |
|
185 |
+ } |
|
186 |
+ foreach ($fields as $field) { |
|
187 |
+ list($key, $value) = $field; |
|
188 |
+ |
|
189 |
+ if (is_resource($value)) { |
|
190 |
+ $value = $this->stringUpload(stream_get_contents($value), basename(stream_get_meta_data($value)['uri'])); |
|
191 |
+ } |
|
192 |
+ |
|
193 |
+ if ($value instanceof Phaxio\StringUpload) { |
|
194 |
+ $body[] = '--' . $boundary; |
|
195 |
+ $body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="string.' . $value->extension . '"'; |
|
196 |
+ $body[] = 'Content-Type: application/octet-stream'; |
|
197 |
+ $body[] = ''; |
|
198 |
+ $body[] = $value->string; |
|
199 |
+ } else { |
|
200 |
+ $body[] = '--' . $boundary; |
|
201 |
+ $body[] = 'Content-Disposition: form-data; name="' . $key . '"'; |
|
202 |
+ $body[] = ''; |
|
203 |
+ $body[] = (is_bool($value) ? var_export($value, true) : $value); |
|
204 |
+ } |
|
205 |
+ } |
|
206 |
+ $body[] = '--' . $boundary . '--'; |
|
207 |
+ $body[] = ''; |
|
208 |
+ $contentType = 'multipart/form-data; boundary=' . $boundary; |
|
209 |
+ $content = join($crlf, $body); |
|
210 |
+ $contentLength = strlen($content); |
|
211 |
+ |
|
212 |
+ curl_setopt( |
|
213 |
+ $ch, |
|
214 |
+ CURLOPT_HTTPHEADER, |
|
215 |
+ array( |
|
216 |
+ 'Content-Length: ' . $contentLength, |
|
217 |
+ 'Expect: 100-continue', |
|
218 |
+ 'Content-Type: ' . $contentType, |
|
219 |
+ ) |
|
220 |
+ ); |
|
221 |
+ |
|
222 |
+ if ($this->debug) { |
|
223 |
+ echo "Request payload:\n\n$content\n\n"; |
|
224 |
+ } |
|
225 |
+ |
|
226 |
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $content); |
|
227 |
+ } |
|
228 |
+} |