Zum Hauptinhalt springen

REST API (PHP)

Here is the REST api of the Vodia PBX implemented in PHP as an example. Below you will find examples of logging in and using the received token as credentials for any subsequent API calls, like creating or deleting an extension, or dial plan etc. Of course basic PHP is used here to give a quick way of explaining the REST API but its implementation is upto you. It merely explains the format of the commands and values to be used for the different tasks.

Vodia Class

Below is the Vodia class in PHP used below for some of the REST API calls.


`class Vodia { private $destUrl = ""; private $loginName = ""; private $loginPass = ""; private $sessId = ""; private function getRest($url) { $params = array('http' => array('method' => 'GET', 'header' => "Cookie: session=" . $this->sessId . "\r\n")); $context = stream_context_create($params); $fp = fopen($url, 'r', false, $context); $metaData = stream_get_meta_data($fp); //print_r($metaData); $lengthRecord = $metaData['wrapper_data'][3]; $dataLen = explode(":", $lengthRecord)[1]; $dataLen = substr($dataLen, 1); $data = fread($fp, $dataLen); preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $metaData['wrapper_data'][0], $matches); $code = end($matches[1]); fclose($fp); if ($code == 200) { return $data; } else { return ""; } } private function putRest($url, $data, $login = "false") { $header = "Content-Type: application/json\r\n" . "Content-Length: " . strlen($data) . "\r\n"; if($login === "false") { $header .= "Cookie: session=" . $this->sessId . "\r\n"; } $params = array('http' =>array('method' => 'PUT', 'header' => $header, 'content'=> $data)); $context = stream_context_create($params); $fp = fopen($url, 'rb', false, $context); if($login === "true") { $this->sessId = fread($fp, 22); $this-&>sessId = substr($this->sessId, 1, 20); //echo $this-&>sessId; } $metaData = stream_get_meta_data($fp); //print_r($metaData); preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $metaData['wrapper_data'][0], $matches); $code = end($matches[1]); fclose($fp); if ($code == 200) { return true; } else { return false; } } public function setDestUrl($url) { $this->destUrl = $url; } public function setLoginPass($u, $p) { $this->loginName = $u; $this->loginPass = $p; } public function login() { $loginHash = md5($this->loginPass); $args = json_encode(array("name" =>"auth", "value" => $this->loginName . " " . $loginHash)); $this->putRest($this->destUrl . "/rest/system/session", $args, "true"); } public function getDomainInfo() { $result = $this->getRest($this-&>destUrl . "/rest/system/domaininfo"); return $result; } public function getDomains() { $result = $this-&>getRest($this->destUrl . "/rest/system/domains"); return $result; } public function getDomainSettings($domain) { $result = $this-&>getRest($this->destUrl . "/rest/domain/" . $domain . "/settings"); return $result; } public function putDomainSettings($domain, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/settings/", $set); } public function getUserList($domain, $type) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/userlist/" . $type); return $result; } public function getUserSettings($domain, $account) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/user_settings/" . $account); return $result; } public function putUserSettings($domain, $account, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/user_settings/" . $account, $set); } public function createAccount($domain, $s) { $set = json_encode($s); $this->putRest($this-&>destUrl . "/rest/domain/" . $domain . "/addacc/", $set); } public function domainAction($domain, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/domain_action/", $set); } public function getDomainTrunks($domain) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/domain_trunks/"); return $result; } public function createTrunk($domain, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/domain_trunks/", $set); } public function setTrunkSettings($domain, $trunk, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/edit_trunk/" . $trunk, $set); } public function getTrunkSettings($domain, $trunk) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/edit_trunk/" . $trunk); return $result; } public function getDialplans($domain) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/dialplans/"); return $result; } public function createDialplan($domain, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/dialplans/", $set); } public function setDialplanSettings($domain, $dp, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/edit_dialplan/" . $dp, $set); } public function getDialplanSettings($domain, $dp) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/edit_dialplan/" . $dp); return $result; } public function getAdrbook($domain) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/adrbook/"); return $result; } public function createAdr($domain, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/adrbook/", $set); } public function setAdr($domain, $adr, $s) { $set = json_encode($s); $this->putRest($this->destUrl . "/rest/domain/" . $domain . "/edit_adrbook/" . $adr, $set); } public function getAdr($domain, $adr) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/edit_adrbook/" . $adr); return $result; } public function getWebLangList($domain) { $result = $this->getRest($this->destUrl . "/rest/domain/" . $domain . "/account/" . "40" . "/list" . "/list_languages"); return $result; }}`

Command Examples in Simple PHP

Below are the examples of REST API as implemented in PHP. The point is to get the REST command or message right for controlling the PBX. We use "localhost" as the example domain but please use which ever domain is being modified.

Login

Of course use your ip, login and password. This is just an example.


`$vodiaObj = new Vodia;$vodiaObj->setDestUrl("http://192.168.203.132");$vodiaObj->setLoginPass("admin", "admin");$vodiaObj->login();`

Getting Domain Info


`$result = $vodiaObj->getDomainInfo();echo $result;`

Getting the Domains


`$result = $vodiaObj->getDomains();echo $result;`

Get and Set Domain Settings


`$result = $vodiaObj->getDomainSettings("localhost");echo $result;$vodiaObj->putDomainSettings("localhost", array("ani" = "123456"));`

Get User List


`$result = $vodiaObj->getUserList("localhost", "extensions");echo $result;`

Get User Settings


`$result = $vodiaObj->getUserSettings("localhost", "40");var_dump(json_decode($result));echo $result;$vodiaObj->putUserSettings("localhost", "40", array("ani" => "123456786"));`

Create, Manipulate and Delete an Account or Multiple Accounts

Of course this example creates an account of type extensions but you can create all account types with this API like AA, ACD, etc.

`$vodiaObj->createAccount("localhost", array("account_ext" => "50", "type" => "extensions"));$vodiaObj->domainAction("localhost", array("type" => "accounts", "action" =>"disable_selected", "selected" => "46 47 48"));$vodiaObj->domainAction("localhost", array("type" => "accounts", "action" => "enable_selected", "selected" => "46 47 48"));$vodiaObj->domainAction("localhost", array("type" => "accounts", "action" =>"delete_selected", "selected" => "50 51"));`

Get the List of Trunks


`$result = $vodiaObj->getDomainTrunks("localhost");echo $result;`

Create, Manipulate or Delete a Trunk


`$vodiaObj->createTrunk("localhost", array("name" => "New trunk", "aadr" => "testtrunk", "reg_account" =>"abc", "reg_pass" => "abcdef"));$vodiaObj->domainAction("localhost", array("type" => "trunks", "action" => "disable_selected", "selected" =>"102"));$vodiaObj->domainAction("localhost", array("type" => "trunks", "action" =>"enable_selected", "selected" =>"102"));$vodiaObj->domainAction("localhost", array("type" => "trunks", "action" => "delete_selected", "selected" => "134"));`

Set and Get Trunk Settings


`$vodiaObj->setTrunkSettings("localhost", "134", array("remote_party" => "70"));$result = $vodiaObj->getTrunkSettings("localhost", "134");echo $result;`

Get Dial Plans


`$result = $vodiaObj->getDialplans("localhost");echo $result;`

Create and Delete a Dial Plan


`$vodiaObj->createDialplan("localhost", array("name" =>"New dialplan"));$vodiaObj->domainAction("localhost", array("type" => "trunks", "action" => "delete_selected", "selected" => "45"));`

Create an Entry into a Dial Plan and Get Dial Plan Settings


`{"name":"Dial Out", "global":"false", "dps":[{"index":"180", "settings": {"cmc":"","dis":"false","id":"30","pattern":"*","preference":"100","replacement":"","sf":"","trunk":"111"}}]}$set = array("trunk_181" => "102", "preference_181" =>"110");$vodiaObj->setDialplanSettings("localhost", "45", $set);$result = $vodiaObj->getDialplanSettings("localhost", "45");echo $result;`

Get Addressbook Entries


`$result = $vodiaObj->getAdrbook("localhost");echo $result;`

Set and Delete an Addressbook Entry


`$vodiaObj->createAdr("localhost", array("number" => "123456789", "mobile" =>"987654321", "first" => "phpcontact", "last" => "php"));$vodiaObj->domainAction("localhost", array("type" =>"trunks", "action" => "delete_selected", "selected" => "45"));`

Edit an Addressbook Entry


`$vodiaObj->setAdr("localhost", "116", array("number" => "789"));$result = $vodiaObj->getAdr("localhost", "116");echo $result;`