Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated using 4PSA automated script
Excerpt

This page contains code snippets that demonstrate various Unified API requests that can be implemented in your App. Downloadusing the PHP language.

Table of Contents
maxLevel2

Requirements

PHP 5.3 .x+ is required.

...

or higher is required.

Note
titleThe code is on GitHub

Download here. Do not hesitate to contribute in order to make this example better - we welcome Pull Requests!

Make a call between two public numbers

Code Block
languagephp
<?php
// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS', 'CHANGEME');
define('API_ACCESS_TOKEN',  'CHANGEME');
define('EXTENSION_NUMBER',  'CHANGEME');
define('PUBLIC_NUMBER1',    'CHANGEME');
define('PUBLIC_NUMBER2',    'CHANGEME');

// The parameters sent in the body of the request
$data = array(
	'extension' => EXTENSION_NUMBER,
	'phoneCallView' => array(array(
		'source' => PUBLIC_NUMBER1,
		'destination' => PUBLIC_NUMBER2))
);

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),

	// URI that identifies the phone call
    CURLOPT_URL => 'https://'.VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/simple',
	CURLOPT_CUSTOMREQUEST => 'POST',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS => json_encode($data)
);

// Makes a HTTP POST request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch), true));

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-php/master/01_public_call.php" -o 01_public_call.php -L

Make a Sandbox call

Code Block
languagephp
<?php

// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS', 'CHANGEME');
define('API_ACCESS_TOKEN',  'CHANGEME');
define('EXTENSION_NUMBER',  'CHANGEME');
define('SANDBOX_NUMBER',    'CHANGEME');
  
// The parameters sent in the body of the request
$data = array(
	'extension' => EXTENSION_NUMBER,
	'phoneCallView' => array(array(
		'source' => EXTENSION_NUMBER,
		'destination' => SANDBOX_NUMBER))
);

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),
    
    // URI that identifies the phone call
    CURLOPT_URL => 'https://'.VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/simple',
	CURLOPT_CUSTOMREQUEST => 'POST',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS => json_encode($data)
);

// Makes a HTTP POST request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch),true)); 

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-php/master/02_sandbox_call.php" -o 02_sandbox_call.php -L

Park a phone call

Anchor
demo-ph-park
demo-ph-park

The example below demonstrates how you can use Unified API to park a party of an ongoing phone call. It uses the the Park PhoneCalls request.

Code Block
languagephp
<?php

// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS', 'CHANGEME');
define('API_ACCESS_TOKEN',  'CHANGEME');
define('PHONECALL_ID',      'CHANGEME"');
define('PHONECALLVIEW_ID',  'CHANGEME');
define('EXTENSION_NUMBER',  'CHANGEME');
 
// The parameters sent in the body of the request
$data = array(
	'action' => 'Park',
	'phoneCallViewId' => PHONECALLVIEW_ID
);

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),
	CURLOPT_URL => 'curl "https://'.VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/'. EXTENSION_NUMBER .'/'.PHONECALL_ID,
	CURLOPT_CUSTOMREQUEST => 'PUT',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS => json_encode($data)
);

// Makes a HTTP PUT request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch),true)); 

...

raw.githubusercontent.com/4psa/uapi-example-php/master/03_park_call.php" -o 03_park_call.php -L

Transfer a call to a public number

Anchor
demo-ph-xfer
demo-ph-xfer

The example below demonstrates how you can use Unified API to transfer a call to a public number, not registered with VoipNow. It uses the the Transfer PhoneCalls request.

Code Block
languagephp
<?php

// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS',			  'CHANGEME');
define('API_ACCESS_TOKEN',  		  'CHANGEME');
define('PHONECALL_ID',      		  'CHANGEME');
define('PHONECALLVIEW_ID',            'CHANGEME');
define('TRANSFER_DESTINATION_NUMBER', 'CHANGEME');

// The parameters sent in the body of the request
$data = array(
	'action' => 'Transfer',
	'sendCallTo' => TRANSFER_DESTINATION_NUMBER,
	'phoneCallViewId' => PHONECALLVIEW_ID
);

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),
    
    // URI that identifies the phone call
    CURLOPT_URL => 'https://'.VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/@self/'.PHONECALL_ID,
	CURLOPT_CUSTOMREQUEST => 'PUT',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS => json_encode($data)
);

// Makes a HTTP PUT request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch),true));

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-php/master/04_transfer_call.php" -o 04_transfer_call.php -L

Record an ongoing conversation

Anchor
demo-ph-rec
demo-ph-rec

The example below demonstrates how you can use Unified API to record an ongoing conversation. This action is possible for phone numbers that are registered with VoipNow. The recording is saved in the the wav format and uses the StartRecording PhoneCalls request.

Code Block
languagephp
<?php

// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS', 'CHANGEME');
define('API_ACCESS_TOKEN',  'CHANGEME');
define('PHONECALL_ID',      'CHANGEME');
define('PHONECALLVIEW_ID',  'CHANGEME');

// The parameters sent in the body of the request
$data = array(
	'action' => 'StartRecording',
	'format' => 'wav',
	'phoneCallViewId' => PHONECALLVIEW_ID
);

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),
    // URI that identifies the phone call
    CURLOPT_URL => 'https://'.VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/@self/'.PHONECALL_ID,
	CURLOPT_CUSTOMREQUEST => 'PUT',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS => json_encode($data)
);

// Makes a HTTP PUT request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch),true));

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-php/master/05_record_call.php" -o 05_record_call.php -L

Log in an agent to a queue

Anchor
demo-queue-ag
demo-queue-ag

The example below demonstrates how you can use Unified API to log in an agent to a queue. It uses the Update QueueAgents request.

Code Block
languagephp
<?php

// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS', 'CHANGEME');
define('API_ACCESS_TOKEN',  'CHANGEME');
define('QUEUE_NUMBER',      'CHANGEME');
define('AGENT_NUMBER',      'CHANGEME');

// The parameters sent in the body of the request
$data = array('status' => '1');

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),
    //URI that identifies the queue agent
    CURLOPT_URL => 'curl "https://'.VN_SERVER_ADDRESS.'/uapi/extensions/@me/'.QUEUE_NUMBER.'/queue/agents/'.AGENT_NUMBER,
	CURLOPT_CUSTOMREQUEST => 'PUT',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POSTFIELDS => json_encode($data)
);

/ Makes a HTTP PUT request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch),true)); 

...

raw.githubusercontent.com/4psa/uapi-example-php/master/06_agent_login.php" -o 06_agent_login.php -L

List the registration status of an extension

Anchor
demo-ext-pres
demo-ext-pres

The example below demonstrates how you can use Unified API to list the status of an extension. It uses the List Presence request.

Code Block
languagephp
<?php

// Modify these values with the ones you intend to use
define('VN_SERVER_ADDRESS', 'CHANGEME');
define('API_ACCESS_TOKEN',  'CHANGEME');
define('EXTENSION_NUMBER',  'CHANGEME');

// Setup the requests parameters
$options = array(
	CURLOPT_HTTPHEADER => array(
		'Content-type: application/json',
		'Authorization: Bearer '.API_ACCESS_TOKEN
	),
    // URI that identifies the presence
    CURLOPT_URL => 'curl "https://'.VN_SERVER_ADDRESS.'/uapi/extensions/@me/'.EXTENSION_NUMBER.'/presence',
	CURLOPT_CUSTOMREQUEST => 'GET',
	CURLOPT_RETURNTRANSFER => true
);

// Makes a HTTP GET request using SSL
$ch = curl_init();
curl_setopt_array($ch, $options);

// Parses the JSON response
print_r(json_decode(curl_exec($ch),true)); raw.githubusercontent.com/4psa/uapi-example-php/master/07_list_registration.php" -o 07_list_registration.php -L

Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.