|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +require_once 'src/FreshMail/RestApi.php'; |
| 4 | +require_once 'src/FreshMail/RestException.php'; |
| 5 | + |
3 | 6 | /**
|
4 |
| - * Klasa do uwierzytelniania i wysyłania danych za pomocą REST API FreshMail |
5 |
| - * |
6 |
| - * @author Tadeusz Kania, Piotr Suszalski, Grzegorz Gorczyca |
7 |
| - * @since 2012-06-14 |
| 7 | + * For backward compatibility only. This class will be removed in feature versions. |
8 | 8 | *
|
| 9 | + * @deprecated |
9 | 10 | */
|
10 |
| - |
11 |
| -class FmRestApi |
12 |
| -{ |
13 |
| - |
14 |
| - private $strApiSecret = null; |
15 |
| - private $strApiKey = null; |
16 |
| - private $response = null; |
17 |
| - private $rawResponse = null; |
18 |
| - private $httpCode = null; |
19 |
| - private $contentType = 'application/json'; |
20 |
| - |
21 |
| - const host = 'https://api.freshmail.com/'; |
22 |
| - const prefix = 'rest/'; |
23 |
| - const defaultFilePath = '/tmp/'; |
24 |
| - //-------------------------------------------------------------------------- |
25 |
| - |
26 |
| - /** |
27 |
| - * Metoda pobiera kody błędów |
28 |
| - * |
29 |
| - * @return array |
30 |
| - */ |
31 |
| - public function getErrors() |
32 |
| - { |
33 |
| - if ( isset( $this->errors['errors'] ) ) { |
34 |
| - return $this->errors['errors']; |
35 |
| - } |
36 |
| - |
37 |
| - return false; |
38 |
| - } |
39 |
| - |
40 |
| - /** |
41 |
| - * @return array |
42 |
| - */ |
43 |
| - public function getResponse() |
44 |
| - { |
45 |
| - return $this->response; |
46 |
| - } |
47 |
| - |
48 |
| - /** |
49 |
| - * @return array |
50 |
| - */ |
51 |
| - public function getRawResponse() |
52 |
| - { |
53 |
| - return $this->rawResponse; |
54 |
| - } |
55 |
| - |
56 |
| - /** |
57 |
| - * @return array |
58 |
| - */ |
59 |
| - public function getHttpCode() |
60 |
| - { |
61 |
| - return $this->httpCode; |
62 |
| - } |
63 |
| - |
64 |
| - /** |
65 |
| - * Metoda ustawia secret do API |
66 |
| - * |
67 |
| - * @param type $strSectret |
68 |
| - * @return rest_api |
69 |
| - */ |
70 |
| - public function setApiSecret( $strSectret = '' ) |
71 |
| - { |
72 |
| - $this->strApiSecret = $strSectret; |
73 |
| - return $this; |
74 |
| - } |
75 |
| - |
76 |
| - public function setContentType( $contentType = '' ) |
77 |
| - { |
78 |
| - $this->contentType = $contentType; |
79 |
| - return $this; |
80 |
| - } |
81 |
| - |
82 |
| - /** |
83 |
| - * Metoda ustawia klucz do API |
84 |
| - * |
85 |
| - * @param string $strKey |
86 |
| - * @return rest_api |
87 |
| - */ |
88 |
| - public function setApiKey ( $strKey = '' ) |
89 |
| - { |
90 |
| - $this->strApiKey = $strKey; |
91 |
| - return $this; |
92 |
| - } |
93 |
| - |
94 |
| - public function doRequest( $strUrl, $arrParams = array(), $boolRawResponse = false ) |
95 |
| - { |
96 |
| - if ( empty($arrParams) ) { |
97 |
| - $strPostData = ''; |
98 |
| - } elseif ( $this->contentType == 'application/json' ) { |
99 |
| - $strPostData = json_encode( $arrParams ); |
100 |
| - } elseif ( !empty($arrParams) ) { |
101 |
| - $strPostData = http_build_query( $arrParams ); |
102 |
| - } |
103 |
| - |
104 |
| - $strSign = sha1( $this->strApiKey . '/' . self::prefix . $strUrl . $strPostData . $this->strApiSecret ); |
105 |
| - |
106 |
| - $arrHeaders = array(); |
107 |
| - $arrHeaders[] = 'X-Rest-ApiKey: ' . $this->strApiKey; |
108 |
| - $arrHeaders[] = 'X-Rest-ApiSign: ' . $strSign; |
109 |
| - |
110 |
| - if ($this->contentType) { |
111 |
| - $arrHeaders[] = 'Content-Type: '.$this->contentType; |
112 |
| - } |
113 |
| - |
114 |
| - $resCurl = curl_init( self::host . self::prefix . $strUrl ); |
115 |
| - curl_setopt( $resCurl, CURLOPT_HTTPHEADER, $arrHeaders ); |
116 |
| - curl_setopt( $resCurl, CURLOPT_HEADER, true); |
117 |
| - curl_setopt( $resCurl, CURLOPT_RETURNTRANSFER, true); |
118 |
| - |
119 |
| - if ($strPostData) { |
120 |
| - curl_setopt( $resCurl, CURLOPT_POST, true); |
121 |
| - curl_setopt( $resCurl, CURLOPT_POSTFIELDS, $strPostData ); |
122 |
| - } |
123 |
| - |
124 |
| - $this->rawResponse = curl_exec( $resCurl ); |
125 |
| - $this->httpCode = curl_getinfo( $resCurl, CURLINFO_HTTP_CODE ); |
126 |
| - |
127 |
| - if ($boolRawResponse) { |
128 |
| - return $this->rawResponse; |
129 |
| - } |
130 |
| - |
131 |
| - $this->_getResponseFromHeaders($resCurl); |
132 |
| - |
133 |
| - if ($this->httpCode != 200) { |
134 |
| - $this->errors = $this->response['errors']; |
135 |
| - if (is_array($this->errors)) { |
136 |
| - foreach ($this->errors as $arrError) { |
137 |
| - throw new RestException($arrError['message'], $arrError['code']); |
138 |
| - } |
139 |
| - } |
140 |
| - } |
141 |
| - |
142 |
| - if (is_array($this->response) == false) { |
143 |
| - throw new Exception('Connection error - curl error message: '.curl_error($resCurl).' ('.curl_errno($resCurl).')'); |
144 |
| - } |
145 |
| - |
146 |
| - return $this->response; |
147 |
| - } |
148 |
| - |
149 |
| - private function _getResponseFromHeaders($resCurl) |
150 |
| - { |
151 |
| - $header_size = curl_getinfo($resCurl, CURLINFO_HEADER_SIZE); |
152 |
| - $header = substr($this->rawResponse, 0, $header_size); |
153 |
| - $TypePatern = '/Content-Type:\s*([a-z-Z\/]*)\s/'; |
154 |
| - preg_match($TypePatern, $header, $responseType); |
155 |
| - if(strtolower($responseType[1]) == 'application/zip') { |
156 |
| - $filePatern = '/filename\=\"([a-zA-Z0-9\.]+)\"/'; |
157 |
| - preg_match($filePatern, $header, $fileName); |
158 |
| - file_put_contents(self::defaultFilePath.$fileName[1], substr($this->rawResponse, $header_size)); |
159 |
| - $this->response = array('path' =>self::defaultFilePath.$fileName[1]); |
160 |
| - } else { |
161 |
| - $this->response = json_decode( substr($this->rawResponse, $header_size), true ); |
162 |
| - } |
163 |
| - return $this->response; |
164 |
| - } |
165 |
| - |
166 |
| -} |
167 |
| - |
168 |
| -class RestException extends Exception |
| 11 | +class FmRestApi extends \FreshMail\RestApi |
169 | 12 | {
|
170 | 13 | }
|
171 |
| - |
172 |
| - |
173 |
| -/* USAGE: ***** |
174 |
| -
|
175 |
| -$rest = new FmRestApi(); |
176 |
| -$rest->setApiSecret(API_SECRET); |
177 |
| -$rest->setApiKey(API_KEY); |
178 |
| -
|
179 |
| -//ping GET (do testowania autoryzacji) |
180 |
| -try { |
181 |
| - $response = $rest->doRequest('ping'); |
182 |
| - print_r($response); |
183 |
| -} catch (Exception $e) { |
184 |
| - //echo 'Code: '.$e->getCode().' Message: '.$e->getMessage()."\n"; |
185 |
| - print_r($rest->getResponse()); |
186 |
| -} |
187 |
| -
|
188 |
| -//ping POST (do testowania autoryzacji) |
189 |
| -try { |
190 |
| - $postdata = array('any required data'); |
191 |
| - $response = $rest->doRequest('ping', $postdata); |
192 |
| - print_r($response); |
193 |
| -} catch (Exception $e) { |
194 |
| - //echo 'Code: '.$e->getCode().' Message: '.$e->getMessage()."\n"; |
195 |
| - print_r($rest->getResponse()); |
196 |
| -} |
197 |
| -
|
198 |
| -//mail POST |
199 |
| -try { |
200 |
| - $data = array('subscriber' => 'put email address here', |
201 |
| - 'subject' => 'put subject', |
202 |
| - 'text' => 'put text message', |
203 |
| - 'html' => '<strong>put HTML message here</strong>'); |
204 |
| - $response = $rest->doRequest('mail', $data); |
205 |
| - print_r($response); |
206 |
| -} catch (Exception $e) { |
207 |
| - //echo 'Code: '.$e->getCode().' Message: '.$e->getMessage()."\n"; |
208 |
| - print_r($rest->getResponse()); |
209 |
| -} |
210 |
| -
|
211 |
| -*/ |
0 commit comments