A very simple PHPAGI example

Discussion in 'General' started by haamed, Oct 15, 2009.

  1. haamed

    Joined:
    Jul 23, 2007
    Messages:
    251
    Likes Received:
    0
    You should make and place PHP file in /var/lib/asterisk/agi-bin/testphp.php directory.
    Don't forget to grant execution rights to your script


    Code:
    1. #!/usr/bin/php -q
    2. <?
    3. set_time_limit(30);
    4. require('include/phpagi.php');
    5. error_reporting(E_ALL);
    6.
    7. $agi = new AGI();
    8.
    9. $agi->answer();
    10.
    11. $agi->stream_file("demo-congrats","#");
    12. do
    13. {
    14. $agi->stream_file("enter-some-digits","#");
    15. $result = $agi->get_data('beep', 3000, 20);
    16. $keys = $result['result'];
    17. $agi->stream_file("you-entered","#");
    18. $agi->say_digits($keys);
    19. } while($keys != '111');
    20. $agi->hangup();
    21. ?>
    

    after this you need an extension to call phpagi, place this code in etc/asterisk/extension_custom.conf,

    Code:
    [from-internal-custom]
    include => testphp
    
    
    [testphp]
    exten => 6565,1,Answer 
    exten => 6565,2,AGI(testphp.php) 
    exten => 6565,3,Hangup 
    reload astrisk with #asterisk -rx "reload"

    Now call 6565
     
  2. bono056

    Joined:
    Jan 19, 2010
    Messages:
    70
    Likes Received:
    0
    hi
    what about connecting to a external database? do you recommend using another class for running queries & after getting values from database using phpagi to read them?
     
  3. haamed

    Joined:
    Jul 23, 2007
    Messages:
    251
    Likes Received:
    0
    In your PHP program you can do any thing you want !
    You can work with any DB and have data transfer,
    it is prevalent !
     
  4. jameswilson

    Joined:
    May 10, 2010
    Messages:
    4
    Likes Received:
    0
    Many thanks for this code. I have followed the instructions. However whenever i connect to the extension from the ivr the system hangsup.
    DO i need additional text to speech modules etc. Or should this work on an out of the box elastix?

    James
     
  5. haamed

    Joined:
    Jul 23, 2007
    Messages:
    251
    Likes Received:
    0
    Yes. it will work on any Asterisk System that have PHP-AGI like Trixbox and Asterisk Now.
    You can use println() to found out, is it run or not, if yes in witch code line you got error.
     
  6. riz

    riz

    Joined:
    Aug 15, 2010
    Messages:
    2
    Likes Received:
    0
    what is that script for..?? im sorry im a newbie in phpagi..
     
  7. mikeosake

    Joined:
    Oct 27, 2011
    Messages:
    2
    Likes Received:
    0
    Hi

    Very nice script.

    How can i get the call duration from that script?

    Michael
     
  8. elastix809

    Joined:
    Apr 5, 2011
    Messages:
    85
    Likes Received:
    0
    i was getting this error message Exec format error, and assigned the proper permision to the file, but solved was a problem in the php path
     
  9. mauroreggio

    Joined:
    May 18, 2010
    Messages:
    9
    Likes Received:
    0
    For Elastix 2.2.0 works, but you must pay attention at 2 "mistake":

    The file to be edit for add the new extension number is /etc/asterisk/extensions_custom.conf (extensionS with an S in the end)

    In the example, at the start, the php script call a required class, the path is not correct.
    Change
    require('include/phpagi.php');
    with
    require('phpagi.php');
    now the php script can find the class in the correct path and not go in error, so the call not hangup.

    However, thanks a lot haamed for start me in PHP-AGI ... now i can play a little :)
     

Share This Page