PHP VoIP developers 101

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

If you are unsure how to use the Ozeki Phone System for your PHP application, read this short guide. After reading this you should be able to send and recieve SMS messages, voice calls and make your very own projects.

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

1. What you need

2. Get started

Overview

HTTP API enables you to get information from the PBX in real time and to control calls with HTTP requests. Take a look at how it works through the example of Call command. Call command can be used to initiate a voice call. It makes an API extension to call a number and do tasks with the call. The tasks are declared in an OzML response document as seen in Figure 1.

how the call command works
Figure 1 - How the Call command works

Step 1: Create a call using a HTTP request, for example:

http://ozekixepbx.ip:7780/?command=Call&Dialed=100&ApiExtension=9997&Url=<?echo urlencode('http://yoursite.com/callconnected.php'); ?&>

Step 2: Accept the CallConnected request from Ozeki Phone System on your webserver and return an XML to handle the call. Example request sent by Ozeki Phone System PBX to your webserver: http://yoursite.com/callconnected.php

OzML example response returned by callconnected.php:

<Response>
  <Delay>1</Delay>
  <Speak>Congratulations, this is your first OzML Response command.</Speak>
  <Delay>2</Delay>
  <Speak>Have a nice day!</Speak>
</Response>

To send a test response to a call connected request in a simple manner using the tester, fill out the Response OzML field. You can choose from Example OzMLs or write your own one.

If you want to use HTTP API, first install a webserver (like Apache). For detailed setup instructions, take a look at our step-by-step guide.

Connecting Apache 2.2 and PHP 5.4

First you need to download PHP 5.4.8 . When you downloaded the zip file, unpack it. Preferably, do not contain any spaces in the path, because it can lead mistakes easily. After unpacking .reg extension files, they needed to be run. Then rename php.ini-production file to php.ini.

Afterwards we open httpd.conf file for editing, with a text editor. Since you unpacked files in C:/Program_Files/PHP, you can find it in conf folder in the main directory of Apache. Now, add the next entries at the end of that file.

PHPIniDir "C:/Program_Files/PHP/"
LoadModule php5_module "C:/Program_Files/PHP/php5apache2_2.dll"

If you are ready, insert the next row under <IfModule mime_module> entry.

AddType application/x-httpd-php .php

Finally rename index.html file (found in htdocs folder in Apache) to index.php.

Now you need to install an API extension in the Ozeki Phone System in order to receive calls and SMS messages. Find out how to install a new API extension.

3. Receive incoming voice calls

If your webserver is working, API extension notifications can be added by http API. With these, you can handle your incoming calls. You can choose different recorded text to different extensions. The easiest way is defining the text in an OzML file. So, when an extension receives a call, the previously given text will be read too. As a result, the file needs to be available.

With Speak tag you can specify which text will be read.

<Speak>Congratulations, this is your first OZML Response command.</Speak>

You can use <Delay> for pausing the reading. It postpones the reading by 1 sec.

<Delay>1</Delay>

It is possible to play a wav or mp3 file when there is an incoming call. You can define with <Play> tag. You can also give an URL here.

You can use <BlindTransfer> for forwarding calls. So calls are forward to the desired extension.

<Play Repeat = true>c:testsound.wav</Play>

You can send SMS messages with the next tag.

<BlindTransfer>101</BlindTransfer>

<SendSms recipient="45026467">Hello from Ozeki Phone System! </SendSms>

It is possible to handle informations from the incoming calls with using <UserInput> tag. With timeout attribute you can decide for how long the caller can answer. And with Repeat you can decide whether this time is over, text will be read again or not.

<UserInput timeout="10" repeat="true">…</UserInput >

<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Delay>1</Delay>
	<UserInput timeout="10" repeat="true">
		<InitialCommands>
			<Speak>Welcome to the Ozeki Phone System IVR.</Speak>
			<Speak>To send an SMS, press 1.</Speak>
			<Speak>To hear some more information, press 2.</Speak>
			<Speak>To transfer the call to another number, press 3.</Speak>
		</InitialCommands>
		<Inputs>
			<Input key="1">
				<Speak>A test SMS will be sent.</Speak>
				<SendSms recipient="45026467">Hello from Ozeki Phone System!</SendSms>
			</Input>
			<Input key="2">
				<Speak>You pressed button 2. For more information on OZML syntax, visit www.ozekiphone.com</Speak>
			</Input>
			<Input key="3">
				<BlindTransfer>101</BlindTransfer>
				<Speak>Sorry, but blind transfer failed to extension 101. Please setup an extension in the system.</Speak>
			</Input>
		</Inputs>
	</UserInput>
	<Speak>Good bye.</Speak>
</Response>

Code example 1 - PHP codes to answer an incoming call

Commands sent by the caller can be initialized by using <InitialCommands> tag.

	
<InitialCommands>
	<Speak>Welcome to the Ozeki Phone System IVR.</Speak>
	<Speak>To send an SMS, press 1.</Speak>
	<Speak>To hear some more information, press 2.</Speak>
	<Speak>To transfer the call to another number, press 3.</Speak>
</InitialCommands>	

Code example 2 - PHP codes to initialize commands

Inside <Inputs> tag, you can decide different interactions to different inputs, sent by the caller. Situations can be selected by <Input> tag.

<Inputs>
	<Inputs>
	<Input key="1">
		<Speak>A test SMS will be sent.</Speak>
		<SendSms recipient="45026467">Hello from Ozeki Phone System!</SendSms>
	</Input>
	<Input key="2">
		<Speak>You pressed button 2. For more information on OZML syntax, visit www.ozekiphone.com</Speak>
	</Input>
		<Input key="3">
		<BlindTransfer>101</BlindTransfer>
			<Speak>Sorry, but blind transfer failed to extension 101. Please setup an extension in the system.</Speak>
	</Input>
</Inputs>

Code example 3 - PHP codes to different inputs

This next example PHP script uses an SQL connection to connect to a database and query information about the customer who initated the call. The script uses the 'Caller' parameter from the HTTP request originating from the Ozeki Phone System to run an SQL SELECT query statement on the database. If the query was successfull, the OzML response will contain the full name of the customer, therefore making the response more interactive and personalized. Please change the ozekixepbx.ip text to the ip address where the Ozeki Phone System XE is installed. On the yoursite.com the address should be that where the sample applications are running.

<?php
	$con=mysqli_connect("ozekixepbx.ip","mysql.user","66GkjOSDBd","customers");
	$callerId = $_REQUEST['Caller'];
	$date = new DateTime();
	$now = $date->format('Y-m-d H:i:s');

	if(mysqli_connect_errno($con)) {
		//Failed to connent to database. Return OzML Response with the original caller id
	}
	else {
		$sqlQuery = $con->prepare('SELECT FullName FROM customers.customerentries WHERE CallerId = ?');
		$sqlQuery->bind_param('s', $callerId);
		$sqlQuery->execute();

		$result = $sqlQuery->get_result();
		$row = $result->fetch_assoc();
		if($row) {
		    $callerId = $row["FullName"];
		}
	}

print <<<END
        <Response>
            <Delay>1</Delay>
            <Speak>Good day $callerId! You have called our customer support hotline at $now.</Speak>
            <Speak>Your call has been noted, our assistans will call you back shortly.</Speak>
            <Delay>1</Delay>
            <Speak>For more information, visit our website.</Speak>
        </Response>
END;

	mysqli_close($con);
?>

Code example 4 - PHP codes to different inputs

4. Make outgoing voice calls

Making an outgoing call is only possible with the command above. After making a connection, specified OzML file (shows in the URL) will be read too. You can also choose PHP file, which is generate the relevant XML file. You can give the called number with Dialled. ApiExtension is stands for that extension with what you can make a call.

	
http://ozekixepbx.ip:7780/?command=Call&Dialed=100
&ApiExtension=9997&Url=http://yoursite.com/speak.xml	

5. Receive SMS message

Receiving SMS messages, like receiving voice calls, is possible with Api Extension notification. At the URL of Api Extension Notification, Incoming Message URL shows the file with http report generated by the incoming message. At the http response you can find the sender, recipient, identity of SMS and API extension, which received the text message.

6. Send SMS message

To send an SMS you can use the command above. ApiExtension is given the extension from which you want to send an SMS. You can edit your text in Message section. Delivery signs, provided by the supplier, are stored onto a file, which was given in the URL. Http request shows the ID of the previously sent SMS, so you can quote it later.

	
http://ozekixepbx.ip:7780/?command=SendSms&ApiExtension=9997&Recipient=
%2b061554670&Message=Hello+world&DeliveryReportUrl=http://yoursite.com/smsdelivered.php	

7. Create a more advanced project

The Ozeki Phone System XE offers a lot more options for PHP developers. You can interact with existing calls, control and configure the PBX, you can introduce new communication techniques and media formats.
For more infromation check out: HTTP API Online Manual

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

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

More information