Integrate API

Integrate API - Header

Friday, August 30, 2013

How to integrate CM.NL SMS API

http://www.cmtelecom.com is one of the largest worldwide SMS provider company who provide two different services named BUSINESS and ELITE. Depending upon your SMS volume choose right package for you. Here is how those two services you will integrate with your own application.

From another view, CM.NL provide two different types of Test Account(may be same for Live)
1. SMPP: http://docs.cm.nl/SMPP.pdf
2. HTTP: http://docs.cm.nl/http_MT.pdf
3. CM Direct: http://docs.cm.nl/Technische-Documentatie-CMDirect.pdf (Detail)

CM Direct:
First of all go to: here and create an account with your email address, company name and phone number. Once you have successfully create your account they will send you a verification email link. after verify your email(they use mobile number verification as well) please log in to system using email address and phone number. every time when you attempt to log in they will send you a verification code to your phone number you have used during registration. After log in, you can see your current credit middle top right corner like Current credit: 74 Messages (Updated to Fri 30 Aug 02:03). Usually cmtelecom provide 100 free SMS credit for test.
Now here is the straight forward and easy PHP coding to integrate CM.NL with your application.
<?php 
class CMSMS 

  function CreateMessage($ProductToken, $Sender, $Recipient, $Tariff, $Body) 
  { 
$XMLSMS = new SimpleXMLElement('<MESSAGES/>'); 
$XMLSMS->addChild('AUTHENTICATION'); 
$XMLSMS->AUTHENTICATION->addChild('PRODUCTTOKEN', $ProductToken); 
$XMLSMS->addChild('TARIFF'); 
$XMLSMS->TARIFF = $Tariff; 
$XMLSMS->addChild('MSG'); 
$XMLSMS->MSG->addChild('FROM'); 
$XMLSMS->MSG->FROM = $Sender; 
$XMLSMS->MSG->addChild('BODY'); 
$XMLSMS->MSG->BODY = $Body; 
$XMLSMS->MSG->addChild('TO'); 
$XMLSMS->MSG->TO = $Recipient; 
return $XMLSMS->asXML(); 
  }
  function SendMessage($URL, $Message) 
  { 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $URL); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'Content-length: ' . strlen($Message))); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $Message); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$return = curl_exec($ch); 
curl_close($ch);
return $return; 
  } 

//Send SMS
$SMS = new CMSMS; 
$ProductToken=$_POST['product_token'];//token you get in email
$Tariff=0; $Sender="123456789"; //your no
$Recipient="123456789"; $Body="Test SMS using CM.NL direct";
$XMLtoSend = $SMS->CreateMessage($ProductToken, $Sender, $Recipient, $Tariff, $Body); 
$return=$SMS->SendMessage('http://gateway.cmdirect.nl/cmdirect/gateway.ashx', $XMLtoSend); 
echo $return;
?>


HTTP:
send-sms-start.php:-
<form method="post" action="send_sms.php"> You can use BUSINESS or ELITE Credential to Send SMS.<br /> Customer ID: <input name="customer_id" type="text" value="1000"><br/> Username: <input name="username" type="text" value="your-user-id"><br/> Password: <input name="password" type="text" value="pass"><br/> <br /> Sender No: <input name="sender_no" type="text" value=""><br/> Recipient No: <input type="text" name="recipient_no" value=""> <br/> SMS Text: <input type="text" name="sms_body"> <input type="submit" value="Send SMS"> </form>

send_sms.php
// Report all PHP errors (see changelog) error_reporting(E_ALL); include("cmSMS.php"); //Create SMS Object $SMS = new CMSMS; $CustomerID=$_POST['customer_id']; $Login=$_POST['username']; $Password=$_POST['password']; $Tariff=0; $SenderName=$_POST['sender_no']; $Body=$_POST['sms_body']; $MSISDN=$_POST['recipient_no']; //Prepare SMS $Reference="member_id=420&Time=".time(); //Any custom Reference/Text. Helpful to identify SMS $XMLtoSend = $SMS->CreateMessage($CustomerID, $Login, $Password, $Tariff, $SenderName, $Body, $MSISDN, $Reference); //Send SMS $respnose=$SMS->SendMessage('http://smsgateway02.cm.nl/cm/gateway.ashx',$XMLtoSend); if($respnose=="") echo "SMS Sent Successfully"; else echo $respnose;

