Skip to content

Yate JSON API

Alexey Pavlyuts edited this page Sep 13, 2021 · 1 revision

YateAPI class

It is wrappeer to generic Yate JSON API, good for the most configuration and control API of Yate products.

Use

Create instance, provide config array and logger of your choice, implementing PSR-3 logging interface.

Config array structure:

$config = [
    
    'hss' => [  //Record for a Yate core component, one for each
        'uri' => 'http(s)://{host:port}/',
        'secret' => 'secret',
    ],
    '*' => [    // this one be used if no exact record found. Good for MiniCore where all the components on one interface
        'uri' => 'http(s)://{host:port}/',
        'secret' => 'secret',
    ],
];

You can define uri to call and secret for each component as it defined by Yate docs, well-known are mgmt,ucn,hss,smsc,usgw,stp and dra. If you have multiple component installed on the same address, you have to repeat the record for each. * is used if no exact match for a required node or the node name is not supplied at all.

Create PSR-3 logger, Monolog as an example:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$log = new Logger('YateAPI');
$log->pushHandler(new StreamHandler('/var/log/YateAPI.log', Logger::DEBUG));

Create YateAPI instance with config and logger and then use ->call(...) method to call API.

$api = new YateAPI($config, $log);

if ($api->call('get_node_type')) {
    echo print_r($api->resultArray(), true).PHP_EOL;
} else {
    echo $api->getMessage().PHP_EOL;
    echo $api->resultRaw().PHP_EOL;
}

->call(...) methid definition:

function call(string $request, array $params = null, array $node = null )

Refer to Yate docs for API to know node you must call to, request type and params array. The method will create the proper JSON structure. $params and $node may be omitted (or set null) if no need for it, but mind that only * endpoint be used if the node name is null.

->call(...) returns boolean, true if sucess and false if something goes wrong. Any error happens will shown in the log. No exception be rised. ->ok() method may be used to check status later. ->getMessage() returns problem description, provided by CURL or Yate backend.

If the call is successfull, use methods to retreive results:

  • ->resultRaw() - original JSON text as received from Yate node
  • ->resultArray() - JSON converted to associated array
  • ->resultObject() - JSON converted to an object

For interpretation and use of the result please refer to Yate docs

Clone this wiki locally