1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pnlinh\InfobipSms; |
4
|
|
|
|
5
|
|
|
class InfobipSmsService |
6
|
|
|
{ |
7
|
|
|
/** @var int retry time */ |
8
|
|
|
public const RETRY_TIME = 3; |
9
|
|
|
|
10
|
|
|
/** @var string */ |
11
|
|
|
private $username; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $password; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $postUrl = 'https://api.infobip.com/sms/1/text/single'; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $header = ['Content-Type:application/json', 'Accept:application/json']; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $from; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* InfobipSmsService constructor. |
27
|
|
|
* |
28
|
|
|
* @param $from |
29
|
|
|
* @param $username |
30
|
|
|
* @param $password |
31
|
|
|
*/ |
32
|
|
|
public function __construct($from, $username, $password) |
33
|
|
|
{ |
34
|
|
|
$this->from = $from; |
35
|
|
|
$this->username = $username; |
36
|
|
|
$this->password = $password; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set post data. |
41
|
|
|
* |
42
|
|
|
* @param $to |
43
|
|
|
* @param $text |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
private function setPostData($to, $text) |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'from' => $this->from, |
51
|
|
|
'to' => (array) $to, |
52
|
|
|
'text' => $text, |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Do send sms. |
58
|
|
|
* |
59
|
|
|
* @see https://www.infobip.com/en/blog/step-by-step-sms-api-php-tutorial-create-your-new-web-app |
60
|
|
|
* |
61
|
|
|
* @param $to |
62
|
|
|
* @param $text |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function send($to, $text) |
67
|
|
|
{ |
68
|
|
|
$postDataJson = json_encode($this->setPostData($to, $text)); |
69
|
|
|
|
70
|
|
|
// Set retry time if response is null or empty |
71
|
|
|
$retrytime = 0; |
72
|
|
|
retry_from_here: |
73
|
|
|
|
74
|
|
|
$ch = curl_init(); |
75
|
|
|
// Setting options |
76
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->postUrl); |
77
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header); |
78
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
79
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password); |
80
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); |
81
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
82
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
83
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); |
84
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
85
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
86
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson); |
87
|
|
|
|
88
|
|
|
// Response of the POST request |
89
|
|
|
$response = curl_exec($ch); |
90
|
|
|
|
91
|
|
|
if ($response === '' && $retrytime <= static::RETRY_TIME) { |
92
|
|
|
$retrytime++; |
93
|
|
|
goto retry_from_here; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
97
|
|
|
$responseBody = json_decode($response); |
|
|
|
|
98
|
|
|
curl_close($ch); |
99
|
|
|
|
100
|
|
|
return [$httpCode, $responseBody]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|