简体   繁体   中英

Pulling and storing SMS numbers with PHP and Twilio

So I'm new to using Twilio (and my PHP is a bit rusty) but currently the code responds to text with data depending on if you give it the right data, otherwise, it just asks you to try it again. So that's the bit that works. However, what I'm hoping to do, is to pull the numbers of incoming SMS texts and temporarily store them in a cookie so I can can have a different response based on their previous responses.

Does that make sense?

Yes! Twilio makes this really simple. Any cookies you set will be saved between two numbers (your incoming phone number and the sender). All the code and explanation is here: http://www.twilio.com/docs/quickstart/sms/tracking-conversations

Here's a quick snippet from that page which should do what you want:

<?php

    // start the session
    session_start();

    // get the session varible if it exists
    $counter = $_SESSION['counter'];

    // if it doesnt, set the default
    if(!strlen($counter)) {
        $counter = 0;
    }

    // increment it
    $counter++;

    // save it
    $_SESSION['counter'] = $counter;

    // make an associative array of senders we know, indexed by phone number
    $people = array(
        "+14158675309"=>"Curious George",
        "+14158675310"=>"Boots",
        "+14158675311"=>"Virgil",
    );

    // if the sender is known, then greet them by name
    // otherwise, consider them just another monkey
    if(!$name = $people[$_REQUEST['From']])
        $name = "Monkey";

    // output the counter response
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
    <Sms><?php echo $name ?> has messaged <?php echo $_REQUEST['To']." ".$counter ?> times</Sms>
</Response>

只需使用$ from = $ _REQUEST ['From'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM