forked from tamkeen-tms/mobily-sms-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.php
103 lines (89 loc) · 2.74 KB
/
client.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
<?php namespace MobilyAPI;
/**
* Mobily SMS service API client
* @package MobilyAPI
*/
class Client{
/**
* The setup
* @var array
*/
public $setup = [
'username' => null,
'password' => null,
'base_uri' => 'http://www.mobily.ws/api/',
'request_time_out' => 5,
'verify_ssl_certificate' => false,
'debug' => false
];
/**
* The default values for request params
* @var array
*/
public $paramDefaults = [
'sender' => null,
'domainName' => null,
'applicationType' => '68',
'lang' => 3
];
/**
* @param $mobileNumber The account mobile number (Mobily uses the mobile phone number as the username)
* @param $password The account password
* @param array $paramDefaults [optional] Request default params overrides
* @param array $setup [optional] Setup overrides
*/
public function __construct($mobileNumber, $password, array $paramDefaults = [], array $setup = []){
// The account
$this->setup['username'] = $mobileNumber;
$this->setup['password'] = $password;
// The client setup
$this->setup = array_merge($this->setup, $setup);
// The request defaults params
$this->paramDefaults = array_merge($this->paramDefaults, $paramDefaults);
// Set a default "domain name"
if(empty($this->paramDefaults['domainName'])){
if (isset($_SERVER['SERVER_NAME'])) {
$this->paramDefaults['domainName'] = $_SERVER['SERVER_NAME'];
} elseif( function_exists('url')) { // laravel specific
$this->paramDefaults['domainName'] = url('/');
} else { // empty
$this->paramDefaults['domainName'] = '';
}
}
}
/**
* Create a new request instance
*
* @param string $function The targeted function, e.g. "sendSMS" or "sendingStatus"
* @param array $params [optional] The request params
*
* @return Request
* @throws \Exception
*/
public function createRequest($function, array $params = []){
// The request class name
$requestClass = "MobilyAPI\\requests\\$function";
// Check if the name isn't valid
if(!class_exists($requestClass)){
throw new \Exception('Invalid request name: ' . basename($requestClass));
}
// Prepend the client to the params
array_unshift($params, $this);
// Call the request
return new $requestClass(...$params);
}
/**
* Makes a request and directly tells if it was successful
* @param $function
* @param array $params
*
* @return Response
*/
public function request($function, array $params = []){
// Make the request
$request = $this->createRequest($function, $params);
// Send the request
$response = $request->send();
return $response;
}
}