Skip to content

Instantly share code, notes, and snippets.

@weezqyd
Created May 11, 2020 14:31
Show Gist options
  • Save weezqyd/aa719815580e98149db365186abf3afc to your computer and use it in GitHub Desktop.
Save weezqyd/aa719815580e98149db365186abf3afc to your computer and use it in GitHub Desktop.
Emalify Send SMS
<?php
class SendSMS
{
/**
*
* Send SMS Class
*
* @author Albert Leitato <albert.leitato@roamtech.com>
*
*/
const BASE_URL = 'https://api.emalify.com';
/**
* The current API version
* @var string
*/
protected $apiVersion = 'v1';
/**
* Cache file location
*
* Stores the token to prevent multiple calls to token endpoint
*/
private $cacheFile = '.token.json';
/**
* Client ID
*
* Emalify Client id
* @var string
*/
private $clientId;
/**
* Client Secret
* @var string
*/
private $clientSecret;
/**
* ProjectId
* Project id from emalify dev portal
*/
private $projectId;
/**
* Initiate class with credentials and projectId
*/
public function __construct($clientId, $clientSecret, $projectId)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->projectId = $projectId;
}
/**
* Get the API URL endpoint
*
* @param string $uri URI to append
* @return string
*/
protected function buildEndpoint($uri)
{
return sprintf('/%s/projects/%s/%s', $this->apiVersion, $this->projectId, $uri);
}
private function authenticate()
{
if (file_exists(__DIR__."/{$this->cacheFile}")) {
$contents = file_get_contents(__DIR__."/{$this->cacheFile}");
$token = json_decode($contents, true);
if (isset($token['ttl']) && $token['ttl'] > time()) {
return $token['token'];
}
}
$token = $this->getAuthToken();
return $token['access_token'];
}
private function getAuthToken()
{
$credentials = [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'client_credentials'
];
$token = $this->makeRequest('POST', "/{$this->apiVersion}/oauth/token", $credentials, false);
$this->saveCredentials($token);
return $token;
}
private function saveCredentials($token)
{
$content = [
'token' => $token['access_token'],
'ttl' => time() + ($token['expires_in'] - 120)
];
return file_put_contents(__DIR__."/{$this->cacheFile}", json_encode($content));
}
private function makeRequest($method, $path, $body = [], $authorize = true)
{
$url = static::BASE_URL.$path;
$headers = [
"Accept: application/json",
"Content-Type: application/json"
];
if ($authorize) {
$headers[] = "Authorization: Bearer ".$this->authenticate();
}
$curl = curl_init();
$curlOptions = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
];
if ($method === 'POST' || $method === 'PATCH' || $method === 'PUT') {
$requestBody = json_encode($body);
$curlOptions[CURLOPT_POSTFIELDS] = $requestBody;
}
curl_setopt_array($curl, $curlOptions);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if (!in_array($httpCode, [200, 201, 202, 204])) {
throw new \Exception('Error while connecting to the API endpoind '.$url.'Body ==> '.$requestBody . 'Response ==> '. $response. 'Headers ==>'. json_encode($headers));
}
return json_decode($response, true);
}
/**
* Send message
*
* This metthod accepts an Array of recipients and optional parameters like
*
* $options = [
* 'messageId' => 'unique-message-id',
* 'callback' => 'http://example.com/dlr/callback',
* 'from' => 'SenderId'
* ]
* @see https://docs.emalify.com/?version=latest#e31e965b-2e80-4ab0-8191-fdd78f972aa9
*/
public function sendMessage($message, array $recipients, array $options = [])
{
$endpoint = $this->buildEndpoint('sms/simple/send');
$body = array_merge($options, [
'message' => $message,
'to' => $recipients
]);
return $this->makeRequest('POST', $endpoint, $body);
}
public function sendBulkMessages($payload, array $options = [])
{
$messages = $this->validateBulkPayLoad($payload);
$params = array_merge([
'messages' => $messages,
], $options);
$endpoint = $this->buildEndpoint('sms/bulk');
return $this->makeRequest('POST', $endpoint, $params);
}
/**
* @param $messageId
* @return mixed
*/
public function getDeliveryReport($messageId)
{
$endpoint = $this->buildEndpoint('sms/delivery-reports?messageId='.$messageId);
return $this->makeRequest('GET', $endpoint, []);
}
/**
* @param array $payload
* @return array
*/
private function validateBulkPayLoad(array $payload)
{
return array_filter($payload, function ($message) {
return is_array($message) && key_exists('message', $message) && key_exists('recipient', $message);
});
}
}
<?php
require 'SendSMS.php';
// Get these credentials from the developer portal https://developer.emalify.com
$clientId = '';
$clientSecret = '';
$projectId = '';
$client = new SendSMS($clientId, $clientSecret, $projectId);
$options = [
'from' => 'Emalify',
'messageId' => 'yourMessageId',
'callback' => 'http://example.com/dlr/callback'
];
$client->sendMessage('Hello world', ['2547xxxxxxx'], $options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment