【问题标题】:How to send bulk sms in twilio api without for loop如何在没有for循环的情况下在twilio api中发送批量短信
【发布时间】:2018-04-12 06:44:15
【问题描述】:

我正在尝试通过 twilio Api 发送群发短信。是否有任何方法可以在单个 API 请求中传递所有电话号码的数组。

【问题讨论】:

  • 这样,我们无法通过单个 API 调用发送消息。

标签: php twilio twilio-api twilio-php


【解决方案1】:

首先,您需要正确配置您的 twilio 号码以进行通知。然后您可以使用以下代码发送批量短信。

$message = 'Any text message';
$to = array();
foreach ($users as $user) { 
    $to[] = '{"binding_type":"sms", "address":"'.$user->phone_number.'"}';
}

$sid    = 'TWILIO_ACCOUNT_SID';
$token  = 'TWILIO_AUTH_TOKEN';
$services_id = 'TWILIO_SERVICE_ID';
$twilio = new Client($sid, $token);


$notification = $twilio
->notify->services($services_id)
->notifications->create([
    "toBinding" => $to,
    "body" => $message
]);

【讨论】:

    【解决方案2】:

    如果其他人像我一样在从 PHP array() 准备 toBinding 参数时遇到麻烦,这里有一个示例:

    <?php
    require_once '/path/to/vendor/autoload.php';
    
    use Twilio\Rest\Client;
    
    $accountSid = "your_account_sid";
    $authToken = "your_auth_token";
    $serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    
    $client = new Client($accountSid, $authToken);
    
    $recipients = array($num1, $num2, ...); // Your array of phone numbers
    
    $binding = array();
    foreach ($recipients as $recipient) { 
        $binding[] = '{"binding_type":"sms", "address":"+1'.$recipient.'"}'; // +1 is used for US country code. You should use your own country code.
    }
    
    $notification = $client
    ->notify->services($service_sid)
    ->notifications->create([
        "toBinding" => $binding,
        "body" => $text
    ]);
    ?>
    

    【讨论】:

      【解决方案3】:

      这里是 Twilio 开发者宣传员。

      是的,现在有!它被称为passthrough API(因为它允许您通过许多不同的消息传递系统并发送批量消息。它是Notify API 的一部分,您可以使用它来发送批量 SMS 消息。您需要设置消息传递服务和控制台中的通知服务,然后您可以使用以下代码:

      <?php
      // NOTE: This example uses the next generation Twilio helper library - for more
      // information on how to download and install this version, visit
      // https://www.twilio.com/docs/libraries/php
      require_once '/path/to/vendor/autoload.php';
      
      use Twilio\Rest\Client;
      
      // Your Account SID and Auth Token from https://www.twilio.com/console
      $accountSid = "your_account_sid";
      $authToken = "your_auth_token";
      
      // your notify service sid
      $serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
      
      // Initialize the client
      $client = new Client($accountSid, $authToken);
      
      // Create a notification
      $notification = $client
          ->notify->services($serviceSid)
          ->notifications->create([
              "toBinding" => [
                  '{"binding_type":"sms", "address":"+15555555555"}',
                  '{"binding_type":"sms", "address":"+12345678912"}'
              ],
              "body" => "Hello Bob"
          ]);
      

      结帐the documentation on sending multiple messages with the Notify passthrough API for all the details

      【讨论】:

      • 您如何使用此方法(将多个收件人的单个 API 调用作为由 toBinding 参数标识的数组)并能够取回每条已发送消息的 Twilio 唯一标识符,以便以后可以通过 twilio 回调更新发送的短信是否链接回我的数据库条目?
      • @philnash 你能在这里查看问题吗stackoverflow.com/questions/65789251/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多