0
votes

I think I'm trying to do something relatively simple but I can't get it to work. Basically, I want a twilio 'menu'. E.g. user calls our number, we say press 1 for english 2 for chinese ... I created a gather twiml:

<Response>
<Gather numDigits="1" action="https://handler.twilio.com/twiml/XXX">
<Say voice="alice">For English, press 1 now.</Say>
<Say voice="alice"> For Chinese, press 2 now. </Say>
</Gather>
</Response>

This works, I get redirected to my second twiml (tested with a simple response-say twiml), but here is where it goes wrong. In my second script, if I add any php, it no longer works. Basically I want to redirect to another twiml to say the proper reply in the proper language depending on the dialed number. My twiml looks like this (so this is what I literally have in the twimlbin):

<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

$digits = $_REQUEST['Digits'];
switch ($digits) {
case 1:
   $response = new Twiml();
$response->redirect('http://pigeons.com/twiml.xml', ['method' => 'POST']);
echo $response;
    break;
case 2:
   $response = new Twiml();
$response->redirect('http://pigeons.com/twiml.xml', ['method' => 'POST']);
echo $response;
    break;
default:
     echo '<Response>';
    echo '<Say>Sorry, your response was invalid.</Say>';
    echo '</Response>';
}

?>

However nothing happens when I have this. I'm not excluding script errors but I then changed my twiml to this:

<?php
header("content-type: text/xml");
?>
<Response>
<Say><?php echo "Test"?></Say>
</Response>

However, I still hear nothing when i have this script, nor do i see anything in the debugger log. I'm completely new to Twilio so I might be missing something simple but I can't seem to figure it out.

So my question, why does this not work in twiml? And if possible, does my redirect script look ok?

1
Is your twiML on a server that can execute PHP? Is it returning the xml header <?xml version="1.0" encoding="utf-8"?>? You should use the Twiml() object to generate itAndy

1 Answers

1
votes

TwiML bins do not execute php. If you want to use php to generate TwiML on the fly then you need to host it on your own server.

You can generate dynamic TwiML from a TwiML bin but you need to use Mustache Templates.

Have a look at Twilio's article and the Mustache pages for more information.