You are here

function sms_txtlocal_command in SMS Framework 6

Executes a command using the Txtlocal API

data array fields:

  • number - MSISDN of message recipient. Purely numeric and must begin with intl prefix, eg. 4477121231234. May be a comma-separated list.
  • message - Message text. Max 612 chars (4x SMS). Use %n for newline.
  • sender - Optional: Sender ID may be an MSISDN or a string. Min=3, max=11 chars.
  • url - Optional: May be used to send a WAP PUSH. 'url' param for txtlocal.
  • reference - Optional: Reference tag to apply to message. Will appear on any receipt. 'custom' param for txtlocal.
  • delaymins - Optional: Delay message sending by N minutes. Formatted into a future timestamp for 'shed' param in txtlocal.

Parameters

$command: One of 'sendmsg' or 'getbalance'.

$data: All data required to perform the command.

$config: Configuration parameters.

Return value

Whether the command succeeded or not.

2 calls to sms_txtlocal_command()
sms_txtlocal_balance in modules/sms_txtlocal/sms_txtlocal.module
Get account balance
sms_txtlocal_send in modules/sms_txtlocal/sms_txtlocal.module
Callback for sending messages.

File

modules/sms_txtlocal/sms_txtlocal.module, line 219
Txtlocal gateway module for Drupal SMS Framework. Outbound+Inbound

Code

function sms_txtlocal_command($command = 'sendmsg', $data = array(), $config = NULL) {
  $gateway = sms_gateways('gateway', 'txtlocal');

  // Get config
  if ($config == NULL) {
    $config = $gateway['configuration'];
  }

  // SSL
  if ($config['sms_txtlocal_ssl'] === 1) {
    $scheme = 'https';
  }
  else {
    $scheme = 'http';
  }

  // Test mode
  if ($config['sms_txtlocal_test']) {
    $test = '1';
  }
  else {
    $test = '0';
  }

  // Preparing the URLs
  $url = $scheme . "://www.txtlocal.com/";
  $url_send = $url . "sendsmspost.php";
  $url_balance = $url . "getcredits.php";
  switch ($command) {
    case 'sendmsg':

      // Txtlocal requires us to specify a sender
      if (isset($data) && array_key_exists('options', $data) && array_key_exists('sender', $data['options'])) {
        $sender = $data['options']['sender'];
      }
      else {
        $sender = $config['sms_txtlocal_defaultsender'];
      }

      // Prepare required arguments
      $post_data = array(
        'uname' => $config['sms_txtlocal_user'],
        'pword' => $config['sms_txtlocal_password'],
        'message' => $data['message'],
        'from' => $sender,
        'selectednums' => $data['number'],
        'info' => '1',
        // We need this debug on to realise errors from txtlocal.
        'test' => $test,
      );

      // Request delivery receipts
      if ($config['sms_txtlocal_receipts']) {
        $post_data['rcpurl'] = url('sms/txtlocal/receipt', array(
          'absolute' => TRUE,
        ));
      }

      // Add any optional arguments
      if (isset($data) && array_key_exists('options', $data)) {
        if (array_key_exists('url', $data['options'])) {
          $post_data['url'] = $data['options']['url'];
        }
        if (array_key_exists('reference', $data['options'])) {
          $post_data['custom'] = $data['options']['reference'];
        }
        if (array_key_exists('delaymins', $data['options']) && $data['options']['delaymins'] > 5) {
          $delay_until = gmmktime() + $data['options']['delaymins'] * 60;
          $post_data['shed'] = date('Y-m-d-G-i-s', $delay_until);
        }
      }

      // Run the command
      // I tried to use http_build_query() here, but it kept adding '&' as
      // '&' which killed the query.
      foreach ($post_data as $key => $value) {
        $content .= $key . "=" . $value . "&";
      }
      $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
      );
      $http_result = drupal_http_request($url_send, $headers, 'POST', $content);
      break;
    case 'getbalance':
      $post_data = array(
        'uname' => $config['sms_txtlocal_user'],
        'pword' => $config['sms_txtlocal_password'],
      );

      // I tried to use http_build_query() here, but it kept adding '&' as
      // '&' which killed the query.
      foreach ($post_data as $key => $value) {
        $content .= $key . "=" . $value . "&";
      }
      $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
      );
      $http_result = drupal_http_request($url_balance, $headers, 'POST', $content);
      break;
  }

  // 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) {

    // Check for txtlocal errors
    if (strpos($http_result->data, 'ERROR') !== FALSE) {

      // There was an error
      $result = array(
        'status' => FALSE,
        'status_code' => SMS_GW_ERR_OTHER,
        // todo Try to map error codes
        'gateway_status_code' => '',
        // todo Try to map error codes
        'gateway_status_text' => $http_result->data,
      );
    }
    else {

      // Prepare a good response array
      $result = array(
        'status' => TRUE,
        'status_code' => SMS_GW_OK,
        'gateway_status_code' => '',
        // todo Try to map error codes
        'gateway_status_text' => $http_result->data,
      );
    }
  }
  return $result;
}