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