opendrive.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. # Debug
  3. $DEBUG = false;
  4. if ($_ENV['DEBUG']) {
  5. $DEBUG = true;
  6. }
  7. # On-disk config
  8. require_once '.secrets';
  9. # Config
  10. $API_BASE = 'https://dev.opendrive.com/api/v1';
  11. $API_VERSION = 10;
  12. # Open the DB connection
  13. function dbOpen() {
  14. global $DB_STRING;
  15. global $DB_USER;
  16. global $DB_PASSWD;
  17. try {
  18. $dbh = new PDO($DB_STRING, $DB_USER, $DB_PASSWD);
  19. } catch (PDOException $e) {
  20. die('DB error: ' . $e->getMessage() . "\n");
  21. }
  22. return $dbh;
  23. }
  24. # POST the provided data to OpenDrive
  25. function curlPost($url, $data) {
  26. global $API_BASE;
  27. global $CA_PATH;
  28. # Setup cURL
  29. $ch = curl_init($API_BASE . $url);
  30. curl_setopt_array($ch, array(
  31. CURLOPT_CAINFO => $CA_PATH,
  32. CURLOPT_POST => TRUE,
  33. CURLOPT_RETURNTRANSFER => TRUE,
  34. CURLOPT_HTTPHEADER => array(
  35. 'Content-Type: application/json'
  36. ),
  37. CURLOPT_POSTFIELDS => json_encode($data)
  38. ));
  39. # Send the request
  40. $response = curl_exec($ch);
  41. # Check for errors
  42. if($response === FALSE){
  43. die(curl_error($ch));
  44. }
  45. # Return
  46. $responseData = json_decode($response, TRUE);
  47. return($responseData);
  48. }
  49. # Login to OD and return the SessionID
  50. function login() {
  51. global $API_VERSION;
  52. global $OD_USER;
  53. global $OD_PASSWD;
  54. # POST
  55. $data = array(
  56. 'username' => $OD_USER,
  57. 'passwd' => $OD_PASSWD,
  58. 'version' => $API_VERISON
  59. );
  60. $response = curlPost('/session/login.json', $data);
  61. # Check the result
  62. if ($response['SessionID']) {
  63. return $response['SessionID'];
  64. }
  65. return false;
  66. }
  67. # Logout of OpenDrive
  68. function logout($session) {
  69. # POST
  70. $data = array(
  71. 'session_id' => $session
  72. );
  73. $response = curlPost('/session/logout.json', $data);
  74. # Check the result
  75. if ($response['result'] == "true") {
  76. return true;
  77. }
  78. return false;
  79. }
  80. ?>