views:

94

answers:

1

Recently, I've released an open-source class - wrapper of API functions available from one of the leading SMS gateway, which provides an HTTP-API access to send an SMS messages.

Now I want to make a custom command line utitity, which can be available for user anywhere in the command line, so he can send SMS just by running console command like this one:

$ my_custom_send_sms_command -u your_username -p your_password -k your_api_key 447771234567 'Hello from CLI'

Please, explain how this can be done?

Updated:

Class is written in PHP.

A: 

Make sure you have the PHP CLI package installed. On my Ubuntu VPS I had to run:

$ apt-get install php5-cli

Check that it's installed:

$ /usr/bin/php -v

Then, create a file called something like sendsms.php with contents similar to this:

#!/usr/bin/php -q
<?php

  // command line parameters are in the format:
  //     key1 value1 .. keyN valueN number message

  $username = "";
  $password = "";
  $apikey = "";

  // process pairs of arguments up to the last two    
  for ($x = 0; $x < ($argc - 3); $x++)
  {
    if ($argv[$x] == "-u") $username = $argv[$x + 1];
    if ($argv[$x] == "-p") $password = $argv[$x + 1];
    if ($argv[$x] == "-k") $apikey = $argv[$x + 1];
  }
  $number = $argv[$argc - 2];
  $message = $argv[$argc - 1];

  // To do: call the SMS API
  // For now just output the info   
  echo("Username = " . $username . "\r\n");
  echo("Password = " . $password . "\r\n");
  echo("API key = " . $apikey . "\r\n");
  echo("Number = " . $number . "\r\n");
  echo("Message = " . $message . "\r\n");
?>

Make sure the file has execute permissions:

chmod 755 sendsms.php

And then run it from the current folder like this:

$ ./sendsms.php -u your_username -p your_password -k your_api_key 447771234567 'Hello from CLI'
Bing