Was this page helpful?

AGI: 不用PHPAGI的 PHP 範例

    內容表格
    1. 1. 更多參考

    使用 PHP 開發 AGI 時經常會用到 PHPAGI,但有時後對於簡單的需求時,這又顯得太肥大,此篇介紹利用 stdin 的方式開發 PHP 的 AGI。

    說明:

    1. debug=true,開啟後,所有輸出內容會寫到檔案 my_agi.log,若是在 SHELL mode 下執行,路徑為目前目錄,若是由 Asterisk 的 dialplan 執行,路徑則改為 /tmp。
       

    agi-test.php :

    #!/usr/bin/php -q
    <?
    ob_implicit_flush(false);
    set_time_limit(6);
    $stdin = fopen('php://stdin', 'r');
    $stdlog = fopen('my_agi.log', 'w');
    
    $debug = true;
    
    /* Read input from Asterisk and output via $astOutput */
    function astRead()
    {
       global $stdin, $debug, $stdlog;
       $astOutput = str_replace("\n", "", fgets($stdin, 4096));
    
       //if ($debug) fputs($stdlog, "read: $input\n");
       if ($debug) fputs($stdlog, "read: $astOutput\n");
          return $astOutput ;
    }
    
    /* Write AGI command to Asterisk */
    function astWrite($agiCommand)
    {
       global $debug, $stdlog;
       if ($debug) fputs($stdlog, "write: $agiCommand\n");
          echo $agiCommand."\n";
    }
    
    /* Handling execution input from Asterisk */
    $agivar = array();
    while (!feof($stdin))
    {
       $temp = fgets($stdin);
       $temp = str_replace("\n","",$temp);
       if ($temp == "")
       {
          break;
       }
       $s = explode(":",$temp);
       $agivar[$s[0]] = trim($s[1]);
    }
    
    /* Operational Code starts here */
    /* Playback the demo-congrats.gsm file from the
     * directory /var/lib/asterisk/sounds/
     */
    
    astWrite("STREAM FILE /var/lib/asterisk/sounds/en/tt-monkeys #");
    astRead();
    
    /* Say the number 123456
    astWrite("SAY NUMBER 123456 #");
    astRead();*/
    
    /* Finalization of AGI script and clean-ups */
    fclose ($stdin);
    fclose ($stdlog);
    exit(0);
    
    ?>
    Was this page helpful?
    標籤 (Edit tags)
    • No tags
    您必須 登入 才能發佈評論。
    Powered by MindTouch Core