This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSMSGlobalAPIWrapper.php
143 lines (122 loc) · 3.86 KB
/
SMSGlobalAPIWrapper.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
require 'vendor/autoload.php';
use Httpful\Http;
use Httpful\Request;
use Httpful\Mime;
/**
* Wrapper class to open and handle communication from/to SMS Global REST API.
* This file only serves as a illustration purpose only and should be revised if included into your project.
*
* @author Huy Dinh <huy.dinh@smsglobal.com>
*/
class SMSGlobalAPIWrapper
{
protected $key;
protected $secret;
protected $protocol;
protected $host;
protected $port;
protected $apiVersion;
protected $extraData;
protected $debug = false;
/**
* @param $key
* @param $secret
* @param string $protocol
* @param string $host
* @param string $port
* @param string $apiVersion
* @param string $extraData
* @param bool $debug
*/
public function __construct($key, $secret, $protocol = "http", $host = "api.smsglobal.com", $port = "80", $apiVersion = "v1", $extraData = "", $debug = false)
{
$this->key = $key;
$this->secret = $secret;
$this->protocol = strtolower($protocol);
$this->host = $host;
$this->port = $port;
$this->apiVersion = $apiVersion;
$this->extraData = $extraData;
$this->debug = $debug;
}
public function get($action, $id = null) {
return $this->connect("GET", $action, $id);
}
public function post($action, $id = null) {
return $this->connect("POST", $action, $id);
}
public function delete($action, $id = null) {
return $this->connect("DELETE", $action, $id);
}
/**
* @param $method
* @param $action
* @param null $id
* @return mixed
* @throws Exception when there's something wrong with the request/response
*/
private function connect($method, $action, $id = null) {
$action = !$id ? "$action/" : "$action/id/$id/";
$uri = "{$this->protocol}://{$this->host}:{$this->port}/{$this->apiVersion}/{$action}";
$request = Request::init()
->expects(Mime::JSON)
->addHeaders($this->getAuthorisationHTTPHeader($method, $action))
->uri($uri);
if ($this->protocol == "https") {
// Dangerous, should not use for PRODUCTION
$request->withoutStrictSSL();
}
switch ($method) {
case "GET":
$request->method(Http::GET);
break;
case "POST":
$request->method(Http::POST);
break;
case "DELETE":
$request->method(Http::DELETE);
break;
}
if ($this->debug) {
$request->_debug = true;
}
try {
// do it!
$response = $request->send();
return $response->body;
} catch (Exception $e) {
if($this->debug) {
throw $e;
} else {
throw new \Exception('Unable to connect to the server.');
}
}
}
/**
* @param $method
* @param $action
* @return array of HTTP headers in key/value pair
*/
private function getAuthorisationHTTPHeader($method, $action)
{
$algorithm = "sha256";
$timestamp = time();
# Random String
$nonce = md5(microtime() . mt_rand());
# Hence
$rawStr = $timestamp . "\n"
. $nonce . "\n"
. $method . "\n"
. '/'. $this->apiVersion .'/'. $action . "\n"
. $this->host . "\n"
. $this->port . "\n"
. $this->extraData . "\n";
var_dump($rawStr);
# Encryptions
$hash = hash_hmac($algorithm, $rawStr, $this->secret, true);
$hash = base64_encode($hash);
return array("Authorization" => sprintf('MAC id="%s", ts="%s", nonce="%s", mac="%s"', $this->key, $timestamp, $nonce, $hash));
}
}
?>