我正在尝试部署一个用PHP编写的短信应用程序,我正在Heroku上部署它,并使用Twilio发送消息。当有人在我的Twilio号码上发送消息时,这个应用程序会对该消息发送适当的回复。我在这个应用程序中面临着一个开关语句的问题。我是PHP编码的新手,我无法做到这一点。我在下面粘贴我的代码。请帮我修一下。提前谢谢你。
<?php
session_start();
header("content-type: text/xml");
switch ($answer) {
case 'headache' : {
print('OTC Crocin may help!');
break;
case 'stomachache' : {
print('OTC Tylenol may help!');
break;
case 'cough' : {
print('OTC Robitussin Cough may help!');
break;
case 'cold' : {
print('OTC Aspirin may help!');
break;
case 'vomiting' : {
print('OTC Pepto-Bismol may help!');
break;
case 'headache doctor' : {
print('Dr. Lorem Ipsum - Here is the address! Call at +1 xxx xxx xxxx');
break;
case 'stomachache doctor' : {
print('Dr. Lorem Ipsum - Here is the address! Call at +1 xxx xxx xxxx');
break;
case 'cough doctor' : {
print('Dr. Lorem Ipsum - Here is the address! Call at +1 xxx xxx xxxx');
break;
case 'cold doctor' : {
print('Dr. Lorem Ipsum - Here is the address! Call at +1 xxx xxx xxxx');
break;
case 'vomiting doctor' : {
print('Dr. Lorem Ipsum - Here is the address! Call at +1 xxx xxx xxxx');
break;
}
$from = $_POST['From'];
$answer = $_POST['Body'];
$reply = array();
echo "<?xml version='"1.0'" encoding='"UTF-8'"?>'n";
?>
<Response>
<Sms>
<?php
if(is_array($reply)){
foreach($reply as $key => $value){
echo $value;
}
}
else{
echo $reply;
}
?>
</Sms>
</Response>
这里是Twilio开发者布道者。
您正在响应传入的SMS消息,但是您正在使用<Sms>
TwiML动词。<Sms>
仅用于语音通话时发送短信
相反,您要使用<Message>
TwiML动词,用于回复SMS消息。因此,代码的最后一部分应该看起来像:
<Response>
<Message>
<?php
if(is_array($reply)){
foreach($reply as $key => $value){
echo $value;
}
}
else{
echo $reply;
}
?>
</Message>
</Response>