Twilio API - 存储来自传入文本消息的回复 [英] Twilio API - Storing Replies from Incoming Text Messages

查看:26
本文介绍了Twilio API - 存储来自传入文本消息的回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Twilio API 接收 SMS 文本消息.我想存储接收到的消息的编号和正文.它是在 xml php 页面中接收的,我想在不同的 php 页面中间使用它.我该怎么办?该消息是通过 Post 请求接收的,twilio 在收到消息后更新 xml php 文件.

I am using Twilio API to receive SMS text messages. I want to store the number and the body of the received message. It is being received in an xml php page, I want to use it in a middle of a different php page. How should I go about it? The message is being received via a Post request, twilio updates the xml php file once it is received.

这是xml php文件代码:

This is the xml php file code:

    <?php
         header("content-type: text/xml");
         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
        <Sms>Hello again, Dr. Evil</Sms>
    </Response>
    <?php
        $body = $_POST['Body'];
        $responder = $_POST['From'];
        if ($body) {  
        // if some response has been received, tell us what it is  
           // echo "<Body>".$body."</Body>"; <--wrong
           // echo "<Responder>".$responder."</Responder>"; <--wrong
        };
    ?>

最后几行中的if"语句似乎不起作用.我应该改用 javascript(&jquery) 吗?如何?我是新手,请多多关照...

The "if" statement in the last few lines doesn't seem to be working. Should I be using javascript(&jquery) instead? how? I'm a novice, so be kind...

谢谢!

更新 1:

我按照您的建议尝试保存到数据库,但仍然无法正常工作... :(

I tried saving to database as you suggested, and it still isn't working... :(

这是新代码:

    <?php
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
        <Sms>Hows it going, Dr. Evil</Sms>
    </Response>
    <?php
        $body = $_POST['Body'];
        $responder = $_POST['From'];
        if ($body) { 
            require_once "../includes/functions.php";
            connectDatabase();
            //storing message and sender in database                                                        
            mysql_query("INSERT INTO sms_received (responder, body) 
                        VALUES ('$responder', '$body')");
            mysql_close();
        };
    ?>

更新 2:

好的,我调试过了,上次我遇到了路径问题,但是这段代码现在可以运行了!!!

Okay, I debugged it, this last time I had a problem with the path, but this code is working now!!!

谢谢大家:-)

推荐答案

PHP 不会将有关文本消息的信息存储在任何地方,因此您将丢失它.您所描述的意味着您的 PHP 文件将被加载两次.第一次,Twilio 服务器将加载您的 PHP 文件并读取 Evil .

PHP won't store the information about the text message anywhere, so you are going to lose it. What you describe means that your PHP file will be loaded two times. The first time, the Twilio server will load your PHP file and read the Evil <Response>.

第二次,您将在 Web 浏览器中加载 PHP 文件.$_POST 变量取决于浏览器(以及 Twilio 请求)中的信息,因此这两种情况不会相同.这意味着您需要将数据保存在某处,以免丢失.您可以在 Twilio 发出请求时使用数据库或将其写入文本文件,然后稍后从文件或数据库加载数据.如果您收到不止一条传入的短信,这也很有帮助 - 您可以将所有短信都存储在文件或数据库中.

The second time, you will load the PHP file in your web browser. The $_POST variable depends on information in your browser (and in the Twilio request), so it won't be the same for both cases. That means you need to save the data somewhere, so it doesn't get lost. You can use a database, or write it to a text file, when Twilio makes the request and then load the data from the file or the database later. This also helps in case you receive more than one incoming text message - you can store all of them in the file or in the database.

这篇关于Twilio API - 存储来自传入文本消息的回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