Skip to content

Commit 9bfc922

Browse files
author
grzechu
committed
refactorization, PSR-0, PSR-2, composer support , thanks to @adam187
1 parent 2fe1769 commit 9bfc922

File tree

13 files changed

+499
-222
lines changed

13 files changed

+499
-222
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
A php library which implements the functionality of FreshMail REST API.
44

5-
## Installation
5+
## Installation via composer
66

7-
require_once 'class.rest.php'
7+
Add to `composer.json` file:
8+
9+
{
10+
"require": {
11+
"freshmail/rest-api": "dev-master"
12+
}
13+
}
14+
15+
Use in php project:
16+
17+
use FreshMail\RestApi as FmRestApi;
18+
19+
## Installation by hand
20+
21+
require_once 'class.rest.php';
822
require_once 'config.php';
923

1024
## Examples
1125

12-
All samples included in samples directory.
26+
All samples included in samples directory.
27+
28+
## Thanks to
29+
@adam187

class.rest.php

Lines changed: 6 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -1,211 +1,13 @@
11
<?php
22

3+
require_once 'src/FreshMail/RestApi.php';
4+
require_once 'src/FreshMail/RestException.php';
5+
36
/**
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.
88
*
9+
* @deprecated
910
*/
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
16912
{
17013
}
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-
*/

composer.json

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
{
2-
"name": "FreshMail/REST-API",
3-
"description": "REST API",
2+
"name": "freshmail/rest-api",
3+
"description": "A php library which implements the functionality of FreshMail REST API.",
4+
"keywords": ["freshmail", "rest"],
5+
"type": "library",
46
"license": "GPL-3.0",
5-
"authors": [{
6-
"name": "Tadeusz Kania",
7-
"email": "tadeusz.kania@freshmail.pl",
8-
"homepage": "https://freshmail.pl",
9-
"role": "Developer"
10-
},{
11-
"name": "Piotr Suszalski",
12-
"email": "piotr.suszalski@freshmail.pl",
13-
"homepage": "https://freshmail.pl",
14-
"role": "Developer"
15-
}
7+
"authors": [
8+
{
9+
"name": "Tadeusz Kania",
10+
"email": "tadeusz.kania@freshmail.pl",
11+
"homepage": "https://freshmail.pl",
12+
"role": "Developer"
13+
},
14+
{
15+
"name": "Piotr Suszalski",
16+
"email": "piotr.suszalski@freshmail.pl",
17+
"homepage": "https://freshmail.pl",
18+
"role": "Developer"
19+
},
20+
{
21+
"name": "Grzegorz Gorczyca",
22+
"email": "grzegorz.gorczyca@freshmail.pl",
23+
"homepage": "https://freshmail.pl",
24+
"role": "Developer"
25+
},
26+
{
27+
"name": "Piotr Leżoń",
28+
"email": "piotr.lezon@freshmail.pl",
29+
"homepage": "https://freshmail.pl",
30+
"role": "Developer"
31+
}
1632
],
1733
"require": {
18-
"php": ">=5.3.0"
34+
"php": ">=5.3.0",
35+
"ext-curl": "*",
36+
"ext-json": "*"
1937
},
2038
"autoload": {
21-
"files": ["class.rest.php", "config.php"]
39+
"psr-0": {
40+
"FreshMail": "src/"
41+
}
2242
}
2343
}

samples/Mail/Send.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require '../../src/FreshMail/RestApi.php';
4+
require '../../src/FreshMail/RestException.php';
5+
require '../../config.php';
6+
7+
$rest = new \FreshMail\RestApi();
8+
9+
$rest->setApiKey(FM_API_KEY);
10+
$rest->setApiSecret(FM_API_SECRET);
11+
12+
$data = [
13+
'subscriber' => 'john@doe.tld',
14+
'subject' => 'Message subject',
15+
'html' => '<h1>HTML message content</h1>',
16+
'text' => 'Text message content',
17+
'from' => 'jane@doe.tld',
18+
'from_name' => 'Jane Doe'
19+
];
20+
21+
try {
22+
$response = $rest->doRequest('mail', $data);
23+
24+
echo 'Mail sent, received data: ';
25+
print_r($response);
26+
echo PHP_EOL;
27+
} catch (Exception $e) {
28+
echo 'Error message: ' . $e->getMessage() . ', Error code: ' . $e->getCode() . ', HTTP code: ' . $rest->getHttpCode() . PHP_EOL;
29+
}

samples/Ping/Ping.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require '../../src/FreshMail/RestApi.php';
4+
require '../../src/FreshMail/RestException.php';
5+
require '../../config.php';
6+
7+
$rest = new \FreshMail\RestApi();
8+
9+
$rest->setApiKey(FM_API_KEY);
10+
$rest->setApiSecret(FM_API_SECRET);
11+
12+
try {
13+
$response = $rest->doRequest('ping');
14+
15+
echo 'Ping ok, received data: ';
16+
print_r($response);
17+
echo PHP_EOL;
18+
} catch (Exception $e) {
19+
echo 'Error message: ' . $e->getMessage() . ', Error code: ' . $e->getCode() . ', HTTP code: ' . $rest->getHttpCode() . PHP_EOL;
20+
}
21+
22+
try {
23+
$response = $rest->doRequest('ping', ['testKey' => 'testValue']);
24+
25+
echo 'Ping ok, received data: ';
26+
print_r($response);
27+
echo PHP_EOL;
28+
} catch (Exception $e) {
29+
echo 'Error message: ' . $e->getMessage() . ', Error code: ' . $e->getCode() . ', HTTP code: ' . $rest->getHttpCode() . PHP_EOL;
30+
}

0 commit comments

Comments
 (0)