create vxml with php dynamically

519 views Asked by At

I am trying to pass a simple phone number to a vxml block. How can I pass a dynamic variable into this?

$my_phone_number_here = '12197719191';

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1">
<menu dtmf="true">  
<prompt>
<audio src="http://my.site.com/app/service-interaction-center.mp3"/>
</prompt>
<choice dtmf="1" next="#sales"/>
</menu>
<form id="sales">
<block>
<audio src="http://my.site.com/app/service-interaction-center-thank-you.mp3"/>
</block>
<transfer name="MyCall" dest="tel:+{$my_phone_number_here}" bridge="true" connecttimeout="20s"/>
</form>
</vxml>
XML;

I have tried to convert that into using:

$string = '';
$string .= $to_call;
$string .= '';
etc...

But that didn't seem to work either. I would just like to get a single php variable to show up at {my_phone_number_here}, what am I missing that won't allow this to work correctly?

EDIT:

The shown code now renders with the phone number in place but my call never actually gets connected. When the call is placed, you get to listed to the audio, and press a prompt, and then the thank you announcement plays - but then it rings for a split second and the call drops. Still has to be something with xml. Any thoughts?

1

There are 1 answers

8
Kevin On BEST ANSWER

You could load it into a Parser (DOMDocument in particular), and change it from there using ->setAttribute():

$transfer->item(0)->setAttribute('dest', $telephone_number);

Simple Example:

// use the parser
$dom = new DOMDocument;
$dom->loadXML($string);
$xpath = new DOMXpath($dom);

// setup those values
$number = 123131;
$telephone_number = 'tel:+' . $number;
// target that element
$transfer = $xpath->query('/vxml/form[@id="sales"]/transfer');
// set the value
$transfer->item(0)->setAttribute('dest', $telephone_number);
// show output
echo $dom->saveXML();

Or just simply substitute and put a variable inside and let it be interpolated:

$my_phone_number_here = 123456879;
$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
    <vxml version = "2.1">
        <menu dtmf="true">
            <prompt>
                <audio src="http://my.site.com/app/service-interaction-center.mp3"/>
            </prompt>
            <choice dtmf="1" next="#sales"/>
        </menu>
        <form id="sales">
            <block>
                <audio src="http://my.site.com/app/service-interaction-center-thank-you.mp3"/>
            </block>
            <transfer name="MyCall" dest="tel:+{$my_phone_number_here}" bridge="true" connecttimeout="20s"/>
        </form>
    </vxml>
XML;

echo $string;