fperdomo/php-agi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fperdomo/php-agi

Composer 安装命令:

composer require fperdomo/php-agi

包简介

Modern PHP 8.4+ fork of PHPAGI for building Asterisk AGI, FastAGI, and AMI integrations.

README 文档

README

PHPAGI is a PHP library that simplifies building Asterisk AGI (Asterisk Gateway Interface), AMI (Asterisk Manager Interface), and FastAGI applications. This repository is a modernized fork of the original welltime/phpagi project and is updated to be compatible with PHP 8.3+ while maintaining a familiar API for easier migration.

Table of Contents

  • Features
  • Requirements
  • Installation
  • Usage
    • AGI script
    • FastAGI
    • Asterisk Manager (AMI)
  • Examples
  • Directory layout
  • Contributing
  • License

Features

  • AGI class for interactive call control
  • FastAGI server support
  • Asterisk Manager Interface (AMI) integration
  • Updated for PHP 8.3+ compatibility
  • Maintains the original API design to simplify migration
  • Extensions: ext-sockets

Requirements

  • PHP 8.3 or higher
  • Asterisk PBX (with AGI / FastAGI support)
  • Composer for dependency management

Why this fork?

The original welltime/phpagi project is now archived, and many deployments still rely on the classic API. This package modernizes PHPAGI for PHP 8.3+ while keeping a familiar developer experience to simplify migration.

Installation

Install the package via Composer (replace with your vendor if you publish under a different name):

composer require fperdomo/php-agi

Usage

Below are minimal examples to get started with AGI, FastAGI, and AMI usage.

AGI script

AGI scripts are executed by Asterisk from the dialplan. Place executable PHP scripts in the location Asterisk can call.

Example: simple AGI script

#!/usr/bin/env php
<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Fperdomo\PhpAgi\Agi;

try {
    $agi = new Agi();
    
    // 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);
}

Make the script executable:

chmod +x your-script.php

In extensions.conf:

exten => 123,1,AGI(your-script.php)
exten => 123,n,Hangup()

More AGI Examples

See the examples/agi/ directory for more comprehensive examples:

Production quickstart (AGI)

  1. Install:
composer require fperdomo/php-agi
  1. Put scripts in Asterisk AGI directory (commonly /var/lib/asterisk/agi-bin/).
  2. Make executable and ensure correct owner:
chmod +x /var/lib/asterisk/agi-bin/hello.php
chown asterisk:asterisk /var/lib/asterisk/agi-bin/hello.php
  1. In your dialplan:
 exten => 123,1,NoOp(Hello AGI)
 same => n,AGI(hello.php)
 same => n,Hangup()
  1. Debug:
  • Check Asterisk console: asterisk -rvvvvv
  • Log in script using error_log() (stderr) and/or a file logger.

FastAGI as a systemd service

Running FastAGI with systemd (recommended)

Create /etc/systemd/system/phpagi-fastagi.service:

[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

Enable & start:

systemctl daemon-reload
systemctl enable phpagi-fastagi
systemctl start phpagi-fastagi
journalctl -u phpagi-fastagi -f

Dialplan:

exten => 123,1,FastAGI(agi://127.0.0.1:4573/your-script)

AMI security notes

  • Never expose AMI (5038) publicly.
  • Use a dedicated AMI user with minimum permissions.
  • Prefer binding to localhost or a private network interface.
  • Store credentials in environment variables (or secrets manager).

FastAGI

FastAGI runs as a server process and handles AGI commands over TCP. Run a FastAGI server and configure Asterisk to call it.

Example FastAGI dispatcher:

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Fperdomo\PhpAgi\FastAgiDispatcher;

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

$dispatcher->handle();

Configure Asterisk to use FastAGI in extensions.conf:

exten => 123,1,FastAGI(agi://your.server.ip:4573/your-script)

FastAGI Example

See examples/fastagi/server.php for a complete FastAGI server implementation with example handlers.

Asterisk Manager (AMI)

Use the AgiAsteriskManager class to connect to Asterisk's management interface to receive events or take actions programmatically.

Example:

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Fperdomo\PhpAgi\AgiAsteriskManager;

// 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);
}

Environment Variables:

For security, use environment variables for sensitive configuration:

export AMI_HOST="127.0.0.1"
export AMI_PORT="5038"
export AMI_USER="admin"
export AMI_SECRET="your-secret-password"

More AMI Examples

See the examples/ami/ directory for more comprehensive examples:

Examples

The examples/ directory contains practical, ready-to-use examples demonstrating various PHPAGI features:

AGI Examples (examples/agi/)

Example Description
interactive-menu.php Interactive IVR menu system with DTMF input handling
call-recording.php Record caller messages and store metadata in Asterisk database
database-lookup.php Lookup caller information using Asterisk database (AstDB)
call-center-router.php Complete call center with VIP detection, queues, and statistics
verbose.php Simple verbose logging demonstration
hello.php Minimal low-level AGI implementation without using PHPAGI class

AMI Examples (examples/ami/)

Example Description
ping.php Simple AMI connection test using Ping action
originate-call.php Originate outbound calls programmatically with custom variables
monitor-events.php Real-time event monitoring with custom event handlers

FastAGI Examples (examples/fastagi/)

Example Description
server.php Complete FastAGI server implementation
handlers/your-script.php Example FastAGI handler script

Running the Examples

AGI Examples:

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

AMI Examples:

# 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

FastAGI Examples:

# Start the FastAGI server
php examples/fastagi/server.php

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

Directory layout

A suggested layout for this project:

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

Contributing

Contributions are welcome. Suggested workflow:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/fooBar
  3. Commit your changes
  4. Push to your fork
  5. Open a Pull Request

Testing & CI: Ensure compatibility with PHP 8.3+; GitHub Actions or similar CI is recommended.

License

This project is released under the GNU Lesser General Public License (LGPL-2.1+), consistent with the original PHPAGI license.

fperdomo/php-agi 适用场景与选型建议

fperdomo/php-agi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 234 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「asterisk」 「ami」 「voip」 「agi」 「ivr」 「telephony」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fperdomo/php-agi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fperdomo/php-agi 我们能提供哪些服务?
定制开发 / 二次开发

基于 fperdomo/php-agi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 234
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-01