SMS is used for various purposes these days. For example, large websites such as Gmail and Facebook use SMS multi-factor authentication and notify users about updates to improve their authentication process. This is a one-way text message application that can only send messages from users of these websites.
Two-way SMS applications are not one-way complex. Two-way SMS application, the user can start a session by sending a message, and then according to the user's command, the application response.
In this article, I will explain the interaction process between two-way text messages and tell you how to integrate them into your PHP application. There are many popular SMS gateways that provide two-way communication around the world. Here I explain that I will use Clickatell.
Bidirectional SMS Lifecycle
The life cycle of two-way SMS can be divided into four main steps as follows:
1. the user sends the SMS gateway request
2. send SMS gateway requests to the application server.
3. The Application Server Processes requests and responds to the SMS gateway.
4. The SMS gateway forwards the request to the user.
Step 2-SMS Gateway
The user sends a conversation to the SMS gateway. Initially, it had to go through the user's mobile service provider. This part is not very important to us because it is the domain name of the local service provider. The SMS gateway that receives messages from the local service provider.
The user apparently needs to send his message to a specific number. You can use two-way communication, whether it is a simple code or a dedicated long number (DID) specific to the application. These numbers are sent through the SMS gateway. You usually need to purchase these numbers.
The dedicated number is a standard phone number, such as 94112367899. A simple code is a 4-6 digit code. Everyone has their own advantages and disadvantages:
Simple code is easier to remember than dedicated long numbers.
You can use a dedicated number globally, and the short code is usually limited to a single service provider in a specific country.
A dedicated long number that can process a large number of messages.
Whether or not to use a simple code or a dedicated long number depends on you.
Once the SMS gateway receives the message, it should be routed to the processing application. For previous message routing, the gateway needs to do two things:
The application can understand one way in the prepared message data.
Application URL ING Private numbers or short codes
Each gateway has its own method to transfer data to the application server, although XML or SOAP is usually the most popular connection over HTTP. The gateway should provide some sort of API documentation for the historical method to use.
Application developers create a system to process the specific entry point of messages received from the gateway. This is the so-called application URL. When the application URL is mapped to a dedicated number or short code, it changes from one gateway to another. Some configurations can be directly performed through the user account interface, such as Clickatell. If this option is unavailable, We have technical support resources for the contact gateway and provide them with URLs for the application to be configured.
Stage 1-SMS gateway Application Server
Then the application owner decides how to receive data from the gateway. Clickatell allows you to specify this method in the user account. If it is unavailable again, you need to contact the technical team of the specific gateway.
The following code example shows how Clickatell sends data through the http get method. Other gateways will provide similar methods as well.
Https://www.example.com/sms/sms.php? Api_id = 12345 & from = 279991235642 & to = 27123456789 & timestamp = 2008-08-0609: 43: 50 & text = Hereisthe % 20 messagetext & charset = ISO-8859-1 & udh = & moMsgId = b2aee337abd962489b123fda9c3480fa
1 <? Php
2 $ fromNo = $ _ GET ["from"];
3 $ toNo = $ _ GET ["to"];
4 $ message = $ _ GET ["text"];
5 $ msgID = $ _ GET ["moMsgId"];
6
7 // Process the user command and generate output
8...
Here is an example of data published as an XML document.
01
<? Xml version = "1.0"?>
02
<Clickmo>
03
<Api_id> xxx </api_id>
04
<MoMsgId> fa6ba35b330ce1bc7e2008e5d92d57aa </moMsgId>
05
<From> handset_number_here </from>
06
<To> mo_number_here </to>
07
<Timestamp> 14:36:50 </timestamp>
08
<Text> xxx </text>
09
<Charset> ISO-8859-1 </charset>
10
<Udh> </udh>
11
</Clickmo>
View source
Print?
01
<? Php
02
$ Data = $ _ POST ["data"];
03
04
$ XmlDoc = new DOMDocument ();
05
$ XmlDoc-> loadXML ($ data );
06
07
$ FromNo = $ xmlDoc-> getElementsByTagName ("from ");
08
$ FromNo = $ fromNo-> item (0)-> nodeValue;
09
10
$ ToNo = $ xmlDoc-> getElementsByTagName ("");
11
$ ToNo = $ toNo-> item (0)-> nodeValue;
12
13
$ Message = $ xmlDoc-> getElementsByTagName ("text ");
14
$ Message = $ message-> item (0)-> nodeValue;
15
16
// Process User Message and Generate Response
17
...
The application needs to capture incoming data, use one of the available methods, and process user commands. This code is a perfect work in a simple scenario with less than 160 characters. But what will happen if the message is more than 160 characters?
Assume that a TV station starts an advertisement and users can send text message advertisements. Once a text message is sent, the user will receive an advertisement for a period of time and the server will be displayed. The advertisement is plain text that will be displayed at the bottom of the screen. We also assume that we have a predefined format to send the following message.
Ad sports question your ad title, your ad Information Content
All content in the message of the value pair. Ad labeling and sports meetings represent the value of advertising. Then, the title is the next tag, and its value will be the title of the advertisement. The message is the final tag that shows the content of the user in the advertisement.
Based on the title and message length, this text message can contain more than 160 characters. When a long message is received, the gateway is divided into 160 characters in multiple parts. However, we still have to process two messages as a single message.
If we use the preceding code example to parse a message, messages with the same 1st and 2 will be processed as two different messages. Because these two parts do not have complete commands, the application will send the error back to the user. UDH solves this problem.
What is UDH?
The UDH group represents the user data header. When we send a long message, send the message split by the device (mobile phone), and send it as a separate message. The UDH value will be allocated to each part of the message to start, so that the receiving device can identify them as a single message and restructure
The earlier UDH group values look like this:
Part 1-05 00 03 CC 02 01
Part 2-05 00 03 CC 02 02
The last two hexadecimal values will be the most important value in UDH. The second to the last digit is 02 the number of parts defined in the Code and message above. Therefore, the message is divided into two parts. If there are three parts, the value should be 03, and so on. The future number defines part of the message. 01 means it is the first part, 02 means the second part, and so on.
Now, what do we have? We need to know the messages whose processing time is extended and divided into multiple parts.
01
<? Php
02
$ FromNo = $ _ GET ["from"];
03
$ ToNo = $ _ GET ["to"];
04
$ Message = $ _ GET ["text"];
05
$ MsgID = $ _ GET ["moMsgId"];
06
$ Udh = $ _ GET ["udh"];
07
08 $ total = 1;
09
$ Count = 1;
10
If ($ udh ){
11
$ Tmp = str_split ($ udh, 2 );
12
$ Total = hexdec ($ tmp [4]);
13
$ Count = hexdec ($ tmp [5]);
14
}
15
16
If ($ count! = $ Total ){
17
// Save the message fragment in database
18
SaveMessagePart ($ db, $ from, $ message, $ udh );
19
}
20
Else if ($ total! = 1 ){
21
$ PrevParts = getMessageParts ($ db, $ from );
22
$ Message = $ prevParts. $ message;
23
}
24
25
// Process $ message
26
...
Now, we can complete the request cycle at this stage in terms of complete information and actions. In the next two phases, how do I respond to the requests sent back through the gateway.
3 and 4th-SMS gateway, gateway to your application server
In the previous phase, I explained the process of receiving emails from the gateway in your application and processing. Once a request is processed and a response is generated, we need to send it back to the gateway.
Generally, the SMS Gateway provides a callback URL for the response data we transmit. You must provide the number of receivers, the sender's phone number, email content, and some authentication information. The exact parameters will vary depending on the gateway, but they are Clickatell, from, text, and api_id, user name, and password.
01
<? Php
02
$ Message = array (
03
"To" => 942288345,
04
"From" = & gt; 944488345,
05
"Text" => "sample message ",
06
"Api_id" => API_KEY,
07
"User" => "myUsername ",
08
"Password" => "secret"
09
);
10
11
$ ApiUrl = "http://api.clickatell.com/http/sendmsg? ";
12
Foreach ($ message as $ parameter => $ value ){
13
$ ApiUrl. = $ parameter. "=". urlencode ($ value )."&";
14
}
15
$ ApiUrl = rtrim ($ apiUrl ,"&");
16
17
$ Ch = curl_init ();
18
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
19 curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
20
Curl_setopt ($ ch, CURLOPT_URL, $ apiUrl );
21
22
Curl_exec ($ ch );
23
Curl_close ($ ch );
The first code example above encodes all the URL URLs that use parameters for urlencode () and adds them to the API callback URL. Then, we initialize the curl request and call the URL. Now the response message is sent to the gateway, and step 2 is completed.
Step 2 is simple, and we have not done anything in this process. The gateway is responsible for sending all messages in the correct order to the user's mobile device.
Summary
We started to discuss why two-way SMS messages are useful in this tutorial. Then, we discussed four major phases in the Process of bidirectional communication. Now you should be able to apply to concepts that cover any specific SMS gateway and implement bidirectional SMS in your PHP application.
Author: ssoftware