a pastebin project

clickatell.php

  1. #! /opt/csw/php5/bin/php
  2. <?php
  3.  
  4. /*
  5. * Send SMS notifications using the Clickatell HTTP API Interface
  6. *
  7. * Description of command line parameters: see further down...
  8. *
  9. * Clickatell HTTP API Documentation:
  10. * http://www.clickatell.com/downloads/http/Clickatell_HTTP.pdf
  11. * -> http://docs.google.com/viewer?url=http://www.clickatell.com/downloads/http/Clickatell_HTTP.pdf
  12. */
  13.  
  14. // "Enhance" the implode() function to work with associative arrays (eg. array("user" => "john"))
  15. function implode_assoc($array, $inner_glue = '=', $outer_glue = '&') {
  16.     $output = array();
  17.     foreach( $array as $key => $item )
  18.         $output[] = $key . $inner_glue . $item;
  19.  
  20.     return implode($outer_glue, $output);
  21. }
  22.  
  23. // Parse command line parameters
  24. $args = getopt('t:u:p:i:@:f:b:c:q:x:');
  25. if (
  26.     ($args == FALSE)
  27.     OR (! isset($args["t"]))
  28.     OR (! isset($args["u"]))
  29.     OR (! isset($args["p"]))
  30.     OR (! isset($args["i"]))
  31. ){
  32.     echo "Error! Command line arguments could not be parsed!
  33.  
  34. * The script requires the following command line parameters:
  35. * # -t ['to' SMS Recipient Number - \$CONTACTPAGER\$] #
  36. * # -u [Clickatell Username] #
  37. * # -p [Clickatell Password] #
  38. * # -i [Clickatell API ID] #
  39. * -@ [Scheduled Delivery Time; When should the message be sent?; Formats: Unix timestamp (1233133393) or UTC date format (2009-01-30T14:00:00Z)]
  40. * -f ['from' Source Address A valid international format number between 1 and 16 characters long, or an 11 character alphanumeric string.]
  41. * -b [Clickatell Callback type; see 5.2.4 Callback URL (callback); 0 = off, 3 = intermediate & final status]
  42. * -c [Max. number of concatted msgs to send out. By default (1), only 1 160char msg is send. If set to 2 or 3, 2x or 3x (160-7) char are allowed]
  43. * -q [Required SMS Features; see 5.2.8 Required features (req_feat); needs to incl. 16 if using Alpha From]
  44. * -x [Max. Credits; how many credits is ONE SMS message allowed to consum?; should be set to 3, to allow every possible SMS]
  45.  
  46. * Parameters listed with # are REQUIRED; others are optional.
  47.  
  48. * The script reads the SMS text from STDIN, ie. invoke it like so:
  49. *   printf 'Hello\\nWorld!' | " . $_SERVER['SCRIPT_NAME'] . " -u JohnDoe -p FooBar -i 9876543 ...
  50.  
  51. * Clickatell HTTP API Documentation:
  52. * http://www.clickatell.com/downloads/http/Clickatell_HTTP.pdf
  53. * -> http://docs.google.com/viewer?url=http://www.clickatell.com/downloads/http/Clickatell_HTTP.pdf
  54. ";
  55.     exit(1);
  56. }
  57.  
  58. echo "Welcome - Going to send an SMS....\n";
  59.  
  60. // "Fetch" parameters from the $args array
  61. define('CLICKATELL_USER', $args['u']);
  62. define('CLICKATELL_PASS', $args['p']);
  63. define('CLICKATELL_API_ID', $args['i']);
  64.  
  65. // "Sanitize" the number of the recipient - ie. drop any non-digit char
  66. define('TO', preg_replace('/[^[:digit:]]/', '', $args["t"]));
  67.  
  68. // Set "FROM"
  69. if (isset($args['f'])){ define('FROM', $args['f']); }
  70.  
  71. // SMS Concatenation
  72. // Maximum number of SMS to send, which makes up 1 msg at the
  73. // receiver. Allows to send SMS messages which are longer than
  74. // 160char
  75. // See 5.2.6 Concatenation (concat)
  76. if (isset($args['c'])){ define('SMS_CONCATS', $args['c']); }
  77.  
  78. // Callback Value
  79. // See 5.2.4 Callback URL (callback)
  80. //
  81. //      Callback      Message status types returned    Message status code returned
  82. //      value
  83. //      0          No message status returned.
  84. //      1          Returns only intermediate statuses.      002, 003, 011
  85. //      2          Returns only final statuses of a message.        004, 005, 006, 007, 008, 010, 012
  86. //      3          Returns both intermediate and final statuses of a msg.       All except 001
  87. if (isset($args['b'])){ define('CALLBACK_VALUE', $args['b']); }
  88.  
  89. // Required Features
  90. // See 5.2.8 Required features (req_feat)
  91. //
  92. //      Hex   Decimal    Feature          Description
  93. //      0x0001        1       FEAT_TEXT      Text  set by default.
  94. //      0x0002        2       FEAT_8BIT      8-bit messaging  set by default.
  95. //      0x0004        4       FEAT_UDH       UDH (Binary) - set by default.
  96. //      0x0008        8       FEAT_UCS2      UCS2 / Unicode  set by default.
  97. //      0x0010        16      FEAT_ALPHA    Alpha source address (from parameter).
  98. //      0x0020        32      FEAT_NUMER    Numeric source address (from parameter).
  99. //      0x0200        512     FEAT_FLASH   Flash messaging.
  100. //      0x2000        8192    FEAT_DELIVACK       Delivery acknowledgments.
  101. //      0x4000        16384   FEAT_CONCAT        Concatenation  set by default.
  102. if (isset($args['q'])){ define('REQ_FEAT', $args['q']); }
  103.  
  104. // Maximum Credits
  105. // This parameter overrides the maximum charge associated with message delivery,
  106. // 5.2.7 Maximum credits (max_credits)
  107. if (isset($args['x'])){ define('MAX_CREDITS', $args['x']); }
  108.  
  109. // Scheduled Time
  110. // The purpose of this parameter is to allow you to specify when you want a message to be delivered.
  111. // See 5.2.16 Scheduled Time
  112. // Formats:
  113. //      1) Unix timestamp:
  114. //           scheduled_time:1233133393
  115. //      2) UTC date format:
  116. //           scheduled_time:2009-01-30T14:00:00Z
  117. if (isset($args['@'])){ define('SCHEDULED_TIME', $args['@']); }
  118.  
  119. // Read the to-be sent message from stdin.
  120. $text = file_get_contents('php://stdin');
  121. // Convert UTF-8 chars -> iso-8859-1
  122. $text = utf8_decode($text);
  123. // Only allow $concat_count * (160-7) chars in $text
  124. if (defined('SMS_CONCATS')){
  125.     $text = substr($text, 0, SMS_CONCATS * (160-7));
  126. } else {
  127.     $text = substr($text, 0, 160);
  128. }
  129.  
  130. // URL to prepend to all outgoing calls
  131. define('BASEURL', "http://api.clickatell.com");
  132.  
  133. // auth call
  134. $cmd = "auth";
  135. $cmd_parms = array("user" => CLICKATELL_USER, "password" => CLICKATELL_PASS, "api_id" => CLICKATELL_API_ID);
  136. $url = BASEURL . "/http/" . $cmd . "?" . implode_assoc(array_map("urlencode", $cmd_parms));
  137. // do auth call
  138. $ret = file($url);
  139. // split our response. return string is on first line of the data returned
  140. $sess = split(":", $ret[0]);
  141.  
  142. // Did we get an "OK"? If so, continue with sending the message.
  143. if ($sess[0] == "OK") {
  144.     $sess_id = trim($sess[1]); // remove any whitespace
  145.     $cmd = "sendmsg";
  146.     $cmd_parms = array("session_id" => $sess_id,
  147.       "to" => TO,
  148.       "text" => $text
  149.     );
  150.     if (defined('MAX_CREDITS')){ $cmd_parms['max_credits'] = MAX_CREDITS; }
  151.     if (defined('FROM')){ $cmd_parms['from'] = FROM; }
  152.     if (defined('REQ_FEAT')){ $cmd_parms['req_feat'] = REQ_FEAT; }
  153.     if (defined('CALLBACK_VALUE')){ $cmd_parms['callback'] = CALLBACK_VALUE; }
  154.     if (defined('SMS_CONCATS')){ $cmd_parms['concat'] = SMS_CONCATS; }
  155.     if (defined('SCHEDULED_TIME')){ $cmd_parms['scheduled_time'] = SCHEDULED_TIME; }
  156.  
  157.     $url = BASEURL . "/http/" . $cmd . "?" . implode_assoc(array_map("urlencode", $cmd_parms));
  158.     // echo 'url: ' . $url . "\n";
  159.     // exit(42);
  160.     // do sendmsg call
  161.     $ret = file($url);
  162.     $send = split(":",$ret[0]);
  163.     if ($send[0] == "ID") {
  164.         echo "success
  165. message ID: ". $send[1] . "\n";
  166.         exit(0);
  167.     } else {
  168.         echo "send message failed: " . $ret[0] . "\n";
  169.         exit(1);
  170.      }
  171. } else {
  172.     echo "Authentication failure: " . $ret[0] . "\n";
  173.     exit(2);
  174. }
  175.  
  176. // We cannot reach this spot!
  177. echo "ERROR! Impossible program location!";
  178. exit(3);
  179. // EOF //

advertising

Create a Paste

Please enter your new post below (or upload a file instead):





Please note that information posted here will not expire by default. If you want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords.

worth-right
fantasy-obligation