2012-03-03 27 views
2

我使用Twilio API发送根据经批准的数目的广播短信MySQL查询Twilio短信电话。我遇到的问题是无法成功运行从数据库中拉取手机信息的查询,并将其输入到API中以符合发送SMS的查询条件的每个结果。以下是我到目前为止。如何使用基于结果

//Include the PHP TwilioRest library 
    require "Services/Twilio.php"; 

    //Set our AccountSid and AuthToken 
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXX"; 

    $AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYY"; 

    //Instantiate a new Twilio Rest Client 
    $client = new Services_Twilio($AccountSid, $AuthToken); 

//Trusted numbers that we want to be able to send a broadcast 

    $trusted = array("+33333333333" => "ApprovedAdmin1"); 

//An unauthorized user tried to send a broadcast. 

    if(!$name = $trusted[$_REQUEST['From']]) 

    {header("content-type: text/xml");echo "\n";echo "Unauthorized broadcaster, contact an admin for access.";exit(0);} 

//These are the recipients that are going to receive the broadcasts 

//database user credentials 
    `$user = "user";` 
    `$password = "password";` 
    `$database = "database";` 
    `$host = "localhost";` 
//connect to the database 
    `$DB = mysql_connect($host, $user, $password) or die("Can't connect to database.");` 
    `@mysql_select_db($database) or die("Unable to select database");` 

//Used to query database for only user phone numbers who accept texts 

    $recipients = array (mysql_query("SELECT phone FROM sms WHERE (accept_text = 'Y')")); 

//I have commented this out to try to get the query to work. The below recipients array does work and even lists the names of the user in the SMS. 

    //$recipients = array("+22222222222" => "testuser1"); 

//Grab the message from the incoming SMS 

    $msg = $_REQUEST['Body']; 

// Iterate over all our recipients and send them the broadcast 

    //foreach ($recipients as $number => $name) { 

//Send a new outgoinging SMS by POSTing to the SMS resource 

    $sms = $client->account->sms_messages->create("3333333333",$number,"$name, " . $msg);echo $name . " ";} 

回答

0

好了,所以首先你是不是在错误条件下执行有效的XML例如所有的如果用户不是受信任的号码。如果您想回复该号码是非法的,你needle。需要做:

header("content-type:text/xml"); 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
echo "<Response>\n<Sms>Unauthorized broadcaster, contact an admin for access.</Sms>\n</Response"; 

我不知道你正在尝试与查询做 - 为什么是一个数组?我不知道为什么你有一个收件人数组 - 这肯定是你从数据库中拉入的东西?你需要做的:

$result = mysql_query("SELECT phone FROM sms WHERE accept_text = 'Y'"); 
while($row = mysql_fetch_assoc($result)) { 
    $client->account->sms_message->create("3333333333", $row['phone'], $_REQUEST['Body']); 
} 
+0

XML的有效性工作正常。结果变量不从表格中提取或响应任何数字。如果我运行直接连接到数据库的查询结果拉动罚款。 – 2012-03-03 17:23:32

+0

如果您尝试使用TwiML来响应用户,那么您的XML不正确。这是无效的TwiML。 – christophmccann 2012-03-03 17:25:52

+0

我正在使用Twilio Rest。如果数组是硬编码的,则响应不会从数据库结果本身发送。 – 2012-03-03 17:27:29

相关问题