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
|
|
|
date_default_timezone_set('Asia/Manila'); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RisingTide |
13
|
|
|
* @package ridvanbaluyos\sms\providers |
14
|
|
|
*/ |
15
|
|
|
class RisingTide extends Sms implements SmsProviderServicesInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $className; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* RisingTide constructor. |
24
|
|
|
*/ |
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->className = substr(get_called_class(), strrpos(get_called_class(), '\\') + 1); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This function sends the SMS. |
32
|
|
|
* |
33
|
|
|
* @param $phoneNumber |
34
|
|
|
* @param $message |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function send($phoneNumber, $message) |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
$conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className]; |
41
|
|
|
|
42
|
|
|
$messageId = $this->generateMessageId(32); |
43
|
|
|
$content = array( |
44
|
|
|
'id' => $messageId, |
45
|
|
|
'from' => $conf['from'], |
46
|
|
|
'to' => $phoneNumber, |
47
|
|
|
'content_type' => "text/plain", |
48
|
|
|
'body' => $message, |
49
|
|
|
'usagetype' => $conf['usagetype'], |
50
|
|
|
'date' => date('Y-m-d') . 'T' . date('H:i:s') . '+0800', |
51
|
|
|
'delivery_receipt_url' => '', |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$content = json_encode($content); |
55
|
|
|
$signature = base64_encode(join(':', array($conf['client_id'], $conf['client_password']))); |
56
|
|
|
|
57
|
|
|
$headers = array( |
58
|
|
|
'Authorization: Basic ' . $signature, |
59
|
|
|
'Accept: /documents', |
60
|
|
|
'Date: ' . date('r'), |
61
|
|
|
'Content-Type: application/vnd.net.wyrls.Document-v3+json', |
62
|
|
|
'Content-Length: ' . strlen($content), |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$url = $conf['url']; |
66
|
|
|
|
67
|
|
|
$ch = curl_init(); |
68
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
69
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
70
|
|
|
curl_setopt($ch, CURLOPT_HEADER, TRUE); |
71
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); |
72
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $content); |
73
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
74
|
|
|
$result = curl_exec($ch); |
75
|
|
|
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
76
|
|
|
|
77
|
|
|
return $this->response($responseCode, $result, null, $this->className); |
78
|
|
|
} catch (Exception $e) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* This function checks the account balance. |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
|
|
public function balance() |
88
|
|
|
{ |
89
|
|
|
return $this->response(404, [], $this->className . ' currently does not support this feature.', $this->className); |
90
|
|
|
} |
91
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.