1

我有一个 Twilio 帐户,并且正在为我的 Drupal 站点编写一个群发短信模块。在模块的开头,我使用以下代码设置了 Twilio 客户端:

$path = drupal_get_path("library", "twilio");
require($path . "twilio/Services/Twilio.php");
$accountSID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$authToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$client = new Services_Twilio($accountSID, $authToken);
$from = "xxxxxxxxxx";

myModule_submit() 查询数据库中的电话号码,并通过上面引用的 Twilio PHP 库将它们发送出去。我正在使用在 Twilio 网站上找到的类似代码(http://www.twilio.com/docs/howto/sms-notifications-and-alerts)。问题是当我填写要发送的 SMS 消息的表格并按提交时,我收到以下错误消息:

注意:未定义的变量:myModule_submit() 中的客户端(/var/www/erosas/anysite.com/sites/all/modules/myModule/myModule.module 的第 128 行)。注意:试图在 myModule_submit() 中获取非对象的属性(/var/www/erosas/anysite.com/sites/all/modules/myModule/myModule.module 的第 128 行)。注意:试图在 myModule_submit() 中获取非对象的属性(/var/www/erosas/anysite.com/sites/all/modules/myModule/myModule.module 的第 128 行)。

提交函数为:

function myModule_submit($form, &$form_state){

// Retrieve the values from the fields of the custom form
$values = $form_state['values'];


// Use Database API to retrieve current posts.
$query = db_select('field_data_field_phone_number', 'n');
$query->fields('n', array('field_phone_number_value'));

// Place queried data into an array
$phone_numbers = $query->execute();

$body = $values['sms_message'];

// Iterate over array and send SMS 
foreach($phone_numbers as $number){
    $client->account->sms_messages->create($from, $number, $body); // This is line 128
}

}

任何想法/帮助将不胜感激,我尝试在此站点和 Google 上搜索答案,但没有出现任何特定于 Drupal 的内容。

4

1 回答 1

2

$client 对象对提交函数不适用。尝试输入相同的代码

$path = drupal_get_path("library", "twilio");
require($path . "twilio/Services/Twilio.php");
$accountSID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$authToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$client = new Services_Twilio($accountSID, $authToken);
$from = "xxxxxxxxxx";

在提交功能的开头。

   function pulsesurf_submit($form, &$form_state){
     $path = drupal_get_path("library", "twilio");
     require($path . "twilio/Services/Twilio.php");
     $accountSID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
     $authToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
     $client = new Services_Twilio($accountSID, $authToken);
     $from = "xxxxxxxxxx";

    // Retrieve the values from the fields of the custom form
    $values = $form_state['values'];


    // Use Database API to retrieve current posts.
    $query = db_select('field_data_field_phone_number', 'n');
    $query->fields('n', array('field_phone_number_value'));

    // Place queried data into an array
    $phone_numbers = $query->execute();

    $body = $values['sms_message'];

    // Iterate over array and send SMS 
    foreach($phone_numbers as $number){
        $client->account->sms_messages->create($from, $number, $body); // This is line 128
    }
...

更好地制作一些不带参数的包含函数,简单地包含库文件并设置令牌/sid以便于使用。

顺便说一句,您网站的域在错误消息中。

于 2012-05-13T02:39:58.973 回答