-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpirius.php
More file actions
103 lines (80 loc) · 2.55 KB
/
Copy pathSpirius.php
File metadata and controls
103 lines (80 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
class Spirius
{
protected $key = null;
protected $username = null;
protected $baseUrl = 'https://rest.spirius.com/v1';
protected $sendEndpoint = '/sms/mt/send';
protected $messageBody = [];
protected $authVersion = 'SpiriusSmsV1';
public function __construct($sharedKey, $username) {
$this->key = $sharedKey;
$this->username = $username;
}
public function createHeaders($signature, $timestamp): array
{
return [
'X-SMS-Timestamp' => $timestamp,
'Authorization' => "{$this->authVersion} {$this->username}:{$signature}",
'Content-Type' => 'application/json',
];
}
public function createSignature($timestamp): string
{
$bodyHash = sha1(utf8_encode(json_encode($this->messageBody)));
$messageToSign = implode('\n', [
$this->authVersion,
$timestamp,
'POST',
$this->sendEndpoint,
$bodyHash,
]);
$digest = hash_hmac(
'sha256',
utf8_encode($messageToSign),
utf8_encode($this->key),
true,
);
return utf8_decode(base64_encode($digest));
}
public function performRequest($timestamp): array
{
$signature = $this->createSignature($timestamp);
$headers = $this->createHeaders($signature, $timestamp);
// Guzzle (https://docs.guzzlephp.org/en/stable) is used to make the HTTP request
// https://docs.guzzlephp.org/en/stable/overview.html#installation
$client = new Client();
$response = $client->request(
'POST',
$this->baseUrl . $this->sendEndpoint,
[
'headers' => $headers,
'json' => $this->messageBody,
'connect_timeout' => 5,
]
);
return json_decode($response->getBody(), true);
}
public function sendSms($data): array
{
$timestamp = time();
$this->messageBody = [
'message' => $data['message'],
'from' => $data['sender'],
'to' => $data['recipient'],
];
return $this->performRequest($timestamp);
}
}
$spirius = new Spirius(
// Key is available on the account page on https://portal.spirius.com
'78701a30f3f83437df6284ced6fc9ba58ca6a31c8031df3e8cb7a17eca7b91ed',
'test',
);
$spirius->sendSms([
'message' => 'Hello world!',
'recipient' => '+46123456789',
'sender' => 'SPIRIUS',
]);