Вопрос

I'm using the Twilio API to send broadcast SMS messages based on an approved number. The problem in which I have encountered is not being able to successfully run a query pulling the phone information from a database and enter it into the API for each results that meet the query criteria to send an SMS to. The below is what I have so far.

    //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 . " ";}
Это было полезно?

Решение

Ok so first of all you are not executing valid XML on the error condition e.g. if the user is not a trusted number. If you want to reply to the texter that the number is unauthorised you would need to do:

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";

I'm not sure what you are trying to do with the query - why is it in an array? And I don't know why you then have a recipients array - surely this is what you are pulling in from the database? You need to do:

$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']); 
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top