Skip to content

Instantly share code, notes, and snippets.

@MMTE
Created January 23, 2023 10:58
Show Gist options
  • Save MMTE/cdc7ba4f40a5b06912c84469f6f59d9d to your computer and use it in GitHub Desktop.
Save MMTE/cdc7ba4f40a5b06912c84469f6f59d9d to your computer and use it in GitHub Desktop.
<?php
namespace App\Helpers;
class SMS
{
/**
* Gets API Ultra Fast Send Url.
*
* @return string Indicates the Url
*/
protected function getAPIUltraFastSendUrl()
{
return "api/UltraFastSend";
}
/**
* Gets Api Token Url.
*
* @return string Indicates the Url
*/
protected function getApiTokenUrl()
{
return "api/Token";
}
/**
* Gets config parameters for sending request.
*
* @param string $APIKey API Key
* @param string $SecretKey Secret Key
* @param string $APIURL API URL
*
* @return void
*/
public function __construct($APIKey, $SecretKey, $APIURL)
{
$this->APIKey = $APIKey;
$this->SecretKey = $SecretKey;
$this->APIURL = $APIURL;
}
/**
* Ultra Fast Send Message.
*
* @param data[] $data array structure of message data
*
* @return string Indicates the sent sms result
*/
public function ultraFastSend($data)
{
$token = $this->_getToken($this->APIKey, $this->SecretKey);
if ($token != false) {
$postData = $data;
$url = $this->APIURL . $this->getAPIUltraFastSendUrl();
$UltraFastSend = $this->_execute($postData, $url, $token);
$object = json_decode($UltraFastSend);
$result = false;
if (is_object($object)) {
$result = $object->Message;
} else {
$result = false;
}
} else {
$result = false;
}
return $result;
}
/**
* Gets token key for all web service requests.
*
* @return string Indicates the token key
*/
private function _getToken()
{
$postData = array(
'UserApiKey' => $this->APIKey,
'SecretKey' => $this->SecretKey,
'System' => 'php_rest_v_2_0'
);
$postString = json_encode($postData);
$ch = curl_init($this->APIURL . $this->getApiTokenUrl());
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result);
$resp = false;
$IsSuccessful = '';
$TokenKey = '';
if (is_object($response)) {
$IsSuccessful = $response->IsSuccessful;
if ($IsSuccessful == true) {
$TokenKey = $response->TokenKey;
$resp = $TokenKey;
} else {
$resp = false;
}
}
return $resp;
}
/**
* Executes the main method.
*
* @param postData[] $postData array of json data
* @param string $url url
* @param string $token token string
*
* @return string Indicates the curl execute result
*/
private function _execute($postData, $url, $token)
{
$postString = json_encode($postData);
$ch = curl_init($url);
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'x-sms-ir-secure-token: ' . $token
)
);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment