【问题标题】:Drupal 7 Not Recognizing Twilio Client in Custom ModuleDrupal 7 在自定义模块中无法识别 Twilio 客户端
【发布时间】:2012-05-12 22:07:32
【问题描述】:

我有一个 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 的内容。

【问题讨论】:

    标签: php drupal foreach submit twilio


    【解决方案1】:

    $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以便于使用。

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

    【讨论】:

    • 谢谢!我不再收到错误消息,但它重定向到用户帐户页面,没有确认正在发送的消息。我尝试使用 drupal_set_message(),但这不起作用。有什么建议吗?
    • 你在提交函数中使用了 drupal_goto() 吗?除非您覆盖,否则不应将用户重定向到用户页面。 drupal_goto() 之前的 drupal_set_message() 大部分时间都对我有用。
    • 不,我没有使用 drupal_goto(),当我在 sms_messages-> 中输入电话号码作为字符串时,代码的查询部分似乎有问题create() 参数代替 $to 它可以工作,否则它会重定向到用户页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多