2009-06-27 74 views
0

最近,我發佈了一個開源類 - 封裝的API函數,它可以從一個領先的SMS網關提供,它提供了一個HTTP-API訪問來發送SMS消息。自定義命令行實用程序。怎麼樣?

現在我想打一個自定義命令行utitity,它可以供用戶在命令行中的任何地方,這樣他就可以僅僅通過這樣的一個運行控制檯命令發送短信:

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

請,解釋如何做到這一點?

更新:

類是用PHP編寫的。

+0

我想你會更好地提供更多的約束條件:可以把班級從例如訪問C,從一個shell,從Ruby,... – xtofl 2009-06-27 10:21:54

回答

0

確保您已安裝PHP CLI軟件包。在我的Ubuntu VPS我不得不跑:

$ apt-get install php5-cli 

檢查它的安裝:

$ /usr/bin/php -v 

然後,創建一個名爲像sendsms.php與內容類似於此文件:

#!/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"); 
?> 

確保該文件具有執行權限:

chmod 755 sendsms.php 

然後從當前文件夾像這樣運行:

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