cmSMS.php: 
class CMSMS 

  function CreateMessage($CustomerID, $Login, $Password, $Tariff, $SenderName, $Body, $MSISDN, $Reference="")
  {  
    $MSISDN=str_replace("+","",$MSISDN);//remove begining plus sign
    if(substr($MSISDN,0,2)!="00")
    $MSISDN="00".$MSISDN;
    $XMLSMS = new SimpleXMLElement('<MESSAGES/>'); 
    $XMLSMS->addAttribute('PID',25); 
     
    $XMLSMS->addChild('CUSTOMER'); 
    $XMLSMS->CUSTOMER->addAttribute('ID',$CustomerID); 
     
    $XMLSMS->addChild('USER'); 
    $XMLSMS->USER->addAttribute('LOGIN',$Login); 
    $XMLSMS->USER->addAttribute('PASSWORD',$Password); 

//Helpful for assign cusotm SMS ID
if($Reference!="")
{
      $XMLSMS->addChild('REFERENCE');
      $XMLSMS->REFERENCE = $Reference;
}
     
    $XMLSMS->addChild('TARIFF'); 
    $XMLSMS->TARIFF = $Tariff; 
     
    $XMLSMS->addChild('MSG'); 
     
    $XMLSMS->MSG->addChild('FROM'); 
    $XMLSMS->MSG->FROM = $SenderName; 
     
    $XMLSMS->MSG->addChild('BODY'); 
    $XMLSMS->MSG->BODY = $Body; 
     
    $XMLSMS->MSG->addChild('TO'); 
    $XMLSMS->MSG->TO = $MSISDN; 

//Multipart SMS.Recipient will get more then 160 char at once
$no_of_sms_credit_required=ceil(strlen($Body)/160.00);
if($no_of_sms_credit_required>1)
    {
      $XMLSMS->MSG->addChild('MINIMUMNUMBEROFMESSAGEPARTS');
      $XMLSMS->MSG->MINIMUMNUMBEROFMESSAGEPARTS = 1;

      $XMLSMS->MSG->addChild('MAXIMUMNUMBEROFMESSAGEPARTS'); 
      $XMLSMS->MSG->MAXIMUMNUMBEROFMESSAGEPARTS = $no_of_sms_credit_required;

      $XMLSMS->MSG->addChild('TARIFFPERMESSAGEPART');
      $XMLSMS->MSG->TARIFFPERMESSAGEPART = $Tariff;
}

    return $XMLSMS->asXML(); 
  } 
   
  function SendMessage($URL, $Message) 
  { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    #curL_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'Content-length: '.strlen($Message))); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $Message); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $return = curl_exec($ch); 
    curl_close($ch);     
    return $return; 
  } 
}

Concatenated SMS:
A single SMS contain maximum 160 character but sometimes your SMS might longer then that and your recipient love to receive whole SMS at once(in a single SMS). In order to do it, you have to use additional parameter MINIMUMNUMBEROFMESSAGEPARTS,  MAXIMUMNUMBEROFMESSAGEPARTS & TARIFFPERMESSAGEPART in SMS XML preparation.

FAQ:
Where from shall i get ProductToken of CM Direct?
=> You will receive a Product Token by mail after creating an account.

For HTTP method where from  shall i get Customer ID, username and password?
=> CM.NL will create these for you.

What response do i receive for HTTP?
=> Every request will get an HTTP response with status 200 (OK), even if the request is malformed. If the request was correct, the response will be empty. If the request was malformed, the response will start with “ERROR”. So it's easy for you to check a SMS sent or not by $response=="" (success)

How to pass a custom text(specially needed for SMS ID) which will back to me while Delivery Receipt?
=> You can pass custom text using REFERENCE field.

For any further enquiry please feel free to post your comments.

4 comments:

  1. Hello.. I have a customer ID, username and password with Cmtelecom but when i try to send message, its not delivering to phone and i am not getting any error.. I am connecting with HTTP. How do i post the xml parameter fields? Is it like 26089766 or its "[26089766]"

    ReplyDelete
    Replies
    1. You do not need to think about 26089766 or [26089766]. XML file automatically created for you by calling function CreateMessage().
      Are you trying from you local computer?
      is your php_curl on in php.ini?
      have you tried different mobile operator as recipient?

      Delete
    2. Hi Kennedy, there is some new (shorter) example code in PHP available. Drop me a line if you need some more help.

      $SMS = new CMSMS;
      $XMLtoSend = $SMS->CreateMessage('de7c7df3-81ca- 4d1e-863d-95a252120321',
      'My company',
      '+31600000000' (NL) or '+32400000000' (BE)',
      'Example message text');

      print $SMS->SendMessage('https://secure.cm.nl/smssgateway/cm/gateway.ashx', $XMLtoSend);

      class CMSMS {
      function CreateMessage($ProductToken, $Sender, $Recipient, $Body) {
      $XMLSMS = new SimpleXMLElement(''); $XMLSMS->addChild('AUTHENTICATION'); $XMLSMS->AUTHENTICATION->addChild('PRODUCTTOKEN', $ProductToken); $XMLSMS->addChild('MSG');
      $XMLSMS->MSG->addChild('FROM'); $XMLSMS->MSG->FROM = $Sender; $XMLSMS->MSG->addChild('BODY'); $XMLSMS->MSG->BODY = $Body; $XMLSMS->MSG->addChild('TO'); $XMLSMS->MSG->TO = $Recipient; return $XMLSMS->asXML();
      }

      function SendMessage($URL, $Message) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $URL);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'Content-length: ' .
      strlen($Message)));
      curl_setopt($ch, CURLOPT_POSTFIELDS, $Message); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $return = curl_exec($ch);
      curl_close($ch);
      return $return;
      }
      }

      Delete
  2. Nice post it is very useful for all. Bulk SMS API In PHP is one of the supported programming languages, so adding the API will be as simple as installing and copying the code snippet(from which ever API you choose) to your application.

    ReplyDelete