Twilio API - Storing Replies from Incoming Text Messages

吃可爱长大的小学妹 提交于 2019-12-11 07:11:16

问题


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.

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
        };
    ?>

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

Thanks!

Update1:

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

Here's the new code:

    <?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();
        };
    ?>

Update2:

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

Thank you all :-)


回答1:


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

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.




回答2:


You're not storing it anywhere in this code. You're just outputting it. You'll need to save it to a database of some sort instead of echoing it back to Twilio (which is going to ignore anything outside of a <Response> block.

JavaScript won't work in this for two reasons - it's an XML file, and Twilio doesn't execute JS code.



来源:https://stackoverflow.com/questions/8608306/twilio-api-storing-replies-from-incoming-text-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!