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