Book Image

Twilio Cookbook: Second Edition

By : Roger Stringer
Book Image

Twilio Cookbook: Second Edition

By: Roger Stringer

Overview of this book

Table of Contents (20 chapters)
Twilio Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding tracking for each user


We want to be able to track each user's response to the survey we sent and this is how we will do that.

In the previous recipe, we stored the phone number, survey ID, and the unique session ID for each person we sent the survey to.

Now we want to store each user's response for tracking later.

Getting ready

The complete source code for this recipe can be found in the Chapter3/ folder.

How to do it...

Let's set up tracker.php that listens for responses from our subscribers using the following steps:

  1. On your web server, create a file and name it tracker.php with the following code:

    <?php
    include("config.php");
    include("pdo.class.php");
    $pdo = Db::singleton();
    if( isset($_POST['Body']) ){
      $phone = $_POST['From'];
      $phone = str_replace('+','',$phone);
      $action = strtolower($_POST['Body']);
      $sid = $_POST['SmsSid'];
      $sql = "UPDATE responses SET answer='{$action}' WHERE phone_number='{$phone}' AND sms_sid='{$sid}'";
      $pdo->exec( $sql );
      $msg = "Your answer...