PHP VoIP developers 101

Part 2: PHP example on recording VoIP calls, call routing

The Ozeki Phone System gives you the opportunity to use its special features. If you have any problems how to log calls, record or route them, do not hesitate to read this guide. All the answers are in it, and far more information about it.

Introduction

On the previous page the first part of PHP VoIP developers 101 could be read. That guide presented the prerequisites of doing basic tasks, such as SMS sending/receiving or making/receiving a call using your own PHP application. This article focuses on far more advanced features, like call logging, call recording and call routing.

Part 1: PHP example on sending SMS, making VoIP calls

Getting started

First of all, install a web server, MySql and PHP to your PC. The easiest way to do this, is installing Xampp package which includes Apache web server, MySql with a GUI, PHPMyAdmin and PHP. You can download it from here.

PHPMyAdmin

After installing Xampp, you can reach PHPMyAdmin at 'localhost/phpmyadmin'. Databases of MySql are availabe here. Username is "root" and password is empty by default. Creating a new database is available on the Databases tab.

After you create a database, create a table where you can record log entries of incoming calls. First, you need to choose the desired database. Then choose columns and create the table.

SQL Scripts

Creating a database:

CREATE DATABASE CallsLog

Code example 1 - SQL commands to create a new database

Creating a table:

CREATE TABLE calls(
    ID INT AUTO_INCREMENT,
    CallID VARCHAR(50),
    CallState VARCHAR(50),
    Caller VARCHAR(50),
    CallerID VARCHAR(50),
    Callee VARCHAR(50),
    NotificationName VARCHAR(50),
    PRIMARY KEY(ID))

Code example 2 - SQL commands to create a table

Inserting into the table:

INSERT INTO calls(CallID, 
		  CallState, 
		  Caller, 
	   	  CallerID, 
		  Callee, 
		  NotificationName)
VALUES("3CiHe",
       "Ringing",
       "103",
       "103",
       "101",
       "CallChanged")

Code example 3 - SQL commands to insert data

Query number of successful calls:

SELECT COUNT(*) FROM calls WHERE CallState = "Completed"

Code example 4 - SQL commands to query number of calls

1. Log calls

To log calls, you must change the settings at PBX, Productivity menu, http API menu item where you need to give a URL and select which script is executed if call status changes. In this case it is a PHP script.

First, PHP script read the call data:

$CallState = $_POST['CallState'];
$CallId = $_POST['CallId'];
$Caller = $_POST['Caller'];
$CallerId = $_POST['CallerId'];
$Callee = $_POST['Callee']; 
$NotificationName = $_POST['NotificationName'];

Code example 5 - PHP commands to change status

Then it makes a connection with the database server. It is possible because of the calling mysql_connect function. There are 3 arguments: the first is the name of database, second is the username and the third is the password. If the connection is successfully created, choose the desired database by calling the mysql_select_db function. To insert data you can use the mysql_query function. The function execute the instruction which was given in arguments, in this case that is an INSERT INTO instruction.

INSERT INTO calls(CallID, 
		  CallState, 
		  Caller, 
		  Callee, 
		  NotificationName)
VALUES("3CiHe",
       "Ringing",
       "103",
       "103",
       "101",
       "CallChanged")

Code example 6 - SQL commands to set calls

After you insert it, close the MySQL connection with mysql_close function.

Making queries

First, you need to join to MySQL server, and then choose the database with the instruction above. To make queries, you can also use the mysql_query function, however to process queries, use the mysql_fetch_array function, and it will make the records (which were given back by the query) into an array. By using the mysql_query argument function you request the return code.

2. Record calls

PBX can record the calls. For this, use your Call changed URL. So, the entered script can start the recording with an instruction. The attribute of the finshedUrl is the URL which gets the URL address from the record. You can choose the format of recording (mp3 or wav) in format attribute. Please change the ozekixepbx.ip text to the ip address where the Ozeki Phone System XE is installed. On yoursite.com the address should be the same as where the sample applications are running.

<Response>
	<Record finishedUrl="http://yoursite.com/Recording.php" format="mp3"/>
</Response>

Code example 7 - XML commands to reponse settings

By the end of the call, PBX sends the URL of record to the script which was given in finishedUrl. First, script read the data. Then write them into Records table, CallRecords, MySQL database.

$CallID = $_POST['CallId'];
$Success = $_POST['Success'];
$Duration = $_POST['Duration'];
$Caller = $_POST['Caller'];
$Callee = $_POST['Callee'];
$RecordURL = $_POST['RecordURL'];
$NotificationName = $_POST['NotificationName'];

Code example 8 - PHP commands to call recording

SQL Scripts

Creating a new database:

CREATE DATABASE CallRecords

Code example 9 - SQL commands to create a new database

Creating a new table:

CREATE TABLE Records(
    ID INT AUTO_INCREMENT ,
    CallID VARCHAR( 50 ) ,
    Succes VARCHAR( 50 ) ,
    Duration INT,
    Caller VARCHAR( 50 ) ,
    Callee VARCHAR( 50 ) ,
    RecordURL VARCHAR( 255 ) ,
    NotificationName VARCHAR( 50 ) ,
    PRIMARY KEY ( ID )

Code example 10 - SQL commands to create a new table

Insert:

INSERT INTO records(CallID, 
					Success, 
					Duration, 
					Caller, 
					CallerID, 
					RecordUrl, 
					NotificationName) 
VALUES("1WrukO", 
	   "True", 
	   "2", 
	   "1000", 
"9998", 	   "http://ozekixepbx.ip:7780/HttpRecordings/437c53e702454a01b8e3f8caa0824d85.mp3", "RecordCompleted")


Code example 11 - SQL commands to inster data

Query all records:

SELECT * FROM records

Code example 12 - SQL commands to make queries

3. Call routing

Besides Dial Plan, it is possible to give a PHP method, so you can decide which extension answers the call when forwarding your calls. If you did not forward the call, it will be handle by the rules of Dial Plan. You can enter into the Route interception URL field the path of your method (e.g. http://yoursite.com/RouteCalls/RouteCall) in the Productivity menu's, HTTP API submenu. PHP script reads the call data:

$NotificationName = $_POST['NotificationName'];
$CallerID = $_POST['CallerID'];
$OriginalDialedNumber = $_POST['OriginalDialedNumber'];
$LastDialedNumber = $_POST['LastDialedNumber'];
$LastCalledExtension = $_POST['LastCalledExtension'];
$LastCallState = $_POST['LastCallState'];
$CallerExtension = $_POST['CallerExtension'];

Code example 13 - PHP commands to set call routing

When there is a call for extension 9999 and it is forwarded to extension 1100 and waits just one second.

<Response>
	<Route RingTime = "1" Destination="1100"/>
</Response>

Code example 14 - XML commands to forward calls

If there was no answer to the previous call or line was occupied, call is forwarded to extension 9998:

<Response>
	<Route Destination="9998"/>
</Response>

Code example 15 - XML commands to forward calls

RingTime attribute is optional, it is 90 by default. If you do not want to forward the call, enter an empty string, so the rules of dial plan work. You can read more about Route command here.

Part 1: PHP example on sending SMS, making VoIP calls

If you have any questions or need assistance, please contact us at info@ozekiphone.com