1

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

2 Answers 2

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.

6
  • Okay then, thanks for the explanation! I think I get it now, it is twilio that are running this page, so I should let it execute just that once and make sure to save the data they send me, and get this output from somewhere else (eg db/text file). Dec 22, 2011 at 19:16
  • Can I redirect to another webpage and keep doing things after twilio run this file? Dec 22, 2011 at 19:25
  • Who would you be redirecting, your own request to the website, or the Twilio server? Dec 22, 2011 at 19:47
  • I want to redirect my own request, but maybe it is not possible? I am implementing the db solution, maybe I'll just look for db sms entries from the other page and leave this xml alone Dec 22, 2011 at 20:17
  • Hi Lucy, If you are redirecting your own request, you may just want to create a new .php file at a different URL and then load that other file directly. Dec 22, 2011 at 23:20
0

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.

3
  • I was not trying to echo it back to twilio. I was thinking to echo it to the page so that I can send this xml to another php page I have things running on. How hould I do that then? either through a DB? or a session? Dec 22, 2011 at 19:01
  • This page is your response you're sending to Twilio, so echoing it is sending it to Twilio. Session won't work, as the session would be Twilio's session cookie, not yours. You need a database.
    – ceejayoz
    Dec 22, 2011 at 19:09
  • Ok I see now, I'll try that. Thanks! Dec 22, 2011 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.