Help with asterisk API

Hi all,
i’m trying to use the asterisk API to send a command.
I need to see how many users are online so, from the cli, the command ‘SIP SHOW PEERS’ shows:

Name/username Host Dyn Nat ACL Port Status
555004/555004 (Unspecified) D N 0 UNKNOWN
555002/555002 D N 41162 OK (113 ms)
555001/555001 (Unspecified) D N 0 UNKNOWN

Inside a simple php page i insered the code below:

$timeout = 10;
$asterisk_ip = “”;
$socket = fsockopen($asterisk_ip,“5038”, $errno, $errstr, $timeout);

fputs($socket, “Action: login\r\n”);
fputs($socket, “Events: off\r\n”);
fputs($socket, “Username: admin\r\n”);
fputs($socket, “Secret: amp111\r\n\r\n”);

echo $wrets;

fputs($socket, “Action: Command\r\n”);
fputs($socket, “Command: SIP SHOW PEERS\r\n”);

echo $wrets;

fputs($socket, “Action: Logoff\r\n\r\n”);

on my php page i see:

Asterisk Call Manager/1.0 Response: Success

How to see the command result?


You need something like this to read the output from the socket

while (!feof($socket)) {
$wrets .= fread($socket, 8192);

Your only reading the first line of the output before you move on.

Try use the phpagi classes, , I think it will be easier.


Marco Bruni