使用twilio拨打数据库中取决于不同的多个号码

时间:2019-10-25 08:53:06

标签: php sql twilio

当传感器提交值时,我试图拨打多个电话号码。

传感器提交一个值和一个sensorid。当数据库中有多个联系人时,它必须呼叫所有这些号码。 此代码选择必须拨打的触发器和夹心号码。如果有2个号码,则必须同时拨打这两个号码。

$conn = mysqli_connect($servername, $username, $password, $dbname);

$sql = "SELECT * FROM alarmtriggers WHERE sensor_id = '$sensor_id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        /* Als waarde gelijk is aan */
        if ($row['type'] == 'vast') {
            if ($row['waarde'] = $val1) {

                // get the phonenumbers it needs to dial
                $sql2 = "SELECT telefoonnummer FROM alarmnummers WHERE sensor_id = '$sensor_id'";
                $result2 = $conn->query($sql2);

                if ($result2->num_rows > 0) {
                    while ($row2 = $result2->fetch_assoc()) {
                        $phonenumber = $row2["telefoonnummer"]; // phonenumbers it needs to dial
                // execute this link several times, depending on how many contacts are in the database 
                // mirandaleus.nl/includes/alarmbot/call.php?sensorid=$sensor_id&callto=$phonenumber
            }
        }
    }
}

call.php:

<?php

require "vendor/autoload.php";

use Twilio\Rest\Client;

$sid    = "mysid";
$token  = "mytoken";
$twilio = new Client($sid, $token);

$sensor_id = $_REQUEST["sensor_id"];
$callto = $_REQUEST["callto"];
$call = $twilio->calls
    ->create(
        $callto, // to
        "+12242631292", // from
        array("url" => "https://mirandaleus.nl/includes/alarmbot/includes/cas_xml.inc.php?sensor=$sensor_id")
    );

print($call->sid);

call_xml.inc.php:

<?php header('Content-type: text/xml'); ?>
<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = 'mydbname';

$sensor_id = $_GET['sensor'];
$conn = mysqli_connect($servername, $username, $password, $dbname);

$sql = "SELECT sensor_naam FROM sensoren WHERE sensor_id = '$sensor_id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $sensornaam = $row['sensor_naam'];
    }
}
?>
<Response>
    <Say voice="alice" language="nl-NL">
        Het alarm van sensor <?php echo $sensornaam ?> gaat af.
    </Say>
    <Pause length="1" />
    <Say voice="alice" language="nl-NL">
        Het alarm van sensor <?php echo $sensornaam ?> gaat af.
    </Say>
    <Pause length="1" />
    <Say voice="alice" language="nl-NL">
        Het alarm van sensor <?php echo $sensornaam ?> gaat af.
    </Say>
    <Pause length="1" />
    <Say voice="alice" language="nl-NL">
        Einde bericht
    </Say>
</Response>

因此,简而言之:它必须执行一个具有不同变量的链接(或脚本),具体取决于数据库中有多少

1 个答案:

答案 0 :(得分:0)

不需要为每个调用通过额外的HTTP请求来完成此操作,您可以在循环内直接进行一些小的修改就可以包含call.php脚本。 HTTP请求增加了“开销”,并减慢了速度,并且由于实际的Twilio API调用意味着已经为每个调用提出了一个HTTP请求,因此最好不要在每个请求之上再添加另一个。

直接包含脚本时,不需要从$ _REQUEST中获取其参数,而是可以直接访问包含脚本的当前范围内的所有变量。

应对此工作进行以下小修改:

主脚本:

if ($result2->num_rows > 0) {
  while ($row2 = $result2->fetch_assoc()) {
    $callto = $row2["telefoonnummer"]; // phonenumbers it needs to dial
    require '{path-from-this-location}/call.php';
  }
}

call.php:

<?php

require_once "vendor/autoload.php"; // require_once, so that this doesn’t crash when call.php 
                                    // itself gets included multiple times in the loop

use Twilio\Rest\Client;

$sid    = "mysid";
$token  = "mytoken";
$twilio = new Client($sid, $token);

// commented out, these variables are in the current scope inside the while loop already,
// and therefor accessible directly here
//$sensor_id = $_REQUEST["sensor_id"];
//$callto = $_REQUEST["callto"];

// rest from here on, same as before
$call = $twilio->calls(…);