Created
August 6, 2009 21:56
-
-
Save cloudvoxcode/163592 to your computer and use it in GitHub Desktop.
Handler for highlight number & call bookmarklet (PHP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Example handler for "call selected number" phone call bookmarklet. | |
# Browser hits CGI with 2 phone numbers (action=dial). Triggers outbound call | |
# via cloudvox.com phone API. When call connects, Cloudvox hits CGI for call | |
# steps (action=answer). CGI serves JSON to call 2nd number. | |
# | |
# Browser bookmarklet example dial params: | |
# action=dial&local_number=1234567890&remote_number=4567890123 | |
# Cloudvox example answer params: | |
# action=answer&phone_number=1234567890&remote_number=4567890123 | |
# More: http://cloudvox.com/ | |
header("Content-type: text/plain"); | |
process_request($_GET['action'], $_GET['local_number'], $_GET['remote_number']); | |
function process_request($action, $local_number, $remote_number) { | |
# dial URL for this app | |
$dial_url = 'http://myhost.cloudvox.com/applications/42/dial'; | |
# API key or basic auth | |
$dial_username = 'my@user.com'; | |
$dial_password = 'mypass'; | |
if ($action == 'dial') { | |
# call the bookmarklet user | |
print place_call($dial_url, $dial_username, $dial_password, $local_number, $remote_number); | |
} elseif ($action == 'answer') { | |
# local user has picked up. Generate steps to call remote number | |
print make_call_steps($remote_number); | |
} | |
} | |
function place_call($url, $username, $password, $local_number, $remote_number) { | |
$url = "$url?phone_number=$local_number&remote_number=$remote_number"; | |
return(http_auth_get($url, $username, $password)); | |
} | |
function make_call_steps($remote_number) { | |
$steps = array( | |
array('Name' => 'Speak', 'phrase' => 'Calling other party...'), | |
array('Name' => 'Dial', 'number' => $remote_number) | |
); | |
return(json_encode($steps)); | |
} | |
function http_auth_get($url, $username, $password) { | |
$ctx = stream_context_create(basic_auth_for($username, $password)); | |
$handle = fopen($url, 'r', false, $ctx); | |
return(stream_get_contents($handle)); | |
} | |
function basic_auth_for($username, $password) { | |
$cred = sprintf('Authorization: Basic %s', base64_encode("$username:$password")); | |
$opts = array( | |
'http' => array( | |
'method' => 'GET', | |
'header' => $cred) | |
); | |
return($opts); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment