php实现双向链表_用PHP实现双向短信

php实现双向链表

SMS is used for various purposes these days. For example, major websites like Gmail and Facebook use SMS to improve their authentication process with multi-factor authentication and notifying users about the updates. These are one-way SMS applications since messages are sent only from these sites to the user.

如今,SMS已用于各种目的。 例如,Gmail和Facebook等主要网站都使用SMS来通过多因素身份验证来改善其身份验证过程,并通知用户有关更新的信息。 这些是单向SMS应用程序,因为消息仅从这些站点发送给用户。

Two-way SMS applications are more complex than one-way ones. In two-way SMS apps, a user can initiate a conversation by sending messages, and then the application responds according to the user’s commands.

双向SMS应用程序比单向应用程序复杂。 在双向SMS应用程序中,用户可以通过发送消息来发起对话,然后该应用程序根据用户的命令进行响应。

In this article I will explain the process of two-way SMS interactions and show you how you can incorporate it in your PHP applications. There are many popular SMS gateways available around the world which provide two-way messaging; In my explanations here, I will be using Clickatell.

在本文中,我将解释双向SMS交互的过程,并向您展示如何将其合并到PHP应用程序中。 世界各地有许多流行的SMS网关提供双向消息传递。 在这里的解释中,我将使用Clickatell。

双向短信生命周期 (Two-Way SMS Life Cycle)

The life cycle of two-way SMS can be divided into 4 main steps as shown below:

双向短信的生命周期可以分为四个主要步骤,如下所示:

alt
  1. User sends request to SMS gateway

    用户向SMS网关发送请求
  2. SMS gateway forwards request to application server

    SMS网关将请求转发到应用程序服务器
  3. Application server processes request and responds to SMS gateway

    应用程序服务器处理请求并响应SMS网关
  4. SMS gateway forwards request back to user

    SMS网关将请求转发回用户

步骤1 –用户使用SMS网关 (Step 1 – User to SMS Gateway)

The user initiates the conversation by sending an SMS message to the gateway. Initially it has to go through user’s mobile service provider. This part is not important to us since it is within the domain of the local service provider. Through the local service provider the message is received by the the SMS gateway.

用户通过向网关发送SMS消息来发起对话。 最初,它必须经过用户的移动服务提供商。 这部分对我们而言并不重要,因为它在本地服务提供商的范围之内。 通过本地服务提供商,该消息由SMS网关接收。

The user obviously needs a specific number to send his messages to. You can use either shortcodes or dedicated long numbers (DID) specific to your application for two-way communication. These numbers are provided by SMS gateways, and normally you need to purchase these numbers.

用户显然需要特定的号码才能将其消息发送到。 您可以使用特定于您的应用程序的短码或专用长号(DID)进行双向通信。 这些号码由SMS网关提供,通常您需要购买这些号码。

A dedicated long number is a standard phone number, like +94112367899. A shortcode is a 4 – 6-digit code. Each have their own pros and cons:

专用长号是标准电话号码,例如+94112367899。 简码是4到6位的代码。 每个人都有自己的优点和缺点:

  • Shortcodes are easier to remember than dedicated long numbers.

    短代码比专用的长数字更容易记住。
  • Dedicated numbers can be used globally while shortcodes are normally limited to a single service provider in a given country.

    专用号码可以在全球范围内使用,而短代码通常限于给定国家/地区中的单个服务提供商。
  • Dedicated long numbers can process a larger number of messages for a given time compared to shortcode numbers.

    与短代码号码相比,专用长号码可以在给定的时间内处理大量消息。

The choice whether to use a shortcode or dedicated long number is ultimately up to you.

最终选择使用短码还是专用长号。

Once the SMS is received by the gateway, it should be routed to the application for processing. Before the routing the message, the gateway needs to do two things:

一旦网关接收到SMS,就应该将其路由到应用程序进行处理。 在路由消息之前,网关需要做两件事:

  • Prepare the message data in a way that application can understand

    以应用程序可以理解的方式准备消息数据
  • Map an application URL to the dedicated long number or shortcode

    将应用程序URL映射到专用的长号或简码

Each gateway has its own methods for transferring data to the application server, though XML or SOAP over an HTTP connection are generally the most popular. The gateway should provide some sort of API documentation for which ever method is used.

每个网关都有自己的方法将数据传输到应用程序服务器,尽管通过HTTP连接的XML或SOAP通常是最受欢迎的方法。 网关应提供某种方法使用过的API文档。

The application developer has to create a specific entry point in the system to handle messages received from the gateway; this is called the application URL. The process of mapping application URLs to the dedicated number or shortcode varies from one gateway to another. Some allow the configuration directly through the user account interface, such as Clickatell does. If this option isn’t available, we have to contact the technical support resource for the gateway and provide them with the application URL to be configured.

应用程序开发人员必须在系统中创建特定的入口点,以处理从网关收到的消息。 这称为应用程序URL。 从一个网关到另一个网关,将应用程序URL映射到专用号码或短代码的过程有所不同。 有些协议允许直接通过用户帐户界面进行配置,例如Clickatell。 如果此选项不可用,我们必须联系网关的技术支持资源,并向他们提供要配置的应用程序URL。

第2阶段–到应用服务器的SMS网关 (Stage 2 – SMS Gateway to Application Server)

The application owner then has to decide how to receive the data from the gateway. Clickatell allows you to specify this method inside the user account. If it’s not available, again you’ll need to contact the technical team of the particular gateway.

然后,应用程序所有者必须决定如何从网关接收数据。 Clickatell允许您在用户帐户中指定此方法。 如果不可用,则再次需要联系特定网关的技术团队。

The following code example shows you how Clickatell sends the data via the HTTP GET method and now you can receive it. Other gateways will provide a similar method as well.

以下代码示例向您展示Clickatell如何通过HTTP GET方法发送数据,现在您可以接收它了。 其他网关也将提供类似的方法。

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
<?php
$fromNo = $_GET["from"];
$toNo = $_GET["to"];
$message = $_GET["text"];
$msgID = $_GET["moMsgId"];

// Process the user command and generate output
...

And here’s an example where the data is POSTed as an XML document.

这是一个将数据作为XML文档发布的示例。

<?xml version="1.0"?>
<clickmo>
  <api_id>xxx</api_id>
  <moMsgId>fa6ba35b330ce1bc7e2008e5d92d57aa</moMsgId>
  <from>handset_number_here</from>
  <to>mo_number_here</to>
  <timestamp>2007-02-26 14:36:50</timestamp>
  <text>xxx</text>
  <charset>ISO-8859-1</charset>
  <udh></udh>
</clickmo>
<?php
$data = $_POST["data"];

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($data);

$fromNo = $xmlDoc->getElementsByTagName("from");
$fromNo = $fromNo->item(0)->nodeValue;

$toNo = $xmlDoc->getElementsByTagName("to");
$toNo = $toNo->item(0)->nodeValue;

$message = $xmlDoc->getElementsByTagName("text");
$message = $message->item(0)->nodeValue;

// Process User Message and Generate Response
...

The application needs to capture the incoming data using one of the available methods and process the user’s commands. In a simple scenario where user’s message is less than 160 characters, this code works perfectly. But what will happen if the message is more than 160 characters?

应用程序需要使用一种可用的方法来捕获传入的数据并处理用户的命令。 在用户消息少于160个字符的简单情况下,此代码可以完美工作。 但是,如果消息超过160个字符,将会发生什么?

Let’s assume a television station starts an advertising campaign where users can send advertisements as SMS. Once the SMS is sent, the user will receive a time period from the server where the advertisement will be displayed. Advertisements are text only and will be displayed on the bottom of the screen. Let’s also assume we have the predefined format for sending messages as shown below.

假设电视台开始了一项广告活动,用户可以在其中发送短信形式的广告。 一旦发送了SMS,用户将从服务器接收显示广告的时间段。 广告仅是文本,将显示在屏幕底部。 我们还假设我们具有用于发送消息的预定义格式,如下所示。

advertisement sports title your advertisement title message content of your advertisement

All of the content in the message appears as value pairs. advertisement will be the label and sports will be the value representing the advertising category. Then, title is the next label and its value will be the heading of the advertisement. message is the final label and will be the content the user wants to display in the advertisement.

消息中的所有内容均显示为值对。 advertisement将是标签,体育将是代表广告类别的值。 然后, title是下一个标签,其值将是广告的标题。 message是最终标签,它将是用户想要在广告中显示的内容。

Depending on the length of the title and message, this SMS can have more than 160 characters. Every time a lengthy message is received, the gateway splits it into multiple 160-character parts. But still we have to handle those 2 parts as a single message.

根据标题和消息的长度,此SMS可以包含超过160个字符。 每次收到冗长的消息时,网关都会将其拆分为多个160个字符的部分。 但是,我们仍然必须将这两个部分作为一条消息处理。

If we use the previous code examples to parse the messages, parts 1 and 2 of the same message would be handled as two different messages. And since both parts do not have the full command, the application would send back an error to the user. UDH is the solution to this problem.

如果使用前面的代码示例来解析消息,则同一消息的第1部分和第2部分将被视为两个不同的消息。 而且由于这两个部分都没有完整的命令,因此应用程序会将错误发送回给用户。 UDH是解决此问题的方法。

什么是UDH? (What is UDH?)

UDH stands for User Data Header. When we send a lengthy message, the sending device (mobile phone) splits the message and sends it as separate messages. UDH values will be assigned to beginning of each message part so that receiving device can identify them as belonging to a single message and recombine them.

