Implementing bidirectional sms_php Tutorials with PHP

Source: Internet
Author: User
Tags urlencode clickatell
SMS is used for various purposes these days. For example, large sites such as Gmail and Facebook use SMS Multi-factor authentication and notify users about updates to improve their certification process. This is a one-way SMS app that can only send messages from users of these sites since.
Bidirectional SMS applications are not a one-way complex. For two-way SMS applications, the user can initiate a session by sending a message and then respond to the application according to the user's command.
In this article, I'll explain the process of bi-directional SMS interactions and show you how you can incorporate your PHP application. There are many popular SMS gateways that provide two-way communication around the world, and here I explain that I will use Clickatell.
Bi-directional SMS life cycle
The life cycle of two-way SMS can be divided into 4 main steps, as follows:

1, the user sends the SMS Gateway request
2, the request of the SMS gateway is forwarded to the application server
3, the application server processes the request and response SMS Gateway
4, SMS gateway forwarded to user request

1th Step-User SMS Gateway

The user sends a conversation via SMS gateway. Initially, it will go through the user's mobile service provider. This part is not very important to us because it is on the local service provider's domain name. SMS gateways received via local service provider messages.
The user obviously needs to send his message to a specific number. You can use two-way communication, either a simple code or a dedicated long number (did) specific to the application. These numbers are via SMS gateways and usually you need to purchase these numbers.
The dedicated quantity is a standard phone number, such as 94112367899. A simple code is a 4-6 digit code. Everyone has their own strengths and weaknesses:
Jane Code is dedicated to long numbers compared to the more easily remembered.
Private numbers can be used globally, while simple codes are usually limited to a single service provider in a particular country.
Dedicated long numbers can handle a large number of messages for a given time compared to a short code number.
Choosing whether to use a simple code or a dedicated long number ultimately depends on you.
Once the SMS gateway is received, it should be routed to the application in process. Before the message is routed, the gateway needs to do two things:
The application can understand a way to prepare the message data in the
Application URL mapping private number or short code
Each gateway has its own method of transferring data to the application server, although XML or soap is usually the most popular over HTTP connections. The gateway should provide some API documentation for the sort of method that has ever been used.
The application developer creates a system to handle the specific pointcut of messages received from the gateway, which is called the URL of the application. The URL of the application maps to a private number or a short code process, from one gateway to another change. Some of the configurations allowed directly through the user account interface, such as Clickatell do. If this option is not available, we have contact technical support resources for the gateway and provide them with the URL of the application to configure.
2nd Stage-SMS Gateway Application Server

The owner of the application then decides how to receive data from the gateway. Clickatell allows you to specify this method within a user account. If it is not available again, you need to contact the technical team for the specific gateway.
The following code example shows how you clickatell through the HTTP GET method, and now you can receive the data it sends. 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%20messagetext&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 a data publication 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 ("to");
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 the incoming data, use one of the available methods, and process the user's commands. This code in the user's message is a simple scene with less than 160 characters, perfect for the work. But what happens if the message is more than 160 characters?
Suppose a TV station starts an ad where users can send SMS ads. Once the SMS is sent, the user will receive a time period of the ad will display the server. The ad is plain text that will be displayed at the bottom of the screen. We also assume that we have a predefined format for sending messages like the one shown below.
Ad campaign title your ad headline, your ad message content
Value pairs all content in the message that appears. Ads will label and Sport will represent the value of the ad category. Then, the title is the next label, and its value will be the title of the ad. The message is the last label that will be displayed to the user in the ad.
Depending on the title and message length, this text message can have more than 160 characters. Each time a lengthy message is received, the gateway is split into multiple parts of 160 characters. But we still have to deal with 2 pieces as a single message.
If we use the previous code example to parse the message, the 1th and 2 identical messages will be processed as two different messages. Because these two parts do not have a complete command, the application will send the error back to the user. The UDH is to solve the problem.
What is the Udh?

The Udh group represents the user data header. When we send a lengthy message, the sending device (mobile phone) splits the message and sends it as a separate message. The value of UDH will be assigned to each message part to start so that the receiving device can identify them as belonging to a single message and reorganize
Udh the earlier scene of the group value looks like this:
Part 1-05, CC 02 01
Part 2-05, CC 02 02

The last two hexadecimal values will be the most important value in the Udh. The second to last number is 02 the number of parts defined in the above code and message. Therefore, this message is divided into two parts. If three parts, then this value should be 03, and so on. The future number defines a part of the message. 01 means that it is the first part, 02 means the second part, and so on.
Now that we have what we need to know about the processing time extended, the message is 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
$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) {
21st
$prevParts = Getmessageparts ($db, $from);
22
$message = $prevParts. $message;
23
}
24
25
Process $message
26
...

Now we can process a complete information and action on the request cycle to complete this phase. The next two phases, how to respond to a user sending back through the gateway.
3 and 4th stage-SMS Gateway, gateway to user's application server

In the previous phase, I explained the process of receiving mail from the gateway in your application and processing. Once the request is processed and a response is generated, we need to send it back to the gateway.
In general, the SMS gateway provides a callback URL for the response data that we pass. You usually have to provide the number of receivers, the phone number of the sender, the message content, and some authentication information. The exact parameters will be based on different gateways, but they are clickatell, from, text, and api_id, username, password.
01
02
$message = Array (
03
"To" = 942288345,
04
"From" = 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);
curl_setopt ($ch, Curlopt_returntransfer, 1);
20
curl_setopt ($ch, Curlopt_url, $APIURL);
21st
22
Curl_exec ($ch);
23
Curl_close ($ch);



The first code example above encodes all the callback URLs that use parameters for UrlEncode () and add them to the API. We then initialize the curly request and call the URL. The response message is now sent to the gateway, ending the 3rd step.
The 4th step is simple and we haven't done any part in this process. The gateway is responsible for sending all messages in the correct order to the user's move.
Summarize
We started talking about bi-directional SMS messages in this tutorial and why it is useful. Then, we discuss the two-way communication process, through 4 main stages. Now, you should be able to apply the concept of covering with any particular SMS gateway and implement bidirectional SMS in your PHP application.

Author: ssoftware

http://www.bkjia.com/PHPjc/478015.html www.bkjia.com true http://www.bkjia.com/PHPjc/478015.html techarticle SMS is used for various purposes these days. For example, large sites such as Gmail and Facebook use SMS Multi-factor authentication and notify users about updates to improve their certification process. This is one-way short ...

  • Related Article

    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.