You are here

function sms_simplegateway_send in SMS Framework 6

Send a message

Parameters

$number: MSISDN of message recipient. Expected to include the country code prefix.

$message: Message body text.

$options: Options array from SMS Framework.

Return value

Response array.

1 string reference to 'sms_simplegateway_send'
sms_simplegateway_gateway_info in modules/sms_simplegateway/sms_simplegateway.module
Implement hook_gateway_info()

File

modules/sms_simplegateway/sms_simplegateway.module, line 187
Simple gateway module for Drupal SMS Framework. Outbound+Inbound

Code

function sms_simplegateway_send($number, $message, $options) {

  // Get config
  $gateway = sms_gateways('gateway', 'simplegateway');
  $config = $gateway['configuration'];

  // Prepare the URL and get the method
  $url_base = $config['sms_simplegateway_base_url'];
  $method = $config['sms_simplegateway_method'];

  // Lets specify a gw number
  $sender = '';
  if (array_key_exists('sender', $options)) {
    $sender = $options['sender'];
  }

  // Prepare required arguments
  $params = array();
  $user_field = $config['sms_simplegateway_user_field'];
  $user = $config['sms_simplegateway_user_value'];
  if (!empty($user_field)) {
    $params[$user_field] = $user;
  }
  $pass_field = $config['sms_simplegateway_pass_field'];
  $pass = $config['sms_simplegateway_pass_value'];
  if (!empty($pass_field)) {
    $params[$pass_field] = $pass;
  }
  $sender_field = $config['sms_simplegateway_sender_field'];
  if (!empty($sender_field) && !empty($sender)) {
    $params[$sender_field] = $sender;
  }
  $number_field = $config['sms_simplegateway_number_field'];
  $params[$number_field] = $number;
  $message_field = $config['sms_simplegateway_message_field'];
  $params[$message_field] = $message;

  // Prepare the query string
  // Note that we are forcing http_build_query to use '&' as a separator instead of the usual '&'
  $query_string = http_build_query($params, NULL, '&');
  if ($method == 'GET') {
    $url = $url_base . '?' . $query_string;
    $http_result = drupal_http_request($url, array(), 'GET');
  }
  elseif ($method == 'POST') {
    $headers = array(
      'Content-Type' => 'application/x-www-form-urlencoded',
    );
    $http_result = drupal_http_request($url_base, $headers, 'POST', $query_string);
  }

  // Check for HTTP errors
  if ($http_result->error) {
    return array(
      'status' => FALSE,
      'message' => t('An error occured during the HTTP request: @error', array(
        '@error' => $http_result->error,
      )),
    );
  }
  if ($http_result->data) {

    // Test the HTTP return code
    if ($http_result->code >= 200 && $http_result->code <= 299) {

      // Prepare a good response array
      $result = array(
        'status' => TRUE,
        'status_code' => SMS_GW_OK,
        'gateway_status_code' => $http_result->code,
        'gateway_status_text' => $http_result->data,
      );
    }
    else {

      // We got a (possibly) bad response code
      $result = array(
        'status' => FALSE,
        'status_code' => SMS_GW_ERR_OTHER,
        'gateway_status_code' => $http_result->code,
        'gateway_status_text' => $http_result->data,
      );
    }
  }
  return $result;
}