Last active
April 5, 2018 09:47
-
-
Save boywijnmaalen/b07a4c5b18f916aa0fed9d5b4f574a8b to your computer and use it in GitHub Desktop.
Send one or more messages via CM Telecom' Messaging Gateway. For the latest version of the docs please go to; https://docs.cmtelecom.com/bulk-sms #CodeExamples
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 | |
namespace CMTelecom\Messaging; | |
/** | |
* Code Examples | |
*/ | |
// initiate | |
$sendMessage = new CMSendMessage('<INSERT PRODUCT TOKEN HERE>', 'SenderID'); | |
// send a single message, returns a string (JSON) | |
$response = $sendMessage->sendASingleMessage('00447911123456', 'Test message 1'); | |
// send multiple messages, returns a string (JSON) | |
$response = $sendMessage->sendMultipleMessage( | |
[ | |
['Test message 2' => ['00447911123456', '+00447965432111']] | |
] | |
); | |
/** | |
* Class ConfigException | |
* | |
* @package CMTelecom\Messaging | |
*/ | |
class ConfigException extends \Exception | |
{ | |
} | |
/** | |
* Send one or multiple messages via CM Telecom' Messaging Gateway | |
* | |
* Class CMSendMessage | |
* | |
* @package CMTelecom\Messaging | |
*/ | |
class CMSendMessage | |
{ | |
/** | |
* @var string productToken | |
*/ | |
private $productToken; | |
/** | |
* CMSendMessage constructor. | |
* | |
* @param $productToken productToken which is supplied by CM Telecom - eg. 00000000-0000-0000-0000-000000000000 | |
* @param $senderID senderID, who sent the message - The maximum length is 11 alphanumerical characters or 16 digits. | |
*/ | |
public function __construct($productToken, $senderID) | |
{ | |
$this->productToken = $productToken; | |
$this->senderID = $senderID; | |
} | |
/** | |
* Send a single message to single recipient | |
* | |
* @param $recipient - This is the destination mobile number. Restrictions: this value should be in international format. A single mobile number per request. Example: '00447911123456' | |
* @param $message - This is the message text. Restrictions: the maximum length is 160 characters (concatenated messages are possible) | |
* | |
* @return string (JSON) | |
* @throws ConfigException | |
*/ | |
public function sendASingleMessage($recipient, $message) | |
{ | |
$requestBody = $this->getRequestBody( | |
[ | |
[$message => [$recipient]] | |
] | |
); | |
return $this->sendRequest($requestBody); | |
} | |
/** | |
* @param array $messages | |
* | |
* @return string (JSON) | |
* @throws ConfigException | |
*/ | |
public function sendMultipleMessage(array $messages) | |
{ | |
return $this->sendRequest($this->getRequestBody($messages)); | |
} | |
/** | |
* Get the Request body | |
* | |
* @param array $messages | |
* | |
* @return \stdClass | |
* @throws ConfigException | |
*/ | |
private function getRequestBody(array $messages) | |
{ | |
$body = new \stdClass(); | |
$body->messages = new \stdClass(); | |
// set product token | |
$body->messages->authentication = new \stdClass(); | |
$body->messages->authentication->producttoken = $this->productToken; | |
$body->messages->msg = []; | |
if (!is_array($messages) || count($messages) === 0) { | |
throw new ConfigException('At least one message must be send when sending a request'); | |
} | |
// loop through all the messages | |
foreach ($messages as $message) { | |
if (!is_array($message) || count($message) === 0) { | |
throw new ConfigException('Message details cannot be empty. Please specify a message body and at least one recipient'); | |
} | |
// loop through message details and 1,n recipients | |
foreach ($message as $messageContent => $recipients) { | |
$msg = new \stdClass(); | |
// set sender ID | |
$msg->from = $this->senderID; | |
// set body | |
$msg->body = new \stdClass(); | |
$msg->body->content = $messageContent; | |
if (!is_array($recipients) || count($recipients) === 0) { | |
throw new ConfigException('The given recipients value is either not an array or it is empty'); | |
} | |
// set recipients | |
$msg->to = []; | |
// loop through 1,n recipients | |
foreach ($recipients as $recipient) { | |
$to = new \stdClass(); | |
$to->number = $recipient; | |
$msg->to[] = $to; | |
} | |
$body->messages->msg[] = $msg; | |
} | |
} | |
return $body; | |
} | |
/** | |
* Send the Request to CM Telecom' Messaging Gateway | |
* | |
* @param \stdClass $requestBody | |
* | |
* @return string (JSON) | |
*/ | |
private function sendRequest(\stdClass $requestBody) | |
{ | |
// cURL v7.18.1+ and OpenSSL 0.9.8+ are required | |
$ch = curl_init(); | |
curl_setopt_array( | |
$ch, | |
[ | |
CURLOPT_URL => 'https://gw.cmtelecom.com/v1.0/message', | |
CURLOPT_HTTPHEADER => array('Content-Type: application/json'), | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => json_encode($requestBody), | |
CURLOPT_RETURNTRANSFER => true | |
] | |
); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment