Sending SMS and Making Calls from your Website Programatically - Part 2 - Handling Incoming Calls and Messages

This post assumes that you have read the part 1 of this series, which covered making calls and sending messages programatically from twilio. This part will cover the handling of incoming messages and phone calls.


  1. Login to your twilio account and goto the 'Numbers' section. Here you can see all the numbers that you have registered with twilio and those you can use with twilio.
  2. Click on any of the listed numbers
  3. On the following page, you will see a textbox under Voice and another text box under Messaging section. Enter the URL to your page in this box so that twilio will invoke this page on receiving a call or message to the number you have selected.(Like a Landing URL or Call back reference). You may set the method to invoke your page(GET/POST), in the following drop down box.
  4. You may write the code to handle your call/message in this landing page. Try
    file_put_contents("test.txt",json_encode($_REQUEST));
    in your landing script so that you may see the various parameters passed to your page from twilio upon receiving a call or message, and you may use these parameters according to your logic.
  5. You may optionally echo an xml (twiml) formatted string for twilio to respond to the incoming call or message. For example,  
    $toPhone = $_REQUEST['From']; //sender's number
    $content = "Thank you for your response. Your sms response : ".$_REQUEST['Body'];
    //xml response
    echo '<?xml version="1.0" encoding="UTF-8"?><Response><Sms from="$twilio_number" to="$toPhone">'.$content.'</Sms></Response>';
    will acknowledge the sms sender with another message. 
PS: If you are having a trial account with twilio, you will only have limited permissions. You can call/text the numbers verified with twilio only.

No comments:

Post a Comment