This is a follow-up article on sending/receiving SMS through Nexmo. In the previous article here, we discussed how to send SMS through Nexmo in PHP. In this article, we discuss about how to receive SMS through Nexmo API.
In this article and the subsequent articles, I will be discussing specifically about Nexmo API. There are a number of SMS Gateways like Twilio, BulkSMS and so on available in the market. However, we found Nexmo to be very reliable as well as cheaper.
When I discuss about Nexmo, I believe, you already have an account with Nexmo. If you donot have an account already, you can create an account for free here. They provide you with $2 Account balance which you can use it to test all the features. Some of the features include :
- Send / Receive SMS
- Send Automated Voice Message
- Voice Telephony
In this article, we will discuss about receiving SMS through Nexmo. For demonstration, I will be using PHP. The complete source for sending SMS can be downloaded from here.
Example : Let’s say, you have a client who wish to send you an SMS. How do you read the inbound SMS from the client and record this message in the system.
Step 1: : Login to your Nexmo Dashboard here and set the callback URL for Inbound SMS to a valid URL for receiving SMS from the user and save your settings. In this example, I have set it to http://sapnaedu.in/sms_inbound.php
Step 2 : Obtain Long Virtual Number :
The next step is used to purchase Virtual Numbers from Nexmo. These numbers are used by your client for sending/receiving SMS. In order to purchase a Virtual Number from Nexmo, goto Dashboard -> Products -> Numbers.
You can purchase as many Virtual numbers from as many countries as possible. Try to purchase a number in a country where you are operating. If you are operating in USA, it makes sense, to purchase a Virtual Number from USA and from your state. It costs less for your client, to send SMS to you through Nexmo API.
Depending on the country, each Virtual Number costs anywhere between 0.5€ to 25€ per month.
Let’s say you bought the number 12069396238 ( which costs 0.67€/month), this is the number you would share with your clients. Your client would send an SMS to this number just like any normal SMS.
Step 3 : Now, lets say your client in USA sends an SMS to the virtual number that you just bought ( 12069396238 ), Nexmo API sends GET request to the callback URL ( http://sapnaedu.in/sms_inbound.php) setup in Step 1. The request would generally include following parameters :
- msisdn – Mobile Number of the Client including the country code
- to – Your long Virtual Number ( 12069396238 )
- messageId – A unique Alpha-Numeric code identifying each message
- text – The SMS text sent by the client
- message-timestamp – Time at which the SMS was sent in YYYY-MM-DD HH:MM:SS format
$frm = $_GET;
$mobile = $frm['msisdn'];
$now = date('Y-m-d H:i:s');
$message = $frm['text'];
//Optional Save the Message in the Database
//$q = "insert into sms_message set
// `date`='" . $now . "',
// `message`='" . $message . "',
// `mobile`='" . $mobile . "'";
####LOG THE MESSAGE#####
$text = "";
foreach ($frm as $key=>$value)
$text .= "$key=$value, \n";
$log_file = '.sms_inbound.txt';
fwrite($fp, $text . "\n\n");
fclose($fp); // close file
Step 4 : You can respond to the SMS automatically by processing the text message, and save the message in the Database etc. However, processing part is beyond the scope of this article. If you have any questions about it, please write to me, I would be glad to help.
The complete PHP source can be downloaded from here.
I hope, you find this article very useful. If you have any doubts or have any comments concerning this tutorial, Please leave a comment or contact me. I would be glad to help.