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 Semaphore |
11
|
|
|
* @package ridvanbaluyos\sms\providers |
12
|
|
|
*/ |
13
|
|
|
class Semaphore extends Sms implements SmsProviderServicesInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $className; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Semaphore 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
|
|
|
$query = [ |
40
|
|
|
'from' => $conf['from'], |
41
|
|
|
'api' => $conf['api'], |
42
|
|
|
'number' => $phoneNumber, |
43
|
|
|
'message' => $message, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$url = $conf['url'] . '?' . http_build_query($query); |
47
|
|
|
|
48
|
|
|
$ch = curl_init(); |
49
|
|
|
curl_setopt($ch,CURLOPT_URL,$url); |
50
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
51
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 240); |
52
|
|
|
$result = curl_exec($ch); |
53
|
|
|
curl_close($ch); |
54
|
|
|
|
55
|
|
|
$result = json_decode($result); |
56
|
|
|
|
57
|
|
|
if ($result->status === 'success') { |
58
|
|
|
return $this->response(202, $result, null, $this->className); |
59
|
|
|
} else { |
60
|
|
|
return $this->response(500, $result->message, null, $this->className); |
61
|
|
|
} |
62
|
|
|
} catch (Exception $e) { |
|
|
|
|
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* This function gets the account balance. |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function balance() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className]; |
75
|
|
|
$query = [ |
76
|
|
|
'api' => $conf['api'], |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$url = $conf['url'] . '/account?' . http_build_query($query); |
80
|
|
|
|
81
|
|
|
$ch = curl_init(); |
82
|
|
|
curl_setopt($ch,CURLOPT_URL,$url); |
83
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
84
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 240); |
85
|
|
|
$result = curl_exec($ch); |
86
|
|
|
curl_close($ch); |
87
|
|
|
|
88
|
|
|
$result = json_decode($result); |
89
|
|
|
|
90
|
|
|
if ($result->status === 'success') { |
91
|
|
|
return $this->response(200, $result, null, $this->className); |
92
|
|
|
} else { |
93
|
|
|
return $this->response(500, $result->message, null, $this->className); |
94
|
|
|
} |
95
|
|
|
} catch (Exception $e) { |
|
|
|
|
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |