待发短信

在线客服
产品支持 短信宝客服
合作渠道 渠道合作
服务咨询

4001-021-502

工作时间

9:00-21:00

Drupal基于SMS Framework模块新增短信接口

Drupal是使用PHP语言编写的开源内容管理框架(CMF),它由内容管理系统(CMS)和PHP开发框架(Framework)共同构成,小编对他算是比较了解,今天就以新增短信接口为例带着大家进行二次开发,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。

本次新增接口是基于SMS Framework这个模块所开发的,所以在开发之前我们要确保Drupal和SMS Framework已经安装并且可以正常使用,如果未安装或不能正常使用都会导致短信发送失败。第一步首先在SMS Framework模块的modules文件夹下新建一个文件夹,取名为sms_smsbao,在sms_smsbao下新建两个文件,分别为sms_smsbao.info和sms_smsbao.module文件sms_smsbao.info中的内容为

?
1
2
3
4
5
6
7
8
9
10
11
12
name = SMS SMSBAO
description = Provides SMSBAO integration for SMS Framework.
core = 7.x
package = SMS Framework
dependencies[] = sms
configure = admin/smsframework/gateways
 
;Information added by Drupal.org packaging script on 2018-03-13
version = "7.x-1.x-dev"
core = "7.x"
project = "sms_smsbao"
datestamp = "1435220283"

sms_smsbao.module中的内容为

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
 
/**
 * @file
 * Provide SMSBAO(https://www.smsbao.com/) integration for SMS framework.
 */
 
/**
 * Implements hook_gateway_info().
 */
function sms_smsbao_gateway_info() {
  return array(
    'sms_smsbao' => array(
      'name' => 'SMS SMSBAO',
      'send' => 'sms_smsbao_send',
      'receive' => TRUE,
      'configure form' => 'sms_smsbao_admin_form',
      // 'send form' => 'sms_smsbao_send_form',
    )
  );
}
 
/**
 * Implements hook_send().
 */
function sms_smsbao_send($number$message$options array()) {
  $status = FALSE;
  $return_message = t("No message sent");
  $validation_message = t("No validation attempted");
  // Validate the message length
  if (strlen($message) > 160) {
    $return_message = t('The message you wanted to send is too long and cannot be sent.');
    return array(
      'status' => $status,
      'message' => $return_message,
      'variables' => NULL,
      'validation' => $validation_message,
    );
  }
  $data array(
    'm' => trim($number),
    'c' => trim($message),
  );
  $result = _sms_smsbao_gateway_request($data);
  $result array(
    'status' => $status,
    'message' => $return_message,
    'variables' => NULL,
  );
 
  return $result;
}
 
/**
 * Callback: sms_smsbao_admin_form.
 */
function sms_smsbao_admin_form($configuration) {
  $form array();
  $form['sms_smsbao_url'] = array(
    '#type' => 'textfield',
    '#title' => t('SMSBAO GATEWAY URL'),
    '#default_value' => isset($configuration['sms_smsbao_url']) ? $configuration['sms_smsbao_url'] : '',
    '#description' => t('The custom URL used to access your SMSBAO GATEWAY API. It often looks like "https://www.smsbao.com".'),
    '#required' => TRUE,
  );
 
  $form['sms_smsbao_cdkey'] = array(
    '#type' => 'textfield',
    '#title' => t('SMSBAO CDKEY'),
    '#default_value' => isset($configuration['sms_smsbao_cdkey']) ? $configuration['sms_smsbao_cdkey'] : '',
    '#description' => t('The SMSBAO CDKEY.'),
    '#required' => TRUE,
  );
 
  $form['sms_smsbao_password'] = array(
    '#type' => 'textfield',
    '#title' => t('SMSBAO Password'),
    '#default_value' => isset($configuration['sms_smsbao_password']) ? $configuration['sms_smsbao_password'] : '',
    '#description' => t('The password to access SMSBAO'),
    '#required' => TRUE,
  );
 
  $form['sms_smsbao_sign'] = array(
    '#type' => 'textfield',
    '#title' => t('SMSBAO SIGN'),
    '#default_value' => isset($configuration['sms_smsbao_sign']) ? $configuration['sms_smsbao_sign'] : '',
    '#description' => t('The SMSBAO SIGN.'),
    '#required' => TRUE,
  );
 
  return $form;
}
 
/**
 * Implements hook_form_alter().
 */
function sms_smsbao_form_sms_admin_gateway_form_alter(&$form, &$form_state) {
  $form['#validate'][] = 'sms_smsbao_admin_form_validate';
}
 
/**
 * Validate the admin form.
 */
function sms_smsbao_admin_form_validate($form, &$form_state) {
  $url $form_state['values']['sms_smsbao_url'];
  $data array(
    'u' => $form_state['values']['sms_smsbao_cdkey'],
    'p' => md5($form_state['values']['sms_smsbao_password']),
  );
 
  $params = drupal_http_build_query($data);
  $url =  $url.'/query?'.$params;
  $result file_get_contents($url);
}
 
/**
 * Request gateway.
 
 * @param array $data
 
 * @return object
 */
function _sms_smsbao_gateway_request($data array()) {
  $gateway = sms_gateways('gateways');
  $config $gateway['sms_smsbao']['configuration'];
  $list array(
    'u' => $config['sms_smsbao_cdkey'],
    'p' => md5($config['sms_smsbao_password']),
    'm' => $data['m'],
    'c' => '【'.$config['sms_smsbao_sign'].'】'.$data['c']
  );
  $params = drupal_http_build_query($list);
  $url $config['sms_smsbao_url'] . '/sms?' $params;
  $result =file_get_contents($url);
  return $result;
}

好了,经过以上的替换,短信宝的短信平台已经替换成功了,我们去进行发送测试:

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,并且免审核了,短信内容3~5秒就可送达。
开源插件

最新更新

电商类

CMS类

微信类

文章标签