如何使用Twilio API发送短信到PHP数组?

问题描述 投票:0回答:1

使用Twilio的API,我已经让我的PHP功能发送至一个电话号码,并且可以成功发送。我们希望通过一个请求发送多个号码,为此,我设置了一个号码数组来进行迭代,然而,当我试图通过点击URL发送消息时,我一直收到一个500错误。下面是我正在使用的文件。

在Linux服务器上运行PHP 7.2。我运行的是 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);
}
);

我对 PHP 不是很熟悉,因为我主要是做市场营销的,但在这个疯狂的时代,我作为开发人员,因为我知道的足够多,所以很危险。我想这是foreach部分的问题,因为那是唯一与单次发送有变化的部分。

感谢任何帮助

php arrays sms twilio-php
1个回答
0
投票

多亏@LuisE的帮助,我终于想明白了。我仔细检查了一下,发现我在数组、$bodyTxt和$message = $twilio->消息后面少了分号。

// 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);
}
© www.soinside.com 2019 - 2024. All rights reserved.