Page tree

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This page contains code snippets that demonstrate various Unified API requests that can be implemented in your App. Download.

Requirements

PHP 5.3.x+ is required.

Make a Call Between Two Public Numbers

<?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));

Make a Sandbox Call

<?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)); 

Park a Phone Call

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

<?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 => '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)); 

Transfer a Call to a Public Number

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 Transfer PhoneCalls request.

<?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));

Record an Ongoing Conversation

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 wav format and uses the StartRecording PhoneCalls request.

<?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));

Log in an Agent to a Queue

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

<?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 => '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)); 

List the Registration Status of an Extension

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

<?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 => '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)); 
#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels

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