|
1
|
|
|
<?php |
|
2
|
|
|
namespace ridvanbaluyos\sms\providers; |
|
3
|
|
|
|
|
4
|
|
|
use ridvanbaluyos\sms\SmsProviderServicesInterface as SmsProviderServicesInterface; |
|
5
|
|
|
use ridvanbaluyos\sms\Sms as Sms; |
|
6
|
|
|
use Exception as Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Nexmo |
|
10
|
|
|
* @package ridvanbaluyos\sms\providers |
|
11
|
|
|
*/ |
|
12
|
|
|
class Nexmo extends Sms implements SmsProviderServicesInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $className; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Nexmo constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->className = substr(get_called_class(), strrpos(get_called_class(), '\\') + 1); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This function sends the SMS. |
|
29
|
|
|
* |
|
30
|
|
|
* @param $phoneNumber |
|
31
|
|
|
* @param $message |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function send($phoneNumber, $message) |
|
35
|
|
|
{ |
|
36
|
|
|
try { |
|
37
|
|
|
$conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className]; |
|
38
|
|
|
|
|
39
|
|
|
$query = array( |
|
40
|
|
|
'api_key' => $conf['api_key'], |
|
41
|
|
|
'api_secret' => $conf['api_secret'], |
|
42
|
|
|
'from' => $conf['from'], |
|
43
|
|
|
'to' => $phoneNumber, |
|
44
|
|
|
'text' => $message, |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$query = http_build_query($query); |
|
48
|
|
|
$ch = curl_init(); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_URL, $conf['url'] . '/sms/json'); |
|
50
|
|
|
curl_setopt($ch, CURLOPT_POST, count($query)); |
|
51
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); |
|
52
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
53
|
|
|
$result = curl_exec($ch); |
|
54
|
|
|
curl_close($ch); |
|
55
|
|
|
|
|
56
|
|
|
$result = json_decode($result); |
|
57
|
|
|
|
|
58
|
|
|
if (intval($result->messages[0]->status) === 0) { |
|
59
|
|
|
return $this->response(200, $result, null, $this->className); |
|
60
|
|
|
} else { |
|
61
|
|
|
return $this->response(500, $result, null, $this->className); |
|
62
|
|
|
} |
|
63
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* This function gets the account balance. |
|
70
|
|
|
* |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function balance() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className]; |
|
76
|
|
|
|
|
77
|
|
|
$query = array( |
|
78
|
|
|
'api_key' => $conf['api_key'], |
|
79
|
|
|
'api_secret' => $conf['api_secret'] |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$query = http_build_query($query); |
|
83
|
|
|
|
|
84
|
|
|
$ch = curl_init(); |
|
85
|
|
|
curl_setopt($ch, CURLOPT_URL, $conf['url'] . '/account/get-balance?' . $query); |
|
86
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
87
|
|
|
$result = curl_exec($ch); |
|
88
|
|
|
curl_close($ch); |
|
89
|
|
|
|
|
90
|
|
|
$result = json_decode($result); |
|
91
|
|
|
|
|
92
|
|
|
if (is_float($result->value) || is_integer($result->value)) { |
|
93
|
|
|
return $this->response(200, $result, null, $this->className); |
|
94
|
|
|
} else { |
|
95
|
|
|
return $this->response(500, $result, null, $this->className); |
|
96
|
|
|
} |
|
97
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|