-
Notifications
You must be signed in to change notification settings - Fork 1
/
TeleforteSMS.class.php
222 lines (194 loc) · 5.45 KB
/
TeleforteSMS.class.php
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
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Teleforte SMS API PHP wrapper
*
* @author Steffen Bewersdorff
* @mail [email protected]
* @website http://steffenbew.com
* @version 0.1
* @date 09.02.2011
* @license MIT License
*
* Copyright (c) 2011 Steffen Bewersdorff, http://steffenbew.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class TeleforteSMS
{
const API_URL = 'https://sms.lcx.at/messages/post_req';
private $recipients = array(), $api_key, $sender;
/*
* Initialize class with API key and optional sender (can be changed later with setter function)
*
* @api_key string API key
* @sender string SMS sender
*
* @return void
*/
function __construct($api_key = NULL, $sender = NULL)
{
if($api_key === NULL)
{
throw new Exception("You must provide an API key.");
}
$this->api_key = $api_key;
if($sender !== NULL)
{
$this->set_sender($sender);
}
}
/**
* Add a recipient to the list
*
* @number string telephone number (should be formatted as 491234567890)
* @return void
*/
public function add_recipient($number = NULL)
{
# validate numeric
if(preg_match("/^[0-9]+$/", $number) !== 1)
{
throw new Exception("Recipient is not a number: $number");
}
array_push($this->recipients, $number);
}
/**
* Get an array of all recipients
*
* @return array recipients
*/
public function get_recipients()
{
return $this->recipients;
}
/**
* Set sender of SMS (can be alphanumeric)
*
* @sender string alphanumeric sender
*
* @return void
*/
public function set_sender($sender = NULL)
{
# validate alphanumeric
if(preg_match("/^[0-9a-zA-Z\s]{1,11}$/", $sender) !== 1)
{
throw new Exception("Sender must be alphanumeric and cannot be longer than 11 characters.");
}
$this->sender = $sender;
}
/**
* Send message with given text
* (check if all required options have been set)
*
* @msg string text message
*
* @return int response id for further tracking
*/
public function send_message($msg = NULL)
{
# check for all required options
if($this->sender === NULL)
{
throw new Exception("You must provide a sender.");
}
if(count($this->recipients) === 0)
{
throw new Exception("You must provide at least one recipient.");
}
if($msg === NULL) {
throw new Exception("You must provide a message.");
}
if(strlen($msg) > 160)
{
throw new Exception("The message must not be longer than 160 characters.");
}
# set post values, expected by api
$post_values = array(
'recipient' => implode(';', $this->recipients),
'sender' => $this->sender,
'message' => $msg,
'api_key' => $this->api_key
);
return $this->send_request($post_values);
}
/**
* Send API request with all supplied data
*
* @post_values array message data
*
* @return int response id
*/
private function send_request($post_values = array())
{
# set cURL options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_USERAGENT => "TeleforteSMS-API-PHP-wrapper",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_values,
CURLOPT_SSL_VERIFYPEER => FALSE
);
# send request
$ch = curl_init(self::API_URL);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
# handle heavy errors
if($err !== 0)
{
# Error at receiving file
throw new Exception("Could not fetch file: $errmsg");
}
if($httpCode !== 200)
{
# Wrong HTTP response
throw new Exception("Request failed. Status code $httpCode returned.");
}
# parse response and return
$valid_response = preg_match("/^([0-9]+):([0-9]+)$/", $response, $response_matches);
if($valid_response !== 1)
{
throw new Exception("Unknown response: $response");
}
# fetch response values
$response_code = (int) $response_matches[1];
$response_id = (int) $response_matches[2];
if($response_code !== 200)
{
switch($response_code)
{
case 0:
throw new Exception("Host failed to send message. Fatal Error. Please contact support.");
break;
case 1:
throw new Exception("Host failed to send message. Message length exceeds max length of 160 chars.");
break;
default:
throw new Exception("Host failed to send message. Response code $response_code. Response ID $response_id.");
}
}
return $response_id;
}
}