Skip to content

Commit 93dadfe

Browse files
committed
Version 4.0.0
Updated SDK to support Async SMPP API
1 parent f30ae18 commit 93dadfe

10 files changed

+178
-19
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "mobiweb/sdk",
3-
"description": "3.0.1 PHP library for communicating with the MobiWeb REST APIs.",
3+
"description": "4.0.0 PHP library for communicating with the MobiWeb REST APIs.",
44
"minimum-stability": "stable",
55
"keywords": [
6-
"sms", "api", "mobiweb", "php", "messaging", "hlr", "hlr lookup", "mobile lookup", "mobile number lookup", "OTP", "one time password", "2FA", "two factor authentication"
6+
"sms", "api", "mobiweb", "php", "messaging", "hlr", "hlr lookup", "mobile lookup", "mobile number lookup", "OTP", "one time password", "2FA", "two factor authentication", "smpp", "asynch"
77
],
88
"type": "library",
99
"license": "MIT",

src/MobiWeb/Rest/AsynchClient.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace MobiWeb\Rest;
4+
5+
use MobiWeb\Rest\Authentication as Auth;
6+
use MobiWeb\Rest\Message;
7+
use MobiWeb\Rest\Utility as Util;
8+
9+
class AsynchClient {
10+
11+
protected $auth;
12+
const API_ENDPOINT = "https://apix.solutions4mobiles.com/apis";
13+
const SMS = "sms";
14+
15+
16+
public function __construct(string $username = null, string $password = null){
17+
18+
if (!$username || !$password) {
19+
throw new \Exception("Username and Password are required to create a Client");
20+
}
21+
22+
$this->auth = new Auth($username,$password,AsynchClient::API_ENDPOINT);
23+
if(!$this->auth->authenticate()){
24+
throw new \Exception("Authentication failed");
25+
}
26+
27+
}
28+
29+
public function broadcast(array $args): array{
30+
31+
if (!$args) {
32+
throw new \Exception("Message arguments are required to broadcast a message");
33+
}
34+
35+
return Message::broadcast($this->auth, $args);
36+
37+
}
38+
39+
public function getBalance(): float{
40+
41+
return Util::getBalance($this->auth);
42+
43+
}
44+
45+
public function getPricing(string $service=Client::SMS): array{
46+
47+
return Util::getPricing($this->auth,$service);
48+
49+
}
50+
51+
}
52+
53+
?>

src/MobiWeb/Rest/Authentication.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MobiWeb\Rest;
44

5-
use MobiWeb\Rest\Client as APIClient;
65
use MobiWeb\Http\Client as HttpClient;
76
use MobiWeb\Rest\Error as APIError;
87

