You are here

function sms_simplegateway_send in SMS simple gateway 7

Same name and namespace in other branches
  1. 6 sms_simplegateway.module \sms_simplegateway_send()

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

\stdClass Response array. Response array.

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

File

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

Code

function sms_simplegateway_send($number, $message, $options) {
  $gateway = sms_gateways('gateway', 'simplegateway');
  $config = $gateway['configuration'];
  if (is_array($options) and array_key_exists('sender', $options)) {
    $config['sms_simplegateway_sender_value'] = $options['sender'];
  }
  $body = array_filter(array(
    $config['sms_simplegateway_user_field'] => $config['sms_simplegateway_user_value'],
    $config['sms_simplegateway_pass_field'] => $config['sms_simplegateway_pass_value'],
    $config['sms_simplegateway_sender_field'] => $config['sms_simplegateway_sender_value'],
    $config['sms_simplegateway_number_field'] => $config['sms_simplegateway_number_prefix_value'] . $number,
    $config['sms_simplegateway_message_field'] => $message,
  ));
  switch ($config['sms_simplegateway_method']) {
    case 'GET':
      $body = http_build_query($body) . '&' . $config['sms_simplegateway_extra_params'];
      $http_result = drupal_http_request($config['sms_simplegateway_base_url'] . '?' . $body);
      break;
    case 'POST':
      $headers = array();
      switch ($config['sms_simplegateway_authorization']) {
        case 'basic':
          $headers += array(
            'Authorization' => 'Basic ' . base64_encode($config['sms_simplegateway_user_value'] . ':' . $config['sms_simplegateway_pass_value']),
          );
          break;
        case 'digest':
          $headers += array(
            'Authorization' => 'Digest ' . base64_encode($config['sms_simplegateway_user_value'] . ':' . $config['sms_simplegateway_pass_value']),
          );
          break;
        case 'ntlm':
          $headers += array(
            'Authorization' => 'Ntlm ' . base64_encode($config['sms_simplegateway_user_value'] . ':' . $config['sms_simplegateway_pass_value']),
          );
          break;
      }
      switch ($config['sms_simplegateway_content_type']) {
        case 'plain':
          $headers += array(
            'Content-Type' => 'application/x-www-form-urlencoded',
          );
          $body = http_build_query($body) . '&' . $config['sms_simplegateway_extra_params'];
          break;
        case 'json':
          $headers += array(
            'Content-Type' => 'application/json',
          );
          $body = json_encode($body);
          break;
      }
      $http_result = drupal_http_request($config['sms_simplegateway_base_url'], array(
        'method' => 'POST',
        'headers' => $headers,
        'data' => $body,
      ));
      break;
  }

  // Check for HTTP errors
  if (property_exists($http_result, 'error') && ($http_result->code <= 200 || $http_result->code >= 299)) {
    return array(
      'status' => FALSE,
      'message' => t('An error occurred during the HTTP request: @error', array(
        '@error' => $http_result->error,
      )),
    );
  }
  if (property_exists($http_result, 'data')) {

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

      // Prepare a good response array
      return 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
      return array(
        'status' => FALSE,
        'status_code' => SMS_GW_ERR_OTHER,
        'gateway_status_code' => $http_result->code,
        'gateway_status_text' => $http_result->data,
      );
    }
  }
}