-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiqSmsSending.php
111 lines (87 loc) · 2.94 KB
/
iqSmsSending.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
<?php
namespace Grinderspro;
/**
* Class iqSmsSending
*
* Wrapper class for iqsms.ru
*
* @author Grigorij Miroshnichenko <[email protected]>
* @copyright 2015
*/
class iqSmsSending
{
const API_LOGIN = SMS_API_LOGIN;
const API_PASSWORD = SMS_API_PASSWORD;
const API_HOST = 'api.iqsms.ru';
const SENDER_NAME = SMS_SENDER_NAME;
const ACTIVE = true;
const ERROR_EMPTY_API_LOGIN = 'Empty api login not allowed';
const ERROR_EMPTY_API_PASSWORD = 'Empty api password not allowed';
const ERROR_EMPTY_RESPONSE = 'errorEmptyResponse';
public function getHost() {
return self::API_HOST;
}
public function status($messages) {
return $this->_sendRequest('status', array('messages' => $messages));
}
/**
* Получаем балланс
* @return mixed
*/
public function balance() {
return $this->_sendRequest('balance');
}
/**
* @param string $messages
* @param null $statusQueueName
* @param null $scheduleTime
* @return bool|mixed
*/
public function send($messages, $statusQueueName = null, $scheduleTime = null) {
if (!empty($this->sender))
foreach ($messages as &$message)
$message['sender'] = self::SENDER_NAME;
$params = array(
'messages' => $messages,
'statusQueueName' => $statusQueueName,
'scheduleTime' => $scheduleTime,
);
if (!self::ACTIVE)
return true;
return $this->_sendRequest('send', $params);
}
private function _sendRequest($uri, $params = null) {
$url = $this->_getUrl($uri);
$data = $this->_formPacket($params);
$client = curl_init($url);
curl_setopt_array($client,
array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array('Host: ' . $this->getHost()),
CURLOPT_POSTFIELDS => $data,
CURLOPT_CONNECTTIMEOUT => 10,
));
$body = curl_exec($client);
curl_close($client);
if (empty($body))
throw new Exception(self::ERROR_EMPTY_RESPONSE);
$decodedBody = json_decode($body, true);
if (is_null($decodedBody))
throw new Exception($body);
return $decodedBody;
}
private function _getUrl($uri) {
return 'http://' . self::API_HOST . '/messages/v2/' . $uri . '.json';
}
private function _formPacket($params = null) {
$params['login'] = self::API_LOGIN;
$params['password'] = self::API_PASSWORD;
foreach ($params as $key => $value)
if (empty($value))
unset($params[$key]);
$packet = json_encode($params);
return $packet;
}
}