简体   繁体   English

如何使用 Twilio API 将 SMS 发送到 PHP 阵列

[英]How to send SMS to a PHP array with Twilio API

Using the Twilio API, I've got my PHP functioning to send to one phone number, and can successfully send.使用 Twilio API,我的 PHP 可以发送到一个电话号码,并且可以成功发送。 We're looking to send to multiple numbers from one request and to do so, I've set up an array of numbers to iterate through, however, I keep getting a 500 error when I attempt to send the message by hitting the URL.我们希望从一个请求发送到多个号码,为此,我设置了一组数字进行迭代,但是,当我尝试通过点击 URL 发送消息时,我不断收到 500 错误。 Below is the file I'm working with.下面是我正在使用的文件。

Running PHP 7.2 on a Linux server.在 Linux 服务器上运行 PHP 7.2。 I'm running CentOS 7.7 and Apache 2.4.43 if that matters at all.如果这很重要,我正在运行 CentOS 7.7 和 Apache 2.4.43。

// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXX';
$token = 'XXXXXXXX';
$client = new Client($sid, $token);

$a = array('+15555555555', '+15555555556');

$bodyTxt = “This is a test of sending the text message to multiple phone numbers.”
// Use the client to do fun stuff like send text messages!

foreach ($a as $v) {
    $message = $twilio->messages
        $client->messages->create($v, // to
                           [
                               "body" => $bodyTxt,
                               "from" => "+15555555557",
                           ]
                  );
        print($message->sid);
}
);

I'm not super familiar with PHP as I'm mostly in marketing, but I'm deputizing as developer in these crazy times because I know just enough to be dangerous.我对 PHP 不是很熟悉,因为我主要从事营销工作,但我在这些疯狂的时期担任开发人员的职务,因为我知道足够危险。 I'm thinking it is something with the foreach section, as that's the only piece that has changed from the single send.我认为这与 foreach 部分有关,因为这是与单次发送不同的唯一部分。

Any help is appreciated!任何帮助表示赞赏!

Figured it out thanks to the help from @LuisE, I went through and figured out where I was missing the semicolons after the array, the $bodyTxt.在@LuisE 的帮助下弄清楚了,我通过并找出了我在数组后面缺少分号的地方,$bodyTxt。 and the $message = $twilio->messages.和 $message = $twilio->messages。

// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXXXX';
$token = 'XXXXXXXXX';
$client = new Client($sid, $token);

$a = array('+15555555555', '+15555555556');

$bodyTxt = 'This is a test of sending the text message to multiple phone numbers.';
// Use the client to do fun stuff like send text messages!

foreach ($a as $v) {
    $message = $twilio->messages;
        $client->messages->create($v, // to
                           [
                               "body" => $bodyTxt,
                               "from" => "+15555555557",
                           ]
                  );
        print($message->sid);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM