简体   繁体   English

使用PHP和Twilio提取和存储SMS号码

[英]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. 因此,我刚开始使用Twilio(并且我的PHP有点生锈),但是当前代码会根据是否为您提供正确的数据来对文本进行数据响应,否则,它只是要求您重试一次。 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. 但是,我希望做的是提取传入的SMS文本的数量并将它们暂时存储在Cookie中,这样我就可以根据其先前的响应获得不同的响应。

Does that make sense? 那有意义吗?

Yes! 是! Twilio makes this really simple. Twilio使这一过程变得非常简单。 Any cookies you set will be saved between two numbers (your incoming phone number and the sender). 您设置的任何Cookie都将保存在两个数字之间(您的传入电话号码和发件人)。 All the code and explanation is here: http://www.twilio.com/docs/quickstart/sms/tracking-conversations 所有代码和解释都在这里: 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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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