@@ -22,18 +21,18 @@ class Authentication {
2221
protected $access_token;
2322
protected $refresh_token;
2423
protected $timestamp;
24+
protected $endpoint;
2525

2626

27-
public function __construct(string $username = null, string $password = null){
27+
public function __construct(string $username = null, string $password = null, string $endpoint){
2828

2929
if (!$username || !$password) {
3030
throw new \Exception("Username and Password are required to authenticate");
3131
}
3232

3333
$this->username=$username;
3434
$this->password=$password;
35-
$this->httpClient=new HttpClient();
36-
35+
$this->endpoint=$endpoint;
3736
}
3837

3938
public function authenticate() :bool{
@@ -44,7 +43,7 @@ public function authenticate() :bool{
4443
$body->username = $this->username;
4544
$body->password = $this->password;
4645
$body->type = Authentication::TYPE_ACCESS;
47-
$executedRequest=$http->request(APIClient::API_ENDPOINT . Authentication::AUTH_ENDPOINT, Authentication::AUTH_METHOD, $headers, $body);
46+
$executedRequest=$http->request($this->endpoint . Authentication::AUTH_ENDPOINT, Authentication::AUTH_METHOD, $headers, $body);
4847

4948
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
5049
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
@@ -71,7 +70,7 @@ public function refresh() :bool{
7170
$body = new \stdClass();
7271
$body->refresh_token = $this->refresh_token;
7372
$body->type = Authentication::TYPE_REFRESH;
74-
$executedRequest=$http->request(APIClient::API_ENDPOINT . Authentication::AUTH_ENDPOINT, Authentication::AUTH_METHOD, $headers, $body);
73+
$executedRequest=$http->request($this->endpoint . Authentication::AUTH_ENDPOINT, Authentication::AUTH_METHOD, $headers, $body);
7574

7675
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
7776
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
@@ -96,6 +95,12 @@ public function getAccessToken(): string{
9695

9796
}
9897

98+
public function getEndPoint(): string{
99+
100+
return $this->endpoint;
101+
102+
}
103+
99104
public function isAuthenticated() :bool{
100105

101106
$timestamp = new \DateTime();

src/MobiWeb/Rest/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(string $username = null, string $password = null){
2323
throw new \Exception("Username and Password are required to create a Client");
2424
}
2525

26-
$this->auth = new Auth($username,$password);
26+
$this->auth = new Auth($username,$password,Client::API_ENDPOINT);
2727
if(!$this->auth->authenticate()){
2828
throw new \Exception("Authentication failed");
2929
}

src/MobiWeb/Rest/Message.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static function broadcast(Auth $auth = null, array $args): array{
2525
return false;
2626
}
2727

28+
$endpoint = $auth->getEndPoint();
29+
2830
$http = new HttpClient();
2931
$headers = array();
3032
$headers["Authorization"] = "Bearer " . $access_token;
@@ -45,7 +47,7 @@ public static function broadcast(Auth $auth = null, array $args): array{
4547
$body[] = $obj;
4648
}
4749

48-
$executedRequest=$http->request(APIClient::API_ENDPOINT . Message::SEND_ENDPOINT, Message::SEND_METHOD, $headers, $body);
50+
$executedRequest=$http->request($endpoint . Message::SEND_ENDPOINT, Message::SEND_METHOD, $headers, $body);
4951

5052
$errors = array();
5153
$responseElements=$executedRequest->response->body->payload;

src/MobiWeb/Rest/Utility.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace MobiWeb\Rest;
44

55
use MobiWeb\Rest\Authentication as Auth;
6-
use MobiWeb\Rest\Client as APIClient;
76
use MobiWeb\Http\Client as HttpClient;
87
use MobiWeb\Rest\Error as APIError;
98

@@ -16,6 +15,9 @@ class Utility {
1615
const PRICING_OTP_ENDPOINT = "/otp/v3/pricing";
1716
const PRICING_METHOD = "GET";
1817

18+
const HLR = "hlr";
19+
const SMS = "sms";
20+
const OTP = "otp";
1921

2022
public static function getBalance(Auth $auth = null): float{
2123

@@ -29,10 +31,12 @@ public static function getBalance(Auth $auth = null): float{
2931
return false;
3032
}
3133

34+
$endpoint = $auth->getEndPoint();
35+
3236
$http = new HttpClient();
3337
$headers = array();
3438
$headers["Authorization"] = "Bearer " . $access_token;
35-
$executedRequest=$http->request(APIClient::API_ENDPOINT . Utility::BALANCE_ENDPOINT, Utility::BALANCE_METHOD, $headers);
39+
$executedRequest=$http->request($endpoint . Utility::BALANCE_ENDPOINT, Utility::BALANCE_METHOD, $headers);
3640

3741
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
3842
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
@@ -56,23 +60,25 @@ public static function getPricing(Auth $auth = null, string $service): array{
5660
return false;
5761
}
5862

63+
$endpoint = $auth->getEndPoint();
64+
5965
$http = new HttpClient();
6066
$headers = array();
6167
$headers["Authorization"] = "Bearer " . $access_token;
6268

6369
switch($service){
64-
case APIClient::SMS:
70+
case Utility::SMS:
6571
$pricing_endpoint = Utility::PRICING_SMS_ENDPOINT;
6672
break;
67-
case APIClient::HLR:
73+
case Utility::HLR:
6874
$pricing_endpoint = Utility::PRICING_HLR_ENDPOINT;
6975
break;
70-
case APIClient::OTP:
76+
case Utility::OTP:
7177
$pricing_endpoint = Utility::PRICING_OTP_ENDPOINT;
7278
break;
7379
}
7480

75-
$executedRequest=$http->request(APIClient::API_ENDPOINT . $pricing_endpoint, Utility::PRICING_METHOD, $headers);
81+
$executedRequest=$http->request($endpoint . $pricing_endpoint, Utility::PRICING_METHOD, $headers);
7682

7783
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
7884
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
@@ -87,11 +93,11 @@ public static function getPricing(Auth $auth = null, string $service): array{
8793
$arr_pricing=array();
8894

8995
switch($service){
90-
case APIClient::SMS:
91-
case APIClient::OTP:
96+
case Utility::SMS:
97+
case Utility::OTP:
9298
foreach ($pricing as $key => $value)$arr_pricing[$value->id] = array("countryname" => $value->operatorname, "operator" => $value->operatorname, "mcc" => $value->mcc, "mnc" => $value->mnc, "price" => $value->price, "currency" => $currency);
9399
break;
94-
case APIClient::HLR:
100+
case Utility::HLR:
95101
foreach ($pricing as $key => $value)$arr_pricing[$value->id] = array("countryname" => $value->countryname, "countrycode" => $value->countrycode, "countryiso" => $value->countryiso, "price" => $value->price, "currency" => $currency);
96102
break;
97103
}

tests/asynch_get_balance.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\AsynchClient as AsynchClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new AsynchClient($username, $password);
12+
13+
//Get account balance and print it
14+
echo $client->getBalance();
15+
16+
?>

tests/asynch_get_pricing.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\AsynchClient as AsynchClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new AsynchClient($username, $password);
12+
13+
//Get account SMS pricing and print it
14+
print_r($client->getPricing(AsynchClient::SMS));
15+
16+
?>

tests/asynch_send_full_example.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\AsynchClient as AsynchClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new AsynchClient($username, $password);
12+
13+
$message = $client->broadcast(
14+
[[
15+
"from" => "HelloWorld", //The sender displayed upon the SMS arrival. Can be composed of 2-11 alphanumeric characters (A-z,0-9, ,-,.) or 14 numeric characters (0-9). Special characters are not allowed.
16+
"to" => ["44xxxxxxxxxx"], //The full international number(s) of the recipient(s) in international E.164 format https://en.wikipedia.org/wiki/E.164.
17+
"message" => "Hello from MobiWeb!", //The text of the SMS message. If all characters in the message belong to the 3GPP GSM 7-bit GSM 03.38 ASCII character table https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_/_GSM_03.38, you can send up to 160 characters in a single SMS. You can send longer messages automatically by setting message parameter with your preffered text. In long SMS (multi-part SMS), each SMS part can be up to 153 characters. Each part costs as 1 SMS. If one or more characters in the message belong to the 16-bit Unicode / UCS-2 character table https://en.wikipedia.org/wiki/UTF-16, because of the increased memory requirement for each character, you can send up to 70 characters in a single SMS. In long SMS (multi-part SMS), each SMS part can be up to 67 characters. Each part costs as 1 SMS.
18+
19+
"options" => [
20+
"receive_dlr" => "1", //Set this parameter to ‘1’ for requesting delivery report for this SMS. Refer to receive Delivery Reports section for more information . https://api.solutions4mobiles.com/sms-api.html#receive_delivery_reports
21+
"message_type" => "sms", //The type of the SMS message.
22+
"track_url" => "0", //Set this parameter to ‘1’ to shorten and track URL link for this SMS. Refer to receive URL tracking and conversion section for more information . https://api.solutions4mobiles.com/sms-api.html#receive_url_ctr
23+
"reference_code" => "ABCD1234", //Set this parameter to your preferred reference id / custom data for this submission. Length can be up to 50 characters.
24+
"schedule_date" => "", //Set this parameter in format "yyyy-mm-dd hh:mm:ss" UTC +0 to schedule your sending to a future datetime. Must be at least 20 minutes from now.
25+
"expire_date" => "" //Set this parameter in format "yyyy-mm-dd hh:mm:ss" UTC +0 to force expiration of your SMS to a future datetime. Must be at least 30 minutes from now and schedule_date.
26+
]
27+
28+
]]
29+
);
30+
31+
//Print message
32+
print_r($message);
33+
34+
?>

tests/asynch_send_single_message.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\AsynchClient as AsynchClient;
6+
7+
8+
//Your account username and password
9+
$username = "";
10+
$password = "";
11+
12+
$client = new AsynchClient($username, $password);
13+
14+
//Submit message
15+
$message = $client->broadcast(
16+
[[
17+
"from" => "HelloWorld", //The sender displayed upon the SMS arrival. Can be composed of 2-11 alphanumeric characters (A-z,0-9, ,-,.) or 14 numeric characters (0-9). Special characters are not allowed.
18+
"to" => ["44xxxxxxxxxx"], //The full international number(s) of the recipient(s) in international E.164 format https://en.wikipedia.org/wiki/E.164.
19+
"message" => "Hello from MobiWeb!" //The text of the SMS message. If all characters in the message belong to the 3GPP GSM 7-bit GSM 03.38 ASCII character table https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_/_GSM_03.38, you can send up to 160 characters in a single SMS. You can send longer messages automatically by setting message parameter with your preffered text. In long SMS (multi-part SMS), each SMS part can be up to 153 characters. Each part costs as 1 SMS. If one or more characters in the message belong to the 16-bit Unicode / UCS-2 character table https://en.wikipedia.org/wiki/UTF-16, because of the increased memory requirement for each character, you can send up to 70 characters in a single SMS. In long SMS (multi-part SMS), each SMS part can be up to 67 characters. Each part costs as 1 SMS.
20+
21+
]]
22+
);
23+
24+
//Print message
25+
print_r($message);
26+
27+
?>

0 commit comments

Comments
 (0)