我正在尝试将一个简单的电话号码传递给vxml块。如何将动态变量传递给它?
$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;
我试图将其转换为使用:
$string = '';
$string .= $to_call;
$string .= '';
etc...
但这似乎也无济于事。我只想在{my_phone_number_here}显示一个php变量,我错过了哪些不能让它正常工作?
修改
显示的代码现在呈现电话号码,但我的电话实际上从未连接过。拨打电话时,您将列出音频,然后按一个提示,然后播放感谢通知 - 但随后它会响一会儿,并且呼叫会下降。仍然需要与xml的东西。有什么想法吗?
答案 0 :(得分:2)
您可以将其加载到Parser(特别是DOMDocument)中,然后使用->setAttribute()
从那里更改它:
$transfer->item(0)->setAttribute('dest', $telephone_number);
简单示例:
// 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();
或者只是简单地替换并将变量放入其中并进行插值:
$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;