Skip to content

Commit

Permalink
Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
aimedidierm committed Jul 20, 2023
1 parent c35b047 commit ca9716c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 88 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,43 @@ This is a php library to help developers include sms service, with FDI sms gatew
Install this package as a dependency using [Composer](https://getcomposer.org).

```bash
composer require aimedidierm/fdi-sms
composer require aimedidierm/fdisms
```

## Usage

This is the documantion

```php
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Aimedidierm\FdiSms\SendSms;

$sms = new SendSms;
$sms->SingleSms(
$username = "", // Your API Username provided by FDI
$password = "", // Your API Password provided by FDI
$sender_id = "", // Your User ID provided by FDI
$phone = "", // Receiver phone number
$message = "", // Text message to be send
$ref = "", // Your unique message reference ID
$callBackURL = "" //Optional Delivery Report destination
);
print_r($sms->SingleSms());
$to = ""; // Receiver phone number
$message = ""; // Text message to be send
$senderId = ""; // Your User ID provided by FDI
$ref = ""; // Your unique message reference ID
$callbackUrl = ""; //Optional Delivery Report destination

```
try {
$apiUsername = ""; // Your API Username provided by FDI
$apiPassword = ""; // Your API Password provided by FDI
$smsSender = new SendSms($apiUsername, $apiPassword);

$response = $smsSender->sendSms($to, $message, $senderId, $ref, $callbackUrl);

if ($response['success']) {
return response()->json(['message' => 'SMS sent successfully']);
} else {
return response()->json(['message' => $response['message'], 500]);
}
} catch (\Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}

```

NB: For some people who are not using composer remember to add:

Expand Down
36 changes: 23 additions & 13 deletions examples/send_sms.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?php
use Aimedidierm\FdiSms\SendSms;

require_once __DIR__ . '/../vendor/autoload.php';
public function testing()
{
$to = "";
$message = "";
$senderId = "";
$ref = "";
$callbackUrl = "";

use Aimedidierm\FdiSms\SendSms;
try {
$apiUsername = "";
$apiPassword = "";
$smsSender = new SendSms($apiUsername, $apiPassword);

$response = $smsSender->sendSms($to, $message, $senderId, $ref, $callbackUrl);

$sms = new SendSms();
$sms->SingleSms(
$username = "", // Your API Username provided by FDI
$password = "", // Your API Password provided by FDI
$sender_id = "", // Your User ID provided by FDI
$phone = "", // Receiver phone number
$message = "", // Text message to be sent
$ref = "", // Your unique message reference ID
$callBackURL = "" // Optional Delivery Report destination
);
var_dump($sms);
if ($response['success']) {
return response()->json(['message' => 'SMS sent successfully']);
} else {
return response()->json(['message' => $response['message'], 500]);
}
} catch (\Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}
}
131 changes: 69 additions & 62 deletions src/SendSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,96 @@

namespace Aimedidierm\FdiSms;

use Exception;

class SendSms
class sendSms
{
private function authFDISMS($SMSSECRET, $SMSID)
{
if (empty($SMSSECRET)) {
throw new Exception("SMSSECRET is required.");
}
if (empty($SMSID)) {
throw new Exception("SMSID is required.");
}
private $baseUrl;
private $bearerToken;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://messaging.fdibiz.com/api/v1/auth/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_POST, TRUE);
public function __construct($apiUsername, $apiPassword)
{
$this->baseUrl = 'https://messaging.fdibiz.com/api/v1/';
$this->bearerToken = $this->getBearerToken($apiUsername, $apiPassword);
}

private function getBearerToken($apiUsername, $apiPassword)
{
$ch = curl_init($this->baseUrl . 'auth/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
"api_username" => $SMSID,
"api_password" => $SMSSECRET
'api_username' => $apiUsername,
'api_password' => $apiPassword,
]));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json"
));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);

$response = curl_exec($ch);
curl_close($ch);
$decodedResponse = json_decode($response, true);
return $decodedResponse;
}
if (!$response) {
throw new \Exception('Empty response from the API');
}

public function SingleSms($username, $password, $sender_id, $phone, $message, $ref, $callBackURL)
{
if (empty($username)) {
throw new Exception("Username is required.");
$responseData = json_decode($response, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Invalid JSON response from the API');
}
if (empty($password)) {
throw new Exception("Password is required.");

if ($responseData === null || !isset($responseData['access_token'])) {
throw new \Exception('Failed to get Bearer token');
}
if (empty($phone)) {
throw new Exception("Phone number is required.");

return $responseData['access_token'];
}

public function sendSms($to, $message, $senderId, $ref, $callbackUrl)
{
if (empty($to)) {
throw new \Exception("Receiver hone number is required.");
}
if (empty($message)) {
throw new Exception("Message is required.");
throw new \Exception("Message is required.");
}
if (empty($ref)) {
throw new Exception("Reference is required.");
throw new \Exception("Reference is required.");
}
$data = [
'msisdn' => $to,
'message' => $message,
'sender_id' => $senderId,
'msgRef' => $ref,
'dlr' => $callbackUrl,
];

$ch = curl_init($this->baseUrl . 'mt/single');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->bearerToken,
'Content-Type: application/json',
]);

$auth = $this->authFDISMS($password, $username);
$token = $auth["access_token"];
var_dump($token);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://messaging.fdibiz.com/api/v1/mt/single");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_POST, TRUE);
$response = curl_exec($ch);
curl_close($ch);
if ($response == null) {
throw new \Exception('Empty response from the API');
}

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
"msisdn" => $phone,
"message" => $message,
"msgRef" => $ref,
"dlr" => $callBackURL,
"sender_id" => $sender_id
]));
$responseData = json_decode($response, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization" => "Bearer " . $token
));
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Invalid JSON response from the API');
}

$response = curl_exec($ch);
curl_close($ch);
if ($responseData['success']) {
throw new \Exception($responseData['message']);
}

var_dump($response);
return $responseData;
}
}

0 comments on commit ca9716c

Please sign in to comment.