/
coolsms.php
291 lines (266 loc) · 7.6 KB
/
coolsms.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
*
* Copyright (C) 2008-2015 NURIGO
* http://www.coolsms.co.kr
*
**/
class coolsms
{
private $api_key;
private $api_secret;
private $host = "http://api.coolsms.co.kr/sms";
private $version = "1.5";
private $sdk_version = "1.1";
private $path;
private $method;
private $timestamp;
private $salt;
private $result;
private $basecamp;
private $user_agent;
public function __construct($api_key, $api_secret, $basecamp=false)
{
if($basecamp)
{
$this->coolsms_user = $api_key;
$this->basecamp = true;
}
else
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
}
public function curlProcess()
{
$ch = curl_init();
// 1 = POST , 0 = GET
if($this->method==1)
$host = sprintf("%s/%s/%s", $this->host, $this->version, $this->path);
elseif($this->method==0)
$host = sprintf("%s/%s/%s?%s", $this->host, $this->version, $this->path, $this->content);
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION,3); // SSL 버젼 (https 접속시에 필요)
curl_setopt($ch, CURLOPT_HEADER, 0); // 헤더 출력 여부
curl_setopt($ch, CURLOPT_POST, $this->method); // Post Get 접속 여부
//Set POST DATA
if($this->method)
{
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->content);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // TimeOut 값
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 결과값을 받을것인지
$this->result = json_decode(curl_exec($ch));
//Handle errors
if(curl_errno($ch))
{
//$this->setError('-1', curl_error($ch));
echo "CURL_ERROR : ".curl_error($ch);
}
curl_close ($ch);
}
private function setContent($options)
{
if($this->method)
{
$this->content = array();
foreach($options as $key => $val)
{
if($key != "image")
$this->content[$key] = sprintf("\0%s", $val);
else
$this->content[$key] = "@".realpath("./$val");
}
}
else
{
foreach($options as $key => $val)
$this->content .= $key."=".urlencode($val)."&";
}
}
private function getSignature()
{
return hash_hmac('md5', (string)$this->timestamp.$this->salt, $this->api_secret);
}
private function addInfos($options)
{
$this->salt = uniqid();
$this->timestamp = (string)time();
if(!$options->User_Agent) $options->User_Agent = sprintf("PHP REST API %s", $this>version);
if(!$options->os_platform) $options->os_platform = $this->getOS();
if(!$options->dev_lang) $options->dev_lang = sprintf("PHP %s", phpversion());
if(!$options->sdk_version) $options->sdk_version = sprintf("PHP SDK %s", $this->sdk_version);
$options->salt = $this->salt;
$options->timestamp = $this->timestamp;
if($this->basecamp)
$options->coolsms_user = $this->coolsms_user;
else
$options->api_key = $this->api_key;
$options->signature = $this->getSignature();
$this->setContent($options);
$this->curlProcess();
}
/**
* $method
* GET = 0, POST, 1
* $path
* 'send' 'sent' 'cancel' 'balance'
*/
private function setMethod($path, $method)
{
$this->path = $path;
$this->method = $method;
}
private function getResult()
{
return $this->result;
}
/**
* @POST send method
* @param $options (options must contain api_key, salt, signature, to, from, text)
* @type, image, refname, country, datetime, mid, gid, subject, charset (optional)
* @returns an object(recipient_number, group_id, message_id, result_code, result_message)
*/
public function send($options)
{
$this->setMethod('send', 1);
$this->addInfos($options);
debugprint($this->getResult());
return $this->getResult();
}
/**
* @GET sent method
* @param $options (options can be optional)
* @count, page, s_rcpt, s_start, s_end, mid, gid (optional)
* @returns an object(total count, list_count, page, data['type', 'accepted_time', 'recipient_number', 'group_id', 'message_id', 'status', 'result_code', 'result_message', 'sent_time', 'text'])
*/
public function sent($options=null)
{
if(!$options)
$options = new stdClass();
$this->setMethod('sent', 0);
$this->addInfos($options);
return $this->getResult();
}
/**
* @POST cancel method
* @options must contain api_key, salt, signature
* @mid, gid (either one must be entered.)
*/
public function cancel($options)
{
$this->setMethod('cancel', 1);
$this->addInfos($options);
return $this->getResult();
}
/**
* @GET balance method
* @options must contain api_key, salt, signature
* @return an object(cash, point)
*/
public function balance()
{
$this->setMethod('balance', 0);
$this->addInfos($options = new stdClass());
return $this->getResult();
}
/**
* @GET status method
* @options must contain api_key, salt, signature
* @return an object(registdate, sms_average, sms_sk_average, sms_kt_average, sms_lg_average, mms_average, mms_sk_average, mms_kt_average, mms_lg_average)
* this method is made for Coolsms inc. internal use
*/
public function status($options)
{
$this->setMethod('status', 0);
$this->addInfos($options);
return $this->getResult();
}
private function objectToArray($d)
{
if (is_object($d))
{
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d))
{
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else
{
// Return array
return $d;
}
}
function getOS()
{
$user_agent = $this->user_agent;
$os_platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value)
{
if (preg_match($regex, $user_agent))
{
$os_platform = $value;
}
}
return $os_platform;
}
function getBrowser()
{
$user_agent = $this->user_agent;
$browser = "Unknown Browser";
$browser_array = array(
'/msie/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/opera/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror',
'/mobile/i' => 'Handheld Browser'
);
foreach ($browser_array as $regex => $value)
{
if (preg_match($regex, $user_agent))
{
$browser = $value;
}
}
return $browser;
}
}