Created
September 6, 2012 03:47
-
-
Save DeviaVir/3650964 to your computer and use it in GitHub Desktop.
An extremely simple authy PHP implementation
This file contains hidden or 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 | |
Class Authy { | |
// API key | |
private static $api_key = ''; | |
// Production | |
// private static $api_url = 'https://api.authy.com/protected/json/'; | |
// Sandbox | |
private static $api_url = 'http://sandbox-api.authy.com/protected/json/'; | |
// Show debug messages? | |
private static $debug = 1; | |
public static function verifyUser( $data ) { | |
/* | |
* $data should be: | |
* Array ( | |
* 'token' => '0000000', | |
* 'id' => '95' | |
* ) | |
*/ | |
$url = self::$api_url . 'verify/' . $data[ 'token' ] . '/' . $data[ 'id' ] . '?api_key=' . self::$api_key; | |
if( self::$debug ) { | |
print '<pre>'; | |
print_r( $url ); | |
print '</pre>'; | |
} | |
// Open connection | |
$ch = curl_init(); | |
// Set the url | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
// Execute cUrl | |
$result = curl_exec( $ch ); | |
if( self::$debug ) { | |
print '<pre>'; | |
print_r( $result ); | |
print '</pre>'; | |
} | |
$result = json_decode( $result ); | |
$result = $result->token; | |
if( $result == 'is valid' ) { | |
$result = true; | |
} else { | |
$result = false; | |
} | |
return $result; | |
} | |
public static function newUser( $data ) { | |
/* | |
* $data should be: | |
* Array( | |
* 'user[email]' => '[email protected]', | |
* 'user[cellphone]' => '0123456789 (without country code)', | |
* 'user[country_code]' => '31' | |
* ) | |
*/ | |
$data[ 'api_key' ] = self::$api_key; | |
$url = self::$api_url . 'users/new'; | |
// Url-ify the data for the POST | |
foreach( $data as $key => $value ) { | |
$data_string .= $key.'='.$value.'&'; | |
} | |
rtrim($data_string, '&'); | |
if( self::$debug ) { | |
print '<pre>'; | |
print_r( $data ); | |
print '</pre>'; | |
print '<pre>'; | |
print_r( $data_string ); | |
print '</pre>'; | |
} | |
// Open connection | |
$ch = curl_init(); | |
// Set the url, number of POST vars, POST data | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_POST, count( $data ) ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string ); | |
// Execute post | |
$result = curl_exec( $ch ); | |
if( self::$debug ) { | |
print '<pre>'; | |
print_r( $result ); | |
print '</pre>'; | |
} | |
// Returns the token ID | |
$result = json_decode( $result ); | |
$result = $result->user->id; | |
return $result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment