Integrate API

Integrate API - Header

Monday, July 22, 2013

How to integrate infobip SMS API in PHP

Now a days infobip.com SMS API becomes popular due to their supurb service. You can integrate their SMS service with your website using their API. You can get detail instructions here.

php code is here for your reference:

infoBipSMS.php Class:
<?php
class infoBipSMS {
/*************** http://www.infobip.com/messaging/connectivity/apis ************************/
// InfoBip account credentials
private $infobip_username = '';
private $infobip_password = '';

/**
* @var string InfoBip server URI
*
*/
var $nx_uri = 'http://api.infobip.com/api/v3/sendsms/xml';

function infoBipSMS ($infobip_username, $infobip_password) {
$this->infobip_username = $infobip_username;
$this->infobip_password = $infobip_password;
}

/**
* @return: 
Status Vale Description
AUTH_FAILED - 1 Invalid username or password
XML_ERROR - 2 Incorrect XML format
NOT_ENOUGH_CREDITS - 3 Not enough credits in user account
NO_RECIPIENTS - 4 No good recipients
GENERAL_ERROR - 5 Error in processing your request
SEND_OK > 0 Number of recipients to which message was sent
*
*/
function send_sms_infobip($sender_no_with_plus, $recipient_no_with_plus, $sms_body, $msg_id="")
{
if(substr($sender_no_with_plus,0,1)!="+")
 $sender_no_with_plus="+".$sender_no_with_plus;
if(substr($recipient_no_with_plus,0,1)!="+")
 $recipient_no_with_plus="+".$recipient_no_with_plus;
$msg_id=($msg_id=="")?time():$msg_id;
//  BUSYBEE's POST URL 
//$postUrl  = "http://193.105.74.59/api/sendsms/xml";
$postUrl  = "http://api.infobip.com/api/v3/sendsms/xml";

//  XML-formatted data
$infobip_username=$this->infobip_username;
$infobip_password=$this->infobip_password;
$no_of_sms_will_send=ceil(strlen($sms_body)/160);
for($i=0;$i<$no_of_sms_will_send;$i++)
{
$sms_part=substr($sms_body,0,160);
if(strlen($sms_body)>160)
   $sms_body=substr($sms_body,160);
$xmlString = "
<SMS> 
 <authentification> 
<username>$infobip_username</username> 
<password>$infobip_password</password> 
 </authentification> 
 <message> 
<sender>$sender_no_with_plus</sender> 
<text>$sms_part</text> 
<flash></flash>
<type></type> 
<wapurl></wapurl> 
<binary></binary> 
<datacoding></datacoding> 
<esmclass></esmclass> 
<srcton></srcton> 
<srcnpi></srcnpi> 
<destton></destton> 
<destnpi></destnpi> 
<ValidityPeriod>23:59</ValidityPeriod> 
 </message> 
 <recipients> 
<gsm messageId=\"$msg_id\">$recipient_no_with_plus</gsm> 
 </recipients> 
</SMS> ";

//echo $xmlString;
 
//  previously formatted XML data becomes value of "XML" POST variable 
$fields  = "XML=" . urlencode($xmlString); 
 
//  in this example, POST request was made using PHP's CURL 
$ch  = curl_init(); 
curl_setopt($ch,  CURLOPT_URL, $postUrl); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,  $fields); 
 
//  response of the POST request 
$response  = curl_exec($ch);
curl_close($ch); 
}

//  write out the response 
return $response;

}

}

And here is send_sms_start.php file:
<form method="post" action="send_sms.php">
  Infobip Username: <input name="username" type="text" value="user-name"><br/>
  Infobip Password: <input name="password" type="text" value="password"><br/><br /><br />

  Sender No: <input name="sender_no" type="text" value="+my-sender-no"><br/>
  Recipient No: <input type="text" name="recipient_no" value="+recipient-no"><br/>
  SMS Text: <input type="text" name="sms_body">
  <input type="submit" value="Send SMS">

</form>


Really simple and easy. right? Lets concentrate how to customize for different cases:

1. How to send Same SMS to multiple recipient?
=> in xml portion add as much recipient no as you need in new line <gsm>987654321</gsm> here i use 2 recipient number but you can add as much as you need.

2. Can i send more then 160 character long SMS?
=> Infobip currently don't support SMS with more then 160 character. But using this class (code written earlier of this page) code you will be able to send any length SMS. I actually used a very simple technique (chunk SMS text by 160 character and send separately)

All variables are self explanatory. so you can easily customize those. if you have any further question please feel free to ask by post comment here.

Error and solution:
Error: For a long SMS around 400 character long why i receive only last chunk(divide sms character into 160 character chunk) of this SMS?
Solution: I found that, i have used & in that long 400 character SMS. when i remove & from SMS it works accurately. instead of use & now i use &amp; and works fine. So before send sms text to infobip treat SMS text by htmlspecialchars() function.

15 comments:

  1. https://github.com/Boorgeon/Infobip_sms_api/

    ReplyDelete
    Replies
    1. Yeah that is easy. How do you get an SMS reply into the system? like into a mysql database

      Delete
    2. In order to receive SMS reply into your system you first need to purchase an inbound number. Then, use it as sender id for outgoing SMS by customizing sender id. Now when a recipient receive your SMS he/she can reply and which actually comes to your inbound number.
      SMS received at inbound number actually hit a URL specified by you(GET or POST). These are configurable and changeable when you purchase inbound number.

      Delete
  2. really, simple and easy!!!
    how can i send to multiple users?

    ReplyDelete
    Replies
    1. In order to send same SMS to multiple recipient you need to add multiple numbers along with distinct message id in . here is an example


      $recipient_no_with_plus
      $recipient_no_with_plus

      add as many recipient as you need.

      Delete
  3. Hello Sumon,
    Please how can i write a query to retrieve my infobip account sms credit balance using php ? I would be glad if i can find my way around this. I have already successfully been able to use the php script to send sms now i just want to be able to get my sms balance.

    Thank you.

    ReplyDelete
  4. how can i do this in c#

    ReplyDelete
  5. Hello Sumon,

    I am trying to integrate my infobip account with Vtiger. I created a PHP script for the SMS Notifier module but I still cant send SMS.

    ReplyDelete
    Replies
    1. do you use same code as i post(with your credentials)? if yes then please email me your code at aminul.tashnim@gmail.com

      Delete
  6. If you are not updated with the ABC's in your field, then you are out of competition. This trend is essential for all business organizations where they are required to stay connected to all their users, and keep them satisfied on services and qualities. This leads to customer satisfaction. But, how does a company achieve this with budgeting, limited staff, and getting complete results effectively. This is where today's technologies of the techno world come handy. One such user friendly technology is bulk SMS gateway API.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Nice post it is very useful for all. It is Simple to Integrate Bulk SMS API in PHP, all you need to do . As it is instant delivery users can get the respective codes from API panel within a fraction of seconds.

    ReplyDelete
  9. Thank for sharing this blog.

    SMS API PHP

    ReplyDelete
  10. Our Ready php code/ script will help you to save time and efforts at a large level. Your enterprise/organization's php application can be easily integrated with our services. However, we provide ready to use bulk SMS API PHP code that helpfuls to send various information to your user's mobile.

    ReplyDelete
  11. Thanks, Such a nice article. You can integrate their SMS service with your website using their API. Please visit http://www.msgclub.net/sms-service/bulk-sms-api.aspx

    ReplyDelete