Page 242 - Asterisk™: The Future of Telephony
P. 242

my $result = <STDIN>;
                   &checkresult($result);
               The second part of this test plays back the audio that was recorded earlier, using the
               STREAM FILE command. We’ve already covered STREAM FILE, so this section of code
               needs no further explanation.
                   print STDERR "================== Complete ======================\n";
                   print STDERR "$tests tests completed, $pass passed, $fail failed\n";
                   print STDERR "==================================================\n";
               At the end of the AGI script, a summary of the tests is printed to STDERR, which should
               end up on the Asterisk console.
               In summary, you should remember the following when writing AGI programs in Perl:

                 • Turn on strict language checking with the use strict command. §
                 • Turn off output buffering by setting $|=1.
                 • Data from Asterisk is received using a while(<STDIN>) loop.
                 • Write values with the print command.
                 • Use the print STDERR command to write debug information to the Asterisk console.

               The Perl AGI Library

               If you are interested in building your own AGI scripts in Perl, you may want to check
               out the Asterisk::AGI Perl module written by James Golovich, which is located at http://
               asterisk.gnuinter.net.  The  Asterisk::AGI  module  makes  it  even  easier  to  write  AGI
               scripts in Perl.

               Creating AGI Scripts in PHP

               We promised we’d cover several languages, so let’s go ahead and see what an AGI script
               in PHP looks like. The fundamentals of AGI programming still apply; only the pro-
               gramming language has changed. In this example, we’ll write an AGI script to download
               a weather report from the Internet and deliver the temperature, wind direction, and
               wind speed back to the caller:

                   #!/usr/bin/php -q
                   <?php
               The first line tells the system to use the PHP interpreter to run this script. The -q option
               turns off HTML error messages. You should ensure that there aren’t any extra lines
               between the first line and the opening PHP tag, as they’ll confuse Asterisk.
                   # change this to match the code of your particular city
                   # for a complete list of U.S. cities, go to



               § This advice probably applies to any Perl program you might write, especially if you’re new to Perl.

               214 | Chapter 9: The Asterisk Gateway Interface (AGI)
   237   238   239   240   241   242   243   244   245   246   247