api.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once("Rest.inc.php");
  3. class API extends REST {
  4. public $data = "";
  5. //Enter details of your database
  6. const DB_SERVER = "localhost";
  7. const DB_USER = "root";
  8. const DB_PASSWORD = "R3M0T31";
  9. const DB = "cropmonitor";
  10. private $db = NULL;
  11. public function __construct(){
  12. parent::__construct(); // Init parent contructor
  13. $this->dbConnect(); // Initiate Database connection
  14. }
  15. private function dbConnect(){
  16. $this->db = mysql_connect(self::DB_SERVER,self::DB_USER,self::DB_PASSWORD);
  17. if($this->db)
  18. mysql_select_db(self::DB,$this->db);
  19. }
  20. /*
  21. * Public method for access api.
  22. * This method dynmically call the method based on the query string
  23. *
  24. */
  25. public function processApi(){
  26. $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
  27. if((int)method_exists($this,$func) > 0)
  28. $this->$func();
  29. else
  30. $this->response('Error code 404, Page not found',404); // If the method not exist with in this class, response would be "Page not found".
  31. }
  32. private function hello(){
  33. echo str_replace("this","that","HELLO WORLD!!");
  34. }
  35. private function test(){
  36. // Cross validation if the request method is GET else it will return "Not Acceptable" status
  37. if($this->get_request_method() != "GET"){
  38. $this->response('',406);
  39. }
  40. $myDatabase= $this->db;// variable to access your database
  41. $param=$this->_request['var'];
  42. // If success everythig is good send header as "OK" return param
  43. $this->response($param, 200);
  44. }
  45. /*
  46. * Encode array into JSON
  47. */
  48. private function json($data){
  49. if(is_array($data)){
  50. return json_encode($data);
  51. }
  52. }
  53. }
  54. // Initiiate Library
  55. $api = new API;
  56. $api->processApi();
  57. ?>