Implement two-way SMS with PHP

Source: Internet
Author: User
Tags clickatell
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... SyntaxHighlighter. all ();

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 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

02

03
Xxx
04
Fa6ba35b330ce1bc7e2008e5d92d57aa
05
Handset_number_here
06
Mo_number_here
07
2007-02-26 14:36:50
08
Xxx
09
ISO-8859-1
10

11

View source
Print?
01
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
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
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.