Skip to content

Commit 4ab6bd5

Browse files
committed
2.0.1
* Removed return value type declaration for method print() of class MobiWeb\Rest\Error. * Corrected error handling in class MobiWeb\Rest\Message * Corrected autoload.php required path in tests. * Implemented HLR API functionality (Classes and methods). * Added HLR API examples in the README.md * Updated all API examples with namespace syntax
1 parent 7207244 commit 4ab6bd5

18 files changed

+213
-36
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 MobiWeb
3+
Copyright (c) 2023 MobiWeb
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
![GitHub top language](https://img.shields.io/github/languages/top/mobiweb/mobiweb-php)
88
![GitHub](https://img.shields.io/github/license/mobiweb/mobiweb-php)
99

10-
A PHP library for interfacing with MobiWeb RESTful SMS API.
10+
A PHP library for interfacing with MobiWeb RESTful SMS and HLR API.
1111

1212
## Documentation
1313

@@ -28,22 +28,24 @@ This library supports the following PHP implementations:
2828
## Installation
2929

3030
* Download Source
31-
* Composer require [`mobiweb/sdk`](https://packagist.org/packages/mobiweb/sdk)
31+
* composer require [`mobiweb/sdk`](https://packagist.org/packages/mobiweb/sdk)
3232

3333
```
3434
composer require mobiweb/sdk
3535
```
3636

3737
## API Account
3838

39-
With MobiWeb SMS API you can send SMS messages to 7+ billion subscribers of 1000+ Mobile Operators in 200+ countries.
39+
* With MobiWeb SMS API you can send SMS messages to 7+ billion subscribers of 1000+ Mobile Operators in 200+ countries.
40+
* With MobiWeb HLR API you can maximize the performance of your campaigns and your business communication. Validate mobile number databases, remove invalid entries and receive correct portability information, identifying the operators that the mobile numbers belong to.
4041

41-
The SMS API is based on REST. It uses built-in HTTP authentication and HTTP status codes. All data exchange is done in JSON format.
42+
The APIs are based on REST with built-in HTTP authentication and HTTP status codes. All data exchange is done in JSON format.
4243

43-
To test the SMS API, you will need a valid API account. If you don't have one yet, [click here][apiaccount] to register for a FREE account.
44+
To test the APIs, you will need a valid API account. If you don't have one yet, [click here][apiaccount] to register for a FREE account.
4445

4546
## Examples
4647

48+
### SMS API
4749
### Send a single SMS
4850

4951
```php
@@ -231,7 +233,7 @@ print_r($message);
231233
$username = "";
232234
$password = "";
233235

234-
$client = new APIClient($username, $password);
236+
$client = new MobiWeb\Rest\Client($username, $password);
235237

236238
//Get account balance and print it
237239
echo $client->getBalance();
@@ -248,10 +250,10 @@ echo $client->getBalance();
248250
$username = "";
249251
$password = "";
250252

251-
$client = new APIClient($username, $password);
253+
$client = new MobiWeb\Rest\Client($username, $password);
252254

253255
//Get account pricing and print it
254-
print_r($client->getPricing());
256+
print_r($client->getPricing(MobiWeb\Rest\Client::SMS));
255257

256258
?>
257259
```
@@ -308,6 +310,63 @@ header('HTTP/1.1 200 OK', true, 200);
308310
?>
309311
```
310312

313+
### HLR API
314+
### Lookup a mobile number
315+
316+
```php
317+
<?
318+
319+
//Your account username and password
320+
$username = "";
321+
$password = "";
322+
323+
$client = new MobiWeb\Rest\Client($username, $password);
324+
325+
//HLR lookup for a mobile number
326+
$lookup = $client->lookup(
327+
"44xxxxxxxxxx" //The mobile number in international E.164 format.
328+
);
329+
330+
//Print the HLR lookup result
331+
print_r($lookup);
332+
333+
?>
334+
```
335+
336+
### Get account balance
337+
338+
```php
339+
<?php
340+
341+
//Your account username and password
342+
$username = "";
343+
$password = "";
344+
345+
$client = new MobiWeb\Rest\Client($username, $password);
346+
347+
//Get account balance and print it
348+
echo $client->getBalance();
349+
350+
?>
351+
```
352+
353+
### Get account pricing
354+
355+
```php
356+
<?php
357+
358+
//Your account username and password
359+
$username = "";
360+
$password = "";
361+
362+
$client = new MobiWeb\Rest\Client($username, $password);
363+
364+
//Get account HLR pricing and print it
365+
print_r($client->getPricing(MobiWeb\Rest\Client::HLR));
366+
367+
?>
368+
```
369+
311370
## Getting help
312371

313372
If you need help installing or using the library, please [contact us][MobiWebSupportCenter].

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "mobiweb/sdk",
3-
"description": "A PHP library for communicating with the MobiWeb SMS REST API.",
3+
"description": "2.0.1 PHP library for communicating with the MobiWeb REST APIs.",
44
"minimum-stability": "stable",
55
"keywords": [
6-
"sms", "api", "mobiweb", "php", "messaging"
6+
"sms", "api", "mobiweb", "php", "messaging", "hlr", "hlr lookup", "mobile lookup", "mobile number lookup"
77
],
88
"type": "library",
99
"license": "MIT",
10-
"homepage": "https://api.solutions4mobiles.com/sms-api.html",
10+
"homepage": "https://api.solutions4mobiles.com/index.html",
1111
"authors": [{
1212
"name": "MobiWeb Tech Team",
1313
"email": "sales@solutions4mobiles.com",

src/MobiWeb/Rest/Client.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Client {
1010

1111
protected $auth;
1212
const API_ENDPOINT = "https://sms.solutions4mobiles.com/apis";
13+
const HLR = "hlr";
14+
const SMS = "sms";
1315

1416

1517
public function __construct(string $username = null, string $password = null){
@@ -35,15 +37,25 @@ public function broadcast(array $args): array{
3537

3638
}
3739

40+
public function lookup(string $mobile): array{
41+
42+
if (!$mobile) {
43+
throw new \Exception("Mobile number is required to make a HLR Lookup");
44+
}
45+
46+
return HLR::lookup($this->auth, $mobile);
47+
48+
}
49+
3850
public function getBalance(): float{
3951

4052
return Util::getBalance($this->auth);
4153

4254
}
4355

44-
public function getPricing(): array{
56+
public function getPricing(string $service=Client::SMS): array{
4557

46-
return Util::getPricing($this->auth);
58+
return Util::getPricing($this->auth,$service);
4759

4860
}
4961

src/MobiWeb/Rest/Error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(string $status_code = null, string $status_message =
2121

2222
}
2323

24-
public function print(): bool{
24+
public function print(){
2525

2626
echo "Error - HTTP: " . $this->status_code . " " . $this->status_message . " - API Error: " . print_r($this->errors, 1);
2727
return true;

src/MobiWeb/Rest/HLR.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MobiWeb\Rest;
4+
5+
use MobiWeb\Rest\Authentication as Auth;
6+
use MobiWeb\Rest\Client as APIClient;
7+
use MobiWeb\Http\Client as HttpClient;
8+
use MobiWeb\Rest\Error as APIError;
9+
10+
class HLR {
11+
12+
const HLR_ENDPOINT = "/hlr/v2/";
13+
const HLR_METHOD = "GET";
14+
15+
16+
public static function lookup(Auth $auth = null, string $mobile): array{
17+
18+
if (!$auth) {
19+
throw new \Exception("Cannot query mobile number without authentication");
20+
}
21+
22+
$access_token = $auth->getAccessToken();
23+
if(!$access_token){
24+
throw new \Exception("Cannot retrieve Access Token");
25+
return false;
26+
}
27+
28+
$http = new HttpClient();
29+
$headers = array();
30+
$headers["Authorization"] = "Bearer " . $access_token;
31+
$body="";
32+
33+
$executedRequest=$http->request(APIClient::API_ENDPOINT . HLR::HLR_ENDPOINT . $mobile, HLR::HLR_METHOD, $headers, $body);
34+
35+
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
36+
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->payload->error);
37+
throw new \Exception($apiError->print());
38+
return false;
39+
}
40+
41+
return array($executedRequest->response->body->payload);
42+
43+
}
44+
45+
}

src/MobiWeb/Rest/Message.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ public static function broadcast(Auth $auth = null, array $args): array{
4444

4545
$body[] = $obj;
4646
}
47+
4748
$executedRequest=$http->request(APIClient::API_ENDPOINT . Message::SEND_ENDPOINT, Message::SEND_METHOD, $headers, $body);
4849

49-
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
50-
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
51-
throw new \Exception($apiError->print());
52-
return false;
50+
$errors = array();
51+
$responseElements=$executedRequest->response->body->payload;
52+
foreach($responseElements as $responseElement){
53+
if($responseElement->status == "error"){
54+
$errors[] = $responseElement->error;
55+
}
56+
}
57+
58+
if(count($errors) > 0){
59+
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $errors);
60+
$apiError->print();
61+
5362
}
5463

5564
return array($executedRequest->response->body->payload);

src/MobiWeb/Rest/Utility.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class Utility {
1111

1212
const BALANCE_ENDPOINT = "/sms/mt/v2/balance";
1313
const BALANCE_METHOD = "GET";
14-
const PRICING_ENDPOINT = "/sms/mt/v2/pricing";
14+
const PRICING_SMS_ENDPOINT = "/sms/mt/v2/pricing";
15+
const PRICING_HLR_ENDPOINT = "/hlr/v2/pricing";
1516
const PRICING_METHOD = "GET";
1617

1718

@@ -42,7 +43,7 @@ public static function getBalance(Auth $auth = null): float{
4243

4344
}
4445

45-
public static function getPricing(Auth $auth = null): array{
46+
public static function getPricing(Auth $auth = null, string $service): array{
4647

4748
if (!$auth) {
4849
throw new \Exception("Cannot get pricing without authentication");
@@ -57,7 +58,17 @@ public static function getPricing(Auth $auth = null): array{
5758
$http = new HttpClient();
5859
$headers = array();
5960
$headers["Authorization"] = "Bearer " . $access_token;
60-
$executedRequest=$http->request(APIClient::API_ENDPOINT . Utility::PRICING_ENDPOINT, Utility::PRICING_METHOD, $headers);
61+
62+
switch($service){
63+
case APIClient::SMS:
64+
$pricing_endpoint = Utility::PRICING_SMS_ENDPOINT;
65+
break;
66+
case APIClient::HLR:
67+
$pricing_endpoint = Utility::PRICING_HLR_ENDPOINT;
68+
break;
69+
}
70+
71+
$executedRequest=$http->request(APIClient::API_ENDPOINT . $pricing_endpoint, Utility::PRICING_METHOD, $headers);
6172

6273
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
6374
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
@@ -71,10 +82,14 @@ public static function getPricing(Auth $auth = null): array{
7182

7283
$arr_pricing=array();
7384

74-
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);
75-
76-
77-
85+
switch($service){
86+
case APIClient::SMS:
87+
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);
88+
break;
89+
case APIClient::HLR:
90+
foreach ($pricing as $key => $value)$arr_pricing[$value->id] = array("countryname" => $value->countryname, "countrycode" => $value->countrycode, "countryiso" => $value->countryiso, "price" => $value->price, "currency" => $currency);
91+
break;
92+
}
7893
return $arr_pricing;
7994

8095
}

tests/get_balance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php'; // Loads MobiWeb package
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
44

55
use MobiWeb\Rest\Client as APIClient;
66

tests/get_hlr_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\Client as APIClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new APIClient($username, $password);
12+
13+
//Get account HLR pricing and print it
14+
print_r($client->getPricing(APIClient::HLR));
15+
16+
?>

tests/get_pricing.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php'; // Loads MobiWeb package
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
44

55
use MobiWeb\Rest\Client as APIClient;
66

@@ -10,7 +10,7 @@
1010

1111
$client = new APIClient($username, $password);
1212

13-
//Get account pricing and print it
14-
print_r($client->getPricing());
13+
//Get account SMS pricing and print it
14+
print_r($client->getPricing(APIClient::SMS));
1515

1616
?>

tests/query_hlr_mobile.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\Client as APIClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new APIClient($username, $password);
12+
13+
//HLR lookup for a mobile number
14+
$lookup = $client->lookup(
15+
"44xxxxxxxxxx" //The mobile number in international E.164 format.
16+
);
17+
18+
//Print the HLR lookup result
19+
print_r($lookup);
20+
21+
?>

tests/send_full_example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php'; // Loads MobiWeb package
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
44

55
use MobiWeb\Rest\Client as APIClient;
66

0 commit comments

Comments
 (0)