PHP code example of clue / ami-react

1. Go to this page and download the library: Download clue/ami-react 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/ */

    

clue / ami-react example snippets




ory = new Clue\React\Ami\Factory();

$factory->createClient('user:secret@localhost')->then(function (Clue\React\Ami\Client $client) {
    echo 'Client connected' . PHP_EOL;

    $sender = new Clue\React\Ami\ActionSender($client);
    $sender->listCommands()->then(function (Clue\React\Ami\Protocol\Response $response) {
        echo 'Available commands:' . PHP_EOL;
        var_dump($response);
    });
});

$factory = new Clue\React\Ami\Factory();

$connector = new React\Socket\Connector(array(
    'dns' => '127.0.0.1',
    'tcp' => array(
        'bindto' => '192.168.10.1:0'
    ),
    'tls' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    )
));

$factory = new Clue\React\Ami\Factory(null, $connector);

$factory->createClient($url)->then(
    function (Clue\React\Ami\Client $client) {
        // client connected (and authenticated)
    },
    function (Exception $e) {
        // an error occurred while trying to connect or authorize client
    }
);

$factory->createClient('localhost:5038');

$factory->createClient('user:secret@localhost');

$user = 'he:llo';
$pass = 'p@ss';

$promise = $factory->createClient(
    rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost'
);

$factory->createClient('tls://user:secret@localhost:5039');

$action = $client->createAction('Originate', array('Channel' => …));
$promise = $client->request($action);

$action = $client->createAction('Originate', array('Channel' => …));
$promise = $client->request($action);

$client->on('event', function (Clue\React\Ami\Protocol\Event $event) {
    // process an incoming AMI event (see below)
    var_dump($event->getName(), $event);
});

$client->on('error', function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$client->on('close', function () {
    echo 'Connection closed' . PHP_EOL;
});

$sender = new Clue\React\Ami\ActionSender($client);

$sender->login($name, $pass);
$sender->logoff();
$sender->ping();
$sender->command($command);
$sender->events($eventMask);

$sender->coreShowChannels();
$sender->sipPeers();
$sender->agents();

// many more…

$sender->ping()->then(
    function (Clue\React\Ami\Protocol\Response $response) {
        // response received for ping action
    },
    function (Exception $e) {
        // an error occurred while executing the action
        
        if ($e instanceof Clue\React\Ami\Protocol\ErrorException) {
            // we received a valid error response (such as authorization error)
            $response = $e->getResponse();
        } else {
            // we did not receive a valid response (likely a transport issue)
        }
    }
});

use Clue\React\Block;
use React\EventLoop\Loop;

function getSipPeers()
{
    $factory = new Clue\React\Ami\Factory();

    $target = 'name:password@localhost';
    $promise = $factory->createClient($target)->then(function (Clue\React\Ami\Client $client) {
        $sender = new Clue\React\Ami\ActionSender($client);
        $ret = $sender->sipPeers()->then(function (Clue\React\Ami\Collection $collection) {
            return $collection->getEntryEvents();
        });
        $client->end();
        return $ret;
    });

    return Block\await($promise, Loop::get(), 5.0);
}

$sender->command('help')->then(function (Clue\React\Ami\Protocol\Response $response) {
    echo $response->getCommandOutput();
});

foreach ($collection->getEntryEvents() as $entry) {
    assert($entry instanceof Clue\React\Ami\Protocol\Event);
    echo $entry->getFieldValue('Channel') . PHP_EOL;
}

echo $collection->getCompleteEvent()->getFieldValue('ListItems') . PHP_EOL;