-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from rtraselbd/master
I just updated few providers latest api.
- Loading branch information
Showing
4 changed files
with
184 additions
and
34 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
|
||
namespace Xenon\Multisms\Provider; | ||
|
||
|
||
use Xenon\Multisms\Handler\XenonException; | ||
use Xenon\Multisms\Sender; | ||
|
||
class Smsq extends AbstractProvider | ||
{ | ||
/** | ||
* Smsq constructor. | ||
* @param Sender $sender | ||
*/ | ||
public function __construct(Sender $sender) | ||
{ | ||
$this->senderObject = $sender; | ||
} | ||
|
||
/** | ||
* Send Request To Api and Send Message | ||
*/ | ||
public function sendRequest(): array | ||
{ | ||
$url = "https://api.smsq.global/api/v2/SendSMS"; | ||
$number = $this->formatNumber($this->senderObject->getMobile()); | ||
$text = $this->senderObject->getMessage(); | ||
$config = $this->senderObject->getConfig(); | ||
|
||
$data = [ | ||
'senderId' => $config['sender_id'], | ||
'is_Unicode' => true, | ||
'apiKey' => $config['api_key'], | ||
'clientId' => $config['client_id'], | ||
'mobileNumbers' => $number, | ||
'message' => $text | ||
]; | ||
|
||
$curl = curl_init(); | ||
curl_setopt_array($curl, [ | ||
CURLOPT_URL => $url, | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_CUSTOMREQUEST => "POST", | ||
CURLOPT_POSTFIELDS => json_encode($data), | ||
CURLOPT_HTTPHEADER => [ | ||
"accept: application/json", | ||
"content-type: application/json" | ||
], | ||
]); | ||
$smsResult = curl_exec($curl); | ||
curl_close($curl); | ||
return $this->generateReport($smsResult, $data); | ||
} | ||
|
||
/** | ||
* For mobile number | ||
* @param $mobile | ||
* @return string | ||
*/ | ||
private function formatNumber($mobile): string | ||
{ | ||
if (mb_substr($mobile, 0, 2) == '01') { | ||
$number = $mobile; | ||
} elseif (mb_substr($mobile, 0, 2) == '88') { | ||
$number = str_replace('88', '', $mobile); | ||
} elseif (mb_substr($mobile, 0, 3) == '+88') { | ||
$number = str_replace('+88', '', $mobile); | ||
} | ||
return '88' . $number; | ||
} | ||
|
||
/** | ||
* @throws XenonException | ||
*/ | ||
public function errorException() | ||
{ | ||
if (!is_array($this->senderObject->getConfig())) { | ||
throw new XenonException('Configuration is not provided. Use setConfig() in method chain'); | ||
} | ||
if (!array_key_exists('sender_id', $this->senderObject->getConfig())) { | ||
throw new XenonException('sender_id key is absent in configuration'); | ||
} | ||
if (!array_key_exists('api_key', $this->senderObject->getConfig())) { | ||
throw new XenonException('api_key key is absent in configuration'); | ||
} | ||
|
||
if (!array_key_exists('client_id', $this->senderObject->getConfig())) { | ||
throw new XenonException('client_id key is absent in configuration'); | ||
} | ||
|
||
if (strlen($this->senderObject->getMobile()) > 11 || strlen($this->senderObject->getMobile()) < 11) { | ||
throw new XenonException('Invalid mobile number. It should be 11 digit'); | ||
} | ||
if (empty($this->senderObject->getMessage())) { | ||
throw new XenonException('Message should not be empty'); | ||
} | ||
} | ||
|
||
/** | ||
* @param $result | ||
* @param $data | ||
* @return array | ||
*/ | ||
public function generateReport($result, $data): array | ||
{ | ||
return [ | ||
'status' => 'response', | ||
'response' => $result, | ||
'provider' => self::class, | ||
'send_time' => date('Y-m-d H:i:s'), | ||
'mobile' => $data['MobileNumbers'], | ||
'message' => $data['Message'] | ||
]; | ||
} | ||
} |
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