UDH代表用户数据头。 当我们发送冗长的消息时,发送设备(手机)会拆分该消息并将其作为单独的消息发送。 UDH值将分配给每个消息部分的开头,以便接收设备可以将它们识别为属于单个消息并重新组合它们。

The UDH values for the earlier scenario would look like this:

早期方案的UDH值如下所示:

Part 1 - 05 00 03 CC 02 01
Part 2 - 05 00 03 CC 02 02

The last two hex values will be the most important values in the UDH. The second to last digit is 02 in the above codes and defines the number of parts in the message. So, this message was split into two parts. If three parts were available, then this value should be 03, and so on. The next digit defines the part number of the message. 01 means it’s the first part, 02 mean the second part, etc.

最后两个十六进制值将是UDH中最重要的值。 在上面的代码中,倒数第二个数字是02,它定义了消息中的部分数量。 因此,此消息分为两个部分。 如果有三个部分可用,则该值应为03,依此类推。 下一位数字定义消息的部件号。 01表示第一部分,02表示第二部分,依此类推。

Now we have what we need to know to process lengthier messages that were split up into multiple parts.

现在,我们需要了解处理更长的消息(这些消息分成多个部分)的知识。

<?php
$fromNo = $_GET["from"];
$toNo = $_GET["to"];
$message = $_GET["text"];
$msgID = $_GET["moMsgId"];
$udh = $_GET["udh"];

$total = 1;
$count = 1;
if ($udh) {
    $tmp = str_split($udh, 2);
    $total = hexdec($tmp[4]);
    $count = hexdec($tmp[5]);
}

if ($count != $total) {
    // Save the message fragment in database
    saveMessagePart($db, $from, $message, $udh);
}
else if ($total != 1) {
	$prevParts = getMessageParts($db, $from);
	$message = $prevParts . $message;
}

// process $message
...

Now that we can handle a full message and act on it, this completes this stage of the request cycle. The next two stages show how the response is sent back to the user through the gateway.

现在我们可以处理完整的消​​息并对其执行操作,从而完成了请求周期的这一阶段。 接下来的两个阶段显示了如何通过网关将响应发送回用户。

第3阶段和第4阶段–从应用服务器到SMS网关,从网关到用户 (Stage 3 and 4 – Application Server to SMS Gateway, Gateway to User)

In the previous stage I explained the process up to receiving messages from the gateway and processing it within your application. Once the request is processed and the response is generated, we need to send it back to the gateway.

在上一阶段中,我解释了从网关接收消息并在您的应用程序中对其进行处理的过程。 处理完请求并生成响应后,我们需要将其发送回网关。

Generally, SMS gateways provide a callback URL for us to pass the response data. You usually have to provide the receiver’s number, the sender’s number, the message content, and some sort of authentication information. The exact parameters will vary according to the gateway, but for Clickatell they are to, from, text, and api_id, username, and password.

通常,SMS网关为我们提供一个回调URL,以传递响应数据。 通常,您必须提供收件人的编号,发件人的编号,消息内容以及某种身份验证信息。 确切的参数会根据网关的不同而有所不同,但对于Clickatell,它们是tofromtextapi_idusernamepassword

<?php
$message = array(
    "to" => 942288345,
    "from" => 944488345,
    "text" => "sample message",
    "api_id" => API_KEY,
    "user" => "myUsername",
    "password" => "secret"
);

$apiUrl = "http://api.clickatell.com/http/sendmsg?";
foreach ($message as $parameter => $value ) {
    $apiUrl .= $parameter . "=" . urlencode($value) . "&";
}
$apiUrl = rtrim($apiUrl, "&");

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $apiUrl);

curl_exec($ch);
curl_close($ch);

The code sample above first encodes all of the parameters using urlencode() and appends them to the API’s callback URL. Then, we initialize a cURL request and call the URL. The response message has now been sent to the gateway, which concludes step 3.

上面的代码示例首先使用urlencode()对所有参数进行urlencode()然后将它们附加到API的回调URL。 然后,我们初始化一个cURL请求并调用该URL。 现在已将响应消息发送到网关,该消息到步骤3结束。

Step 4 is straightforward and we don’t have to do anything in this part of the process. The gateway is responsible for sending all the messages in the correct order to the users mobile.

第4步很简单,在此过程的这一部分中我们无需做任何事情。 网关负责将所有消息以正确的顺序发送给移动用户。

摘要 (Summary)

We started this tutorial by discussing what two-way SMS messaging is and why it is useful. Then, we discussed the process of two-way messaging through 4 major stages. Now you should be able to apply the concepts covered here with any given SMS gateway and implement two-way SMS in your PHP applications.

我们通过讨论什么是双向SMS消息传递以及为什么有用来开始本教程。 然后,我们讨论了通过四个主要阶段进行双向消息传递的过程。 现在,您应该能够在任何给定的SMS网关中应用此处介绍的概念,并在PHP应用程序中实现双向SMS。

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/implement-two-way-sms-with-php/

php实现双向链表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值