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 yo开发者_JS百科u 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>
Just use $from = $_REQUEST['From'];
精彩评论