Browse Source

Start the API building

Zachary Kotlarek 12 years ago
parent
commit
6df80073c1
1 changed files with 48 additions and 9 deletions
  1. 48 9
      backup/opendrive.php

+ 48 - 9
backup/opendrive.php

@@ -11,6 +11,7 @@ require_once '.secrets';
 
 # Config
 $API_BASE = 'https://dev.opendrive.com/api/v1';
+$API_VERSION = 10;
 
 # Open the DB connection
 function dbOpen() {
@@ -25,32 +26,70 @@ function dbOpen() {
 	return $dbh;
 }
 
-function login() {
+# POST the provided data to OpenDrive
+function curlPost($url, $data) {
 	global $API_BASE;
-	global $OD_USER;
-	global $OD_PASSWD;
-	
-	$ch = curl_init($API_BASE . '/session/login.json');
+	global $CA_PATH;
+
+	# Setup cURL
+	$ch = curl_init($API_BASE . $url);
 	curl_setopt_array($ch, array(
+	    CURLOPT_CAINFO => $CA_PATH,
 	    CURLOPT_POST => TRUE,
 	    CURLOPT_RETURNTRANSFER => TRUE,
 	    CURLOPT_HTTPHEADER => array(
 	        'Content-Type: application/json'
 	    ),
-	    CURLOPT_POSTFIELDS => json_encode($postData)
+	    CURLOPT_POSTFIELDS => json_encode($data)
 	));
 
-	// Send the request
+	# Send the request
 	$response = curl_exec($ch);
 
-	// Check for errors
+	# Check for errors
 	if($response === FALSE){
 	    die(curl_error($ch));
 	}
 
-	// Decode the response
+	# Return
 	$responseData = json_decode($response, TRUE);
 	return($responseData);
 }
 
+# Login to OD and return the SessionID
+function login() {
+	global $API_VERSION;
+	global $OD_USER;
+	global $OD_PASSWD;
+
+	# POST
+	$data = array(
+		'username' 	=> $OD_USER,
+		'passwd'	=> $OD_PASSWD,
+		'version'	=> $API_VERISON
+	);
+	$response = curlPost('/session/login.json', $data);
+
+	# Check the result
+	if ($response['SessionID']) {
+		return $response['SessionID'];
+	}
+	return false;
+}
+
+# Logout of OpenDrive
+function logout($session) {
+	# POST
+	$data = array(
+		'session_id'	=> $session
+	);
+	$response = curlPost('/session/logout.json', $data);
+
+	# Check the result
+	if ($response['result'] == "true") {
+		return true;
+	}
+	return false;
+}
+
 ?>