duanmao1975 2012-05-12 22:07
浏览 38
已采纳

Drupal 7无法识别自定义模块中的Twilio客户端

I have a Twilio account and I am writing a mass text message module for my Drupal site. At the beginning of the module I have set up the Twilio client with the following code:

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

The myModule_submit() queries the database for phone numbers and sends them out via the Twilio PHP libraries referenced above. I am using code found on the Twilio site for something similar here(http://www.twilio.com/docs/howto/sms-notifications-and-alerts). The problem is when I fill in the forms for the SMS message to be sent out and press submit I get the following error message:

Notice: Undefined variable: client in myModule_submit() (line 128 of /var/www/erosas/anysite.com/sites/all/modules/myModule/myModule.module). Notice: Trying to get property of non-object in myModule_submit() (line 128 of /var/www/erosas/anysite.com/sites/all/modules/myModule/myModule.module). Notice: Trying to get property of non-object in myModule_submit() (line 128 of /var/www/erosas/anysite.com/sites/all/modules/myModule/myModule.module).

The submit function is:

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
}

}

Any thoughts/help would be greatly appreciated, I tried searching this site and Google for an answer, but nothing specific to Drupal came up.

  • 写回答

1条回答 默认 最新

  • douzhouhan4618 2012-05-13 02:39
    关注

    $client object is n/a to the submit function. Try putting the same code

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

    in the beginning of the submit function.

       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
        }
    ...
    

    Better making some include function without arguments the simply includes the library files and sets the tokens/sid for ease of use.

    and btw, your site's domain is in the error message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低