PHP code example of fperdomo / php-agi

1. Go to this page and download the library: Download fperdomo/php-agi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fperdomo / php-agi example snippets


#!/usr/bin/env php


declare(strict_types=1);

();
    
    // Answer the call
    $agi->answer();
    
    // Speak text using text-to-speech
    $agi->text2wav("Hello from PHPAGI!");
    
    // Hangup the call
    $agi->hangup();
    
    exit(0);
} catch (\Throwable $e) {
    // Log errors to stderr for debugging
    error_log('AGI Error: ' . $e->getMessage());
    exit(1);
}



declare(strict_types=1);

atcher;

// Handler scripts live here
$dispatcher = new FastAgiDispatcher(
    baseDir: __DIR__ . '/handlers',
    dropPrivileges: true,
    logVerboseDump: false,
);

$dispatcher->handle();



declare(strict_types=1);

Manager;

// Configuration from environment variables or defaults
$config = [
    'server'   => getenv('AMI_HOST') ?: '127.0.0.1',
    'port'     => (int) (getenv('AMI_PORT') ?: 5038),
    'username' => getenv('AMI_USER') ?: 'admin',
    'secret'   => getenv('AMI_SECRET') ?: 'amp111',
];

try {
    $manager = new AgiAsteriskManager(null, $config);
    
    // Connect to Asterisk Manager
    if ($manager->connect()) {
        echo "Connected to Asterisk Manager Interface\n";
        
        // Perform actions (e.g., originate call, query status, etc.)
        // $response = $manager->send_request('Action', ['Action' => 'Ping']);
        
        // Remember to disconnect when done
        // $manager->disconnect();
    }
} catch (\Throwable $e) {
    error_log('AMI Error: ' . $e->getMessage());
    exit(1);
}

composer 
bash
chmod +x your-script.php
ini
exten => 123,1,AGI(your-script.php)
exten => 123,n,Hangup()
bash
composer 
bash
chmod +x /var/lib/asterisk/agi-bin/hello.php
chown asterisk:asterisk /var/lib/asterisk/agi-bin/hello.php
ini
[Unit]
Description=PHPAGI FastAGI Dispatcher
After=network.target

[Service]
Type=simple
User=asterisk
Group=asterisk
WorkingDirectory=/opt/phpagi
ExecStart=/usr/bin/php /opt/phpagi/fastagi-server.php
Restart=always
RestartSec=2
Environment=APP_ENV=production

[Install]
WantedBy=multi-user.target
bash
# Make executable
chmod +x examples/agi/interactive-menu.php

# Copy to Asterisk AGI directory
cp examples/agi/interactive-menu.php /var/lib/asterisk/agi-bin/

# Add to extensions.conf
# exten => 123,1,AGI(interactive-menu.php)
bash
# Set environment variables
export AMI_HOST="127.0.0.1"
export AMI_USER="admin"
export AMI_SECRET="your-secret"

# Run the example
php examples/ami/ping.php
bash
# Start the FastAGI server
php examples/fastagi/server.php

# In extensions.conf:
# exten => 123,1,FastAGI(agi://127.0.0.1:4573/your-script)

src/
  Agi.php               # Main AGI class
  FastAgiDispatcher.php # FastAGI dispatcher
  AgiAsteriskManager.php# Asterisk Manager (AMI) support
composer.json
README.md