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

# http://www.nws.noaa.gov/data/current_obs/
                   $weatherURL="http://www.nws.noaa.gov/data/current_obs/KMDQ.xml";
               This tells our AGI script where to go to get the current weather conditions. In this
               example, we’re getting the weather for Huntsville, Alabama. Feel free to visit the web
               site listed above for a complete list of stations throughout the United States of America. ‖
                   # don't let this script run for more than 60 seconds
                   set_time_limit(60);
               Here, we tell PHP not to let this program run for more than 60 seconds. This is a safety
               net, which will end the script if for some reason it takes more than 60 seconds to run.
                   # turn off output buffering
                   ob_implicit_flush(false);
               This command turns off output buffering, meaning that all data will be sent immedi-
               ately to the AGI interface and will not be buffered.
                   # turn off error reporting, as it will most likely interfere with
                   # the AGI interface
                   error_reporting(0);
               This command turns off all error reporting, as it can interfere with the AGI interface.
               (You might find it helpful to comment out this line during testing.)
                   # create file handles if needed
                   if (!defined('STDIN'))
                   {
                      define('STDIN', fopen('php://stdin', 'r'));
                   }
                   if (!defined('STDOUT'))
                   {
                      define('STDOUT', fopen('php://stdout', 'w'));
                   }
                   if (!defined('STDERR'))
                   {
                      define('STDERR', fopen('php://stderr', 'w'));
                   }
               This section of code ensures that we have open file handles for STDIN, STDOUT, and
               STDERR, which will handle all communication between Asterisk and our script.
                   # retrieve all AGI variables from Asterisk
                   while (!feof(STDIN))
                   {
                       $temp = trim(fgets(STDIN,4096));
                       if (($temp == "") || ($temp == "\n"))
                       {
                           break;
                       }


               ‖ We apologize to our readers outside of the United States for using a weather service that only works for U.S.
                 cities. If you can find a good international weather service that provides its data in XML, it shouldn’t be too
                 hard to change this AGI script to work with that particular service. Once we find one, we’ll update this script
                 for future editions.

                                                                   Creating AGI Scripts in PHP | 215
   238   239   240   241   242   243   244   245   246   247   248