2

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?

Kevin
  • 41,694
  • 12
  • 53
  • 70
MrTechie
  • 1,797
  • 4
  • 20
  • 36

1 Answers1

2

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;
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Derp! Thanks! That indeed does it, don't know why I didn't think about doing it like that. Although I have another issue. $xml->asXML() seems to add one more line after the xml and creates an issue and won't place the call. Any ideas how I can clean that last line? – MrTechie Nov 12 '14 at 03:39
  • Well, it doesn't work as I expect it to. Weird! If I copy and paste the rendered xml into a file, and try it, it works. But if I try to load it dynamically (like I am) it never actually connects the call. Any thoughts? – MrTechie Nov 12 '14 at 03:45
  • 1
    @MrTechie can you give some context here? what is it really about. how are you using this xml? anyways, consider putting an `exit;` in the last line of your code. – Kevin Nov 12 '14 at 03:51
  • User calls a number, database figures out who user is, gets next queued number to be called. Renders vxml which then the user interacts with on a normal phone (or cell phone). Prompts user "Are you ready to make the call?" User enters 1 on their phone (the number that appears within the xml) and then call is supposed to be connected between user and the person the user is calling. All of that works except the actual call connection. Quick 1/2 second ring and then the call drops on the users side. Call is never connected. – MrTechie Nov 12 '14 at 03:57
  • 1
    @MrTechie so basically, this xml is used as a request to make a call is that right? do you transmit it thru something like `curl`? or an SDK of some sort? make you need also to check that the response pertinent to this request. maybe there is an error that would give a hint why it isn't working – Kevin Nov 12 '14 at 04:01
  • No no curl with this at all. It resides on a server and a 3rd party service provider sends the inbound call to this php script to figure out what it's supposed to do (ie: make the call connection happen) Nothing is showing up in error logs at all with this. Seems like the only difference I can see is if one file is named ".xml" it works, but if it's ".php" it doesn't seem to work even tho it is rendering xml/vxml. And I do have the headers set to header('Content-type: text/xml'); – MrTechie Nov 12 '14 at 04:07
  • @MrTechie do you have other outputting operations below the `echoing` of that xml? try to put `exit;` after echo – Kevin Nov 12 '14 at 04:12
  • @MrTechie hey don't leave me hanging :D lol whats the reason, i get to learn from this also haha – Kevin Nov 12 '14 at 04:12