2012-03-03 57 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

相關問題