Created
October 4, 2017 14:53
-
-
Save laudarch/d17fdcac22acc4a388e4e11ce017bc95 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
<?php | |
namespace lib; | |
class Authy { | |
// API key | |
private $api_key = ''; | |
private $api_url = 'https://api.authy.com/protected/json/'; | |
public function __construct($api_key) { | |
$this->api_key = $api_key; | |
} | |
public function requestToken($data) { | |
$url = $this->api_url . 'sms/' . $data['authy_id'] . '?api_key=' . $this->api_key . '&force=' . $data['force']; | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Authy-API-Key: {$this->api_key}")); | |
$result = curl_exec( $ch ); | |
$result = json_decode($result); | |
return $result->success; | |
} | |
public function verifyToken($data) { | |
$url = $this->api_url . 'verify/' . $data['token'] . '/' . $data['id']; | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Authy-API-Key: {$this->api_key}")); | |
$result = curl_exec( $ch ); | |
$result = json_decode( $result ); | |
return $result->success; | |
} | |
public function newUser($data) { | |
$data[ 'api_key' ] = $this->api_key; | |
$data_string = ""; | |
$url = $this->api_url . 'users/new'; | |
foreach ($data as $key => $value) { | |
$data_string .= $key.'='.$value.'&'; | |
} | |
rtrim($data_string, '&'); | |
$ch = curl_init(); | |
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); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Authy-API-Key: {$this->api_key}")); | |
$result = curl_exec($ch); | |
$result = json_decode($result); | |
$result = $result->success ? $result->user->id : ""; | |
return $result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment