In this article and the subsequent articles, I will be discussing about sending / receiving SMS through PHP. To send/receive SMS, we will be using 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 Sending 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 want to send an SMS to your client in United States reminding about the payment. So, you would send an SMS to your client saying,
Hey John, This is a reminder to make a payment for the month of August,2015 subscription. Please make a payment to avoid cancellation of your subscription
Thanks
Sapnaedu
Steps
Step 1: : Login to your Nexmo Dashboard here and obtain the API key and a secret key associated with your account. You can also set the URL to notify the delivery of the message to your client.
Step 2 : Now, we have the following information to send the SMS to our client :
- Nexmo API Key : 1234
- Nexmo API Secret : 456789
- Delivery Receipt URL : http://sapnaedu.in/sms_delivery.php
- Client Mobile Number( Including Country Code ) : +1954123456789
- Message : Try to keep the length of the text to less than 160 characters. If the message is longer than 160 characters, Nexmo automatically send multiple text messages in parts each having 160 characters.
Step 3 : Build the argument list to be submitted to the base URL. https://rest.nexmo.com/sms/json?. This can be done using the following PHP code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
$base_url = "https://rest.nexmo.com/sms/json?";
$params = array();
$params['api_key'] = '1234';
$params['api_secret'] = '456789';
$params['from'] = 'SAPNAEDU';
$params['to'] = '+1954123456789';
$params['status-report-req'] = '1'; // Send status report to the Delivery Recipt URL
//( http://sapnaedu.in/sms_delivery.php )
$params['client-ref'] = '123456|Client-rambo456';
// Client Reference. This parameter is passed as-is to the Delivery Recipt URL
$params['text'] = "Hey John, This is a reminder to make a payment for the month of August,2015 subscription. Please make a payment to avoid cancellation of your subscription \nThanks \n Sapnaedu";
$params1 = http_build_query($params);
$url = $base_url . $params1;
|
Step 4 : The next step is to submit the request to the Nexmo API. This can be done using the following PHP code :
1
2
3
|
$result = call_url($url); // Definition of call_url can be found in the source code
//Call the URL with all the parameters and return the HTTP response as a JSON Object.
|
Step 5 : Handling the response
As we have submitted the request to https://rest.nexmo.com/sms/json which is a JSON end point, the response will be a JSON object. A typical response would look like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$result =
{
"message-count":"1",
"messages":[
{
"status":"0",
"message-id":"32242322",
"to":"1954123456789'",
"remaining-balance":"11.15",
"message-price":"0.02",
"network":"4408"
}
]
}
|
The response with status=0, indicate that the message was sent successfully. Even though the SMS was sent successfully, it does not necessarily guarantee that SMS was delivered to the recipient.The delivery status is confirmed to the Delivery Receipt URL : http://sapnaedu.in/sms_delivery.php via GET method ( as configured in Step 2 ).
Step 6 : Check successful receipt of the SMS
Once the SMS was sent, Nexmo API notifies the Delivery receipt URL http://sapnaedu.in/sms_delivery.php via GET method. A typical delivery response would look similar to this :
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$delivery_response =
{
"msisdn" : "1954123456789'",
"to" : "SAPNAEDU",
"network-code" : "310260",
"messageId" : "020000006FC27627",
"price" : "0.00480000",
"status" : "accepted",
"scts" : "1507270315",
"err-code" : "1",
"client-ref" : "123456|Client-rambo456",
"message-timestamp" : "2015-07-27 03:"15:"02"
}
|
You need to check the status field. If the SMS is delivered, the status field would be set to “accepted” or “delivered”. If the SMS is not delivered for variety of reason which include :
- The client phone is not reachable/switched off
- Network Error
- The client has barred all incoming SMS, etc
the status is set to “failure” or “failed”.
Download
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.
2 Comments