Created
May 3, 2012 00:24
-
-
Save tobsn/2582151 to your computer and use it in GitHub Desktop.
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
///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// HTML | |
<form action="" method="post" id="enter_number"> | |
<p>Enter your phone number:</p> | |
<p><input type="text" name="phone_number" id="phone_number" /></p> | |
<p><input type="submit" name="submit" value="Verify" /></p> | |
</form> | |
<div id="verify_code" style="display:none;"> | |
<p>Calling you now.</p> | |
<p>When prompted, enter the verification code:</p> | |
<h1 id="verification_code"></h1> | |
<p><strong id="status">Waiting...</strong></p> | |
</div> | |
///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Javascript | |
$(function(){ | |
var initiateCall = function() { | |
$.post( | |
'call.php', | |
{phone_number:( $('#phone_number').val() || false )}, | |
function( data ) { | |
showCodeForm( data.verification_code); | |
}, | |
'json' | |
); | |
checkStatus(); | |
}, | |
showCodeForm = function( code ) { | |
$("#verification_code").text(code); | |
$("#verify_code").fadeIn(); | |
$("#enter_number").fadeOut(); | |
}, | |
checkStatus = function() { | |
$.post( | |
'status.php', | |
{phone_number:( $('#phone_number').val() || false )}, | |
function( data ) { | |
updateStatus( data.status ); | |
}, | |
'json' | |
); | |
}, | |
updateStatus = function( current ) { | |
if( current === 'unverified' ) { | |
$('#status').append( '.' ); | |
setTimeout( checkStatus, 2000 ); | |
} | |
else { | |
$('#status').text( 'Verified!' ); | |
} | |
}; | |
$('#enter_number').submit( function( e ) { | |
e.preventDefault(); | |
initiateCall(); | |
}); | |
}); | |
///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// call.php | |
<?php | |
require_once( 'Services/Twilio.php' ); | |
require_once( 'database.php' ); | |
header( 'Content-type: application/json' ); | |
// require POST request | |
if( $_SERVER['REQUEST_METHOD'] != 'POST' ) { | |
echo json_encode( array( 'error' => 'Data not valid.' ) ): | |
exit; | |
} | |
// generate "random" 6-digit verification code | |
mt_seed( sprintf( '%u', crc32( $_SERVER['REMOTE_ADDR'] ) ).time() ); | |
$code = mt_rand( 100000, 999999 ); | |
// save verification code in DB with phone number | |
// does not check for duplicates like it should | |
$number = sprintf( '%u', preg_replace( '/[^0-9]/', '', $_POST['phone_number'] ) ); | |
// check if resulting number is actually a number (yeah we quickly cast a str on it) | |
if( !is_numeric( $number ) && strlen( $number ) == 10 ) { | |
echo json_encode( array( 'error' => 'Phone number not valid.' ) ): | |
exit; | |
} | |
// phone_number is unique and an unsigned INT | |
db( sprintf( 'INSERT INTO numbers SET phone_number = %u, verification_code = %d ON DUPLICATE verification_code = %2$d', $number, $code ) ); | |
// initiate phone call via Twilio REST API | |
// Set our AccountSid and AuthToken | |
$twilio = array(); | |
$twilio['id'] = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
$twilio['token'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
// Instantiate a new Twilio Rest Client | |
$client = new Services_Twilio( $twilio['id'], $twilio['token'] ); | |
try { | |
// make call | |
$call = $client->account->calls->create( | |
'+18881234567', // Verified Outgoing Caller ID or Twilio number | |
$number, // The phone number you wish to dial | |
'http://example.com/twiml.php' // The URL of twiml.php on your server | |
); | |
} catch( Exception $e ) { | |
echo json_encode( array( 'error' => 'Error starting phone call: '.$e->getMessage() ) ); | |
exit; | |
} | |
// return verification code as JSON | |
echo json_encode( array( 'error' => false, verification_code' => $code ) ); | |
exit; | |
?> | |
///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// twiml.php | |
<?php | |
require_once( 'Services/Twilio.php' ); | |
require_once( 'database.php' ); | |
$response = new Services_Twilio_Twiml(); | |
if( empty( $_POST['Digits'] ) ) { | |
$gather = $response->gather( array( 'numDigits' => 6 ) ); | |
$gather->say( 'Please enter your verification code.' ); | |
} | |
else { | |
// grab db record and check for match | |
$called = sprintf( '%u', preg_replace( '/[^0-9]+/', '', $_POST['Called'] ) ); | |
$result = db( sprintf( 'SELECT verification_code FROM numbers WHERE phone_number = %u', $called ) ); | |
if( $line = mysql_fetch_assoc( $result ) ) { | |
if( $_POST['Digits'] === $line['verification_code'] ) { | |
db( sprintf( 'UPDATE numbers SET verified = 1 WHERE phone_number = %u', $called ); | |
$response->say( 'Thank you! Your phone number has been verified.' ); | |
} | |
else { | |
// if incorrect, prompt again | |
$gather = $response->gather( array( 'numDigits' => 6 ) ); | |
$gather->say( 'Verification code incorrect, please try again.' ); | |
} | |
} | |
} | |
echo $response; | |
exit; | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment