$from_number, 'to' => $to_number, 'call_app_sid' => $application_sid, 'timeout' => $timeout, ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $api_url.'/v2/accounts/'.$account_sid.'/calls'); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1); // Set user and password curl_setopt ($curl, CURLOPT_USERPWD, $account_sid.':'.$account_token); // Do not check SSL curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // Add header curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // Set POST curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($body)); $result = curl_exec($curl); if ($result) { $result = json_decode($result, true); } else { $result = array( 'error_text' => curl_error($curl), 'error_code' => curl_errno($curl), 'status_code' => 600, ); } return $result; } /** * This function retrieves all calls made from the specified account * @param string $api_url URL to retrieve Apifonica API * @param string $account_sid Your Apifonica account identifier * @param string $account_token Password for your Apifonica account * @param string $uri URI to check the status of the call at * @return array */ function getCalls($api_url, $account_sid, $account_token, $uri) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $api_url . $uri); curl_setopt ($curl, CURLOPT_POST, 0); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1); // Set user and password curl_setopt ($curl, CURLOPT_USERPWD, $account_sid.':'.$account_token); // Do not check SSL curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // Add header curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $result = curl_exec($curl); if ($result) { $result = json_decode($result, true); } else { $result = array( 'error_text' => curl_error($curl), 'error_code' => curl_errno($curl), 'status_code' => 600, ); } return $result; } /** * Set default variables */ /** * set variables from POST */ $action = isset($_POST['action']) ? $_POST['action'] : 'default'; // Apifonica API URL $api_url = isset($_POST['api_url']) ? $_POST['api_url'] : 'https://api.apifonica.com'; // Apifonica account SID $account_sid = isset($_POST['account_sid']) ? $_POST['account_sid'] : ''; // Apifonica account password $account_token = isset($_POST['account_token']) ? $_POST['account_token'] : ''; // Caller's number (this number must belong to the Apifonica account you have specified) $from_number = isset($_POST['from_number']) ? $_POST['from_number'] : ''; // Number to call $to_number = isset($_POST['to_number']) ? $_POST['to_number'] : ''; // Application SID $application_sid = isset($_POST['application_sid']) ? $_POST['application_sid'] : ''; $timeout = 330; $result = false; $call_status_text = isset($_POST['call_status_text']) ? $_POST['call_status_text'] : 'Call status unknown.'; $uri = isset($_POST['uri']) ? $_POST['uri'] : ''; /** * Making a call */ if ($action == 'make_call') { $result = call($api_url, $account_sid, $account_token, $from_number, $to_number, $application_sid, $timeout); if ($result['status_code'] > 299) { // If the call action failed, display an error message and web contact form $action = 'view'; } /** * Check call status */ } else if ($action == 'get_call_status') { $result = getCalls($api_url, $account_sid, $account_token, $uri); if (isset($result['status'])) { switch ($result['status']) { case 'queued': $call_status_text = 'Call has been queued!'; break; case 'initiated': $call_status_text = 'Call has been initiated!'; break; case 'ringing': $call_status_text = 'Call is ringing!'; break; case 'answered': $call_status_text = 'Call has been answered!'; break; case 'no_answer': $call_status_text = 'There was no answer!'; break; case 'completed': $call_status_text = 'Call has been completed!'; break; case 'busy': $call_status_text = 'Call destination is busy!'; break; case 'rejected': $call_status_text = 'Call has been rejected!'; break; case 'failed': $call_status_text = 'Call has failed!'; break; case 'modified': $call_status_text = 'Call has been modified!'; break; default: } } } /** * Default view form */ ?>