Last active
October 20, 2022 05:38
-
-
Save AminulBD/60b7e991049e31820c5786737c0734e3 to your computer and use it in GitHub Desktop.
Nawab SMS PHP Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NawabSMS | |
{ | |
const ERRORS = [ | |
'1' => 'Request has been failed.', | |
'101' => 'Internal server error.', | |
'108' => 'Wrong password or Not provided.', | |
'109' => 'User is not found or deleted.', | |
'114' => 'Content is not provided.', | |
'5400' => 'Source IP is not allowed.', | |
]; | |
/** | |
* The credentials. | |
* | |
* @var array | |
*/ | |
private array $credential; | |
/** | |
* The API endpoint for the gateway. | |
* | |
* @var string | |
*/ | |
private string $endpoint; | |
/** | |
* Phone | |
* | |
* @var string|null | |
*/ | |
private ?string $recipient; | |
/** | |
* Message. | |
* | |
* @var string|null | |
*/ | |
private ?string $message; | |
/** | |
* NawabSMS constructor. | |
*/ | |
public function __construct($credential = [], $endpoint = null) | |
{ | |
$this->endpoint = $endpoint ?? 'http://api.nawabsms.com/sendtext'; | |
$this->credential = $credential; | |
} | |
/** | |
* Phone numbers where to send. | |
* | |
* @param string $recipient | |
* | |
* @return self | |
*/ | |
public function to(string $recipient): self | |
{ | |
$this->recipient = $recipient; | |
return $this; | |
} | |
/** | |
* Get receptions. | |
* | |
* @return string | |
*/ | |
protected function getRecipient(): string | |
{ | |
return $this->recipient; | |
} | |
/** | |
* The text to send. | |
* | |
* @param string $message | |
* | |
* @return self | |
*/ | |
public function message(string $message): self | |
{ | |
$this->message = $message; | |
return $this; | |
} | |
/** | |
* Get Messages. | |
* | |
* @return string | |
*/ | |
protected function getMessage(): string | |
{ | |
return $this->message; | |
} | |
/** | |
* Send SMS. | |
*/ | |
public function send(): array | |
{ | |
$params = http_build_query($this->prepareParams()); | |
$url = "{$this->endpoint}?{$params}"; | |
$response = file_get_contents($url); | |
$data = json_decode($response, true); | |
$error = self::ERRORS[$data['Status']] ?? null; | |
if ($error !== null) { | |
return [ | |
'status' => 'error', | |
'message' => $error, | |
]; | |
} | |
return [ | |
'status' => 'success', | |
'message' => 'Your message has been sent.', | |
]; | |
} | |
/** | |
* Prepare parameters. | |
* | |
* @return array | |
*/ | |
private function prepareParams(): array | |
{ | |
$params = [ | |
'toUser' => $this->getRecipient(), | |
'messageContent' => $this->getMessage(), | |
]; | |
return array_merge($this->credential, $params); | |
} | |
} | |
$nawabSMS = new NawabSMS([ | |
'apikey' => '', | |
'secretkey' => '', | |
'callerID' => '', // Username, MaskID, or Phone Number | |
]); | |
$nawabSMS->to('01xxxxxxxxx')->message('Hi, this is test')->send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment