JulienBreux
10/23/2013 - 2:26 PM

Twilio Test

Twilio Test

[23-Oct-2013 16:22:24 Europe/Paris] PING: init
[23-Oct-2013 16:22:24 Europe/Paris] CALL INIT: {"step":"init"}
[23-Oct-2013 16:22:35 Europe/Paris] PING: success
[23-Oct-2013 16:22:35 Europe/Paris] CALL SUCCESS: {"step":"success","AccountSid":"AC...","ToZip":"","FromState":"","Called":"+33XXXXXXXXX","FromCountry":"FR","CallerCountry":"FR","CalledZip":"","Direction":"outbound-api","FromCity":"","CalledCountry":"FR","CallerState":"","CallSid":"xxx","CalledState":"","From":"+33XXXXXXXXX","CallerZip":"","FromZip":"","CallStatus":"in-progress","ToCity":"","ToState":"","To":"+33XXXXXXXXX","ToCountry":"FR","CallerCity":"","ApiVersion":"2010-04-01","Caller":"+33XXXXXXXXX","CalledCity":""}
[23-Oct-2013 16:22:39 Europe/Paris] PING: status
[23-Oct-2013 16:22:39 Europe/Paris] CALL STATUS: {"step":"status","AccountSid":"AC...","ToZip":"","FromState":"","Called":"+33XXXXXXXXX","FromCountry":"FR","CallerCountry":"FR","CalledZip":"","Direction":"outbound-api","FromCity":"","CalledCountry":"FR","Duration":"1","CallerState":"","CallSid":"xxx","CalledState":"","From":"+33XXXXXXXXX","CallerZip":"","FromZip":"","CallStatus":"completed","ToCity":"","ToState":"","To":"+33XXXXXXXXX","CallDuration":"5","ToCountry":"FR","CallerCity":"","ApiVersion":"2010-04-01","Caller":"+33XXXXXXXXX","CalledCity":""}
<?php
// Lib: https://github.com/twilio/twilio-php
require_once __DIR__.'/libs/twilio/Services/Twilio.php';

// Constants
const SID = 'AC...';
const TKN = '...';
const NBR = '+33XXXXXXXXX';

// Variables
$step       = !empty($_GET['step']) ? $_GET['step'] : null;
$currentUrl = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' .
	$_SERVER['SERVER_NAME'] . parse_url($_SERVER["REQUEST_URI"])['path'];

// Create Twilio client
$client = new Services_Twilio(SID, TKN);

error_log('PING: ' . $step);

switch ($step) {
	default:
		echo '<a href="' . $currentUrl . '?step=init">Use: ?step=init</a>';
		break;
	// Call initialisation
	case 'init':
		error_log('CALL INIT: ' . json_encode($_REQUEST));
		$call = $client->account->calls->create(
			NBR,
			'+33XXXXXXXX',
			$currentUrl . '?step=success',
			[
				'StatusCallback'       => $currentUrl . '?step=status',
				'StatusCallbackMethod' => 'GET'
			]
		);
		break;

	// Call status
	case 'status':
		error_log('CALL STATUS: ' . json_encode($_REQUEST));
		break;

	// Call success
	case 'success':
		error_log('CALL SUCCESS: ' . json_encode($_REQUEST));
		break;
}