File: D:/HostingSpaces/SBogers10/komma-mediadesign.nl/wwwroot/ledubsms/classes/class.molliesmsbatch.php
<?php
/**
* MollieSmsBatch
*
* This class is used by Mollie customers to communicate with Mollie's SMS XML API and submit large batches of SMS messages
*
* @author Rick Wong <info@mollie.nl> Mar 29, 2011
* @package Mollie
* @copyright Copyright (C) Mollie B.V.
* @link http://www.mollie.nl/ Mollie homepage
* @link http://www.mollie.nl/support/documentatie/sms-diensten/sms/xml/ Mollie's SMS XML API documentation
*/
class MollieSmsBatch
{
/**
* @var string
*/
protected $_username;
/**
* @var string
*/
protected $_password;
/**
* @var bool
*/
protected $_use_md5;
/**
* @var string
*/
protected $_api_url;
/**
* Messages in the current batch
* @var array
*/
protected $_messages;
/**
* Stores the last known API result
* @var object
*/
protected $_last_result;
/**
* Constructor
*
* @param string $username Mollie username
* @param string $password Mollie password or the MD5 hash of it
* @param bool $use_md5 (optional) Must be TRUE if the password is an MD5 hash
* @param string $api_url (optional) Can be used to specify an alternative API URL
*/
public function __construct ($username, $password, $use_md5 = FALSE, $api_url = 'http://www.mollie.nl/xml/sms.xml')
{
$this->_username = (string) $username;
$this->_password = (string) $password;
$this->_use_md5 = (bool) $use_md5;
$this->_api_url = (string) $api_url;
$this->_messages = array();
}
/**
* Add a message to the current batch
*
* @param string $originator Originator of the SMS message
* @param string|array $recipients Recipients of the SMS message
* @param string $body Message text/body
* @param int $gateway (optional) Gateway number
* @param string $deliverydate (optional) Can be used to deliver the message later, format: YYYYMMDDHHMMSS
* @param string $reference (optional) Reference to be passed back in a Delivery Report
* @param string $type (optional) Message type, options: normal/wappush/vcard/flash/binary/long
* @param string $udh (optional) User Data Header (only when type=binary)
* @return bool
*/
public function addMessage ($originator, $recipients, $body, $gateway = NULL, $deliverydate = NULL, $reference = NULL, $type = NULL, $udh = NULL)
{
if (empty($originator) || empty($recipients) || empty($body))
{
return FALSE;
}
if (is_array($recipients)) {
$recipients = join(',', $recipients);
}
array_push($this->_messages, array(
'originator' => (string) $originator,
'recipients' => (string) $recipients,
'body' => (string) $body,
'gateway' => (int) $gateway,
'deliverydate' => (string) $deliverydate,
'reference' => (string) $reference,
'type' => (string) $type,
'udh' => (string) $udh,
));
return TRUE;
}
/**
* Clears the current batch
*
*/
public function clearMessages ()
{
$this->_messages = array();
}
/**
* Generates an XML document of the current batch and posts it to the SMS XML API
*
* @return bool
*/
public function submit ()
{
$this->_last_result = NULL;
if (!count($this->_messages)) {
return FALSE;
}
$password_tag = $this->_use_md5 ? 'md5_password' : 'password';
$xml = new SimpleXMLElement2('<text_message></text_message>');
$xml->addChild('username', $this->_username);
$xml->addChild($password_tag, $this->_password);
$messages_xml = $xml->addChild('messages');
foreach ($this->_messages as $msg)
{
$msg = (object) $msg;
$msg_xml = $messages_xml->addChild('message');
if (!empty($msg->reference)) {
$msg_xml->addAttribute('reference', $msg->reference);
}
$recipients_xml = $msg_xml->addChild('recipients');
$recipients_xml->addChild('recipient', $msg->recipients);
$msg_xml->addChild('originator', $msg->originator);
$msg_xml->addChildWithCDATA('body', $msg->body);
if (!empty($msg->gateway)) {
$msg_xml->addChild('gateway', $msg->gateway);
}
if (!empty($msg->deliverydate)) {
$msg_xml->addChild('deliverydate', $msg->deliverydate);
}
if (!empty($msg->type)) {
$msg_xml->addChild('type', $msg->type);
}
if (!empty($msg->udh)) {
$msg_xml->addChild('udh', $msg->udh);
}
}
$response = $this->_postBatch($xml->asXML());
if (!$response) {
return FALSE;
}
$this->_last_result = simplexml_load_string($response);
if (!$this->_last_result) {
return FALSE;
}
if ($this->getErrorCode()) {
return FALSE;
}
return TRUE;
}
/**
* Submits the current batch to Mollie's SMS XML API
*
* @param string @xml_batch XML document with the current batch marked up
* @return bool
*/
public function _postBatch ($xml_batch)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->_api_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_batch);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Returns the result code of the last result, only on failure
*
* @return int
*/
public function getErrorCode ()
{
return (int) $this->_last_result->item->resultcode;
}
/**
* Returns the result message of the last result, only on failure
*
* @return string
*/
public function getErrorMessage ()
{
return (string) $this->_last_result->item->resultmessage;
}
/**
* Returns the number of messages sent in the last result, only on success
*
* @return int
*/
public function getTotalMessages ()
{
return (int) $this->_last_result->total_messages;
}
/**
* Returns the number of recipients sent to in the last result, only on success
*
* @return int
*/
public function getTotalRecipients ()
{
return (int) $this->_last_result->total_recipients;
}
/**
* Returns the failures that were part of a successful last result, only on success
*
* @return array
*/
public function getFailures ()
{
return $this->_last_result->failures;
}
}
/**
* SimpleXMLElement2
*
* This class extends PHP5's SimpleXMLElement class and adds a method for adding CDATA-children to an XML document
*
* @since v1.1
* @author Rick Wong <info@mollie.nl> Apr 1, 2011
* @package Mollie
* @copyright Copyright (C) Mollie B.V.
* @link http://www.mollie.nl/ Mollie homepage
* @link http://www.mollie.nl/support/documentatie/sms-diensten/sms/xml/ Mollie's SMS XML API documentation
*/
class SimpleXMLElement2 extends SimpleXMLElement
{
public function addChildWithCDATA ($name, $value)
{
$dom = dom_import_simplexml($this->addChild($name));
$cdata = $dom->ownerDocument->createCDATASection($value);
$dom->appendChild($cdata);
}
}