1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\GoSms; |
4
|
|
|
|
5
|
|
|
use DomainException; |
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
7
|
|
|
use NotificationChannels\GoSms\Exceptions\CouldNotSendNotification; |
8
|
|
|
|
9
|
|
|
class GoSmsApi |
10
|
|
|
{ |
11
|
|
|
const FORMAT_JSON = 3; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $apiUrl = 'http://api.gosms.com.my/eapi/sms.aspx'; |
15
|
|
|
|
16
|
|
|
/** @var HttpClient */ |
17
|
|
|
protected $httpClient; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $company; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $username; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $password; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $sender; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $gateway; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $type; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $charge; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $maskid; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
protected $convert; |
45
|
|
|
|
46
|
|
|
public function __construct($config) |
47
|
|
|
{ |
48
|
|
|
$this->company = $config['company']; |
49
|
|
|
$this->username = $config['username']; |
50
|
|
|
$this->password = $config['password']; |
51
|
|
|
$this->sender = $config['sender']; |
52
|
|
|
$this->gateway = $config['gateway']; |
53
|
|
|
$this->mode = $config['mode']; |
|
|
|
|
54
|
|
|
$this->type = $config['type']; |
55
|
|
|
$this->charge = $config['charge']; |
56
|
|
|
$this->maskid = $config['maskid']; |
57
|
|
|
$this->convert = $config['convert']; |
58
|
|
|
|
59
|
|
|
$this->httpClient = new HttpClient([ |
60
|
|
|
'base_uri' => $this->apiUrl, |
61
|
|
|
'timeout' => 2.0, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $params |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
* |
70
|
|
|
* @throws CouldNotSendNotification |
71
|
|
|
*/ |
72
|
|
|
public function send($params) |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
$sendsms_url = "?company={$this->company}&user={$this->username}&password={$this->password}&gateway={$this->gateway}&mode={$this->mode}&type={$this->type}&hp={$params['hp']}&mesg={$params['mesg']}&charge={$this->charge}&maskid={$this->maskid}&convert={$this->convert}"; |
76
|
|
|
|
77
|
|
|
$response = $this->httpClient->request('GET', $this->apiUrl.$sendsms_url); |
78
|
|
|
|
79
|
|
|
$stream = $response->getBody(); |
80
|
|
|
|
81
|
|
|
$content = $stream->getContents(); |
82
|
|
|
|
83
|
|
|
$response = json_decode((string) $response->getBody(), true); |
84
|
|
|
|
85
|
|
|
if ($content == 'E01') { |
86
|
|
|
throw new \Exception('E01'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $response; |
90
|
|
|
} catch (DomainException $exception) { |
91
|
|
|
throw CouldNotSendNotification::exceptionGoSmsRespondedWithAnError($exception); |
92
|
|
|
} catch (\Exception $exception) { |
93
|
|
|
throw CouldNotSendNotification::couldNotCommunicateWithGoSms($exception); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: