O'Reilly logo

Asterisk: The Future of Telephony by Leif Madsen, Jared Smith, Jim Van Meggelen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 programming 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 US cities, go to
    # 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.[74]

    # 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required