Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. <?php
  2.  
  3. require_once("Rest.inc.php");
  4.  
  5. class API extends REST {
  6.  
  7. public $data = "";
  8.  
  9. const DB_SERVER = "x";
  10. const DB_USER = "x";
  11. const DB_PASSWORD = "x";
  12. const DB = "x";
  13.  
  14. private $db = NULL;
  15.  
  16. public function __construct(){
  17. parent::__construct(); // Init parent contructor
  18. $this->dbConnect(); // Initiate Database connection
  19. }
  20.  
  21. private function dbConnect(){
  22. $this->db = mysql_connect(self::DB_SERVER,self::DB_USER,self::DB_PASSWORD);
  23. if($this->db)
  24. mysql_select_db(self::DB,$this->db);
  25. }
  26.  
  27.  
  28. public function processApi(){
  29. $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
  30. if((int)method_exists($this,$func) > 0)
  31. $this->$func();
  32. else
  33. $this->response('',404); // If the method not exist with in this class, response would be "Page not found".
  34. }
  35.  
  36. private function registerClient(){
  37.  
  38. $entityBody = file_get_contents('php://input');
  39. $jsonDecoded = $this->jsonDecode($entityBody);
  40. //var_dump($entityBody);
  41.  
  42. $jcname = $jsonDecoded["cname"];
  43. $jclocation = $jsonDecoded["clocation"];
  44. $jckey = $jsonDecoded["key"];
  45. $jencyKey = md5($jckey);
  46.  
  47. /* $cname = $this->_request['cname'];
  48. $clocation = $this->_request['clocation'];
  49. $keyStr = $this->_request['clientKey'];
  50. $key = md5($keyStr);
  51. $registerStr = array("cname"=>$cname,"clocation"=>$clocation,"key"=>$key);
  52. $jsonArr = $this->json($registerStr);
  53. //$jsoncheck = $this->isJson($jsonArr);
  54. $jsonD = $this->jsonDecode($jsonArr);
  55. $jcname = $jsonD["cname"];
  56. echo($jcname);
  57.  
  58. */
  59. // Input validations
  60. if(!empty($jcname)){
  61.  
  62.  
  63. $sql = mysql_query("INSERT into client(cname,clocation,key) values('$jcname','$jclocation','$jencyKey')", $this->db);
  64. echo "inside if condition";
  65. var_dump($sql);
  66. if($sql){
  67. $result = "Client Registered Successfully";
  68.  
  69. // If success everythig is good send header as "OK" and user details
  70. }
  71. else{
  72. $result = "Unsuccessful Registration!";
  73.  
  74. } // If no records "No Content" status
  75. $this->response($result, 200);
  76. }
  77.  
  78. // If invalid inputs "Bad Request" status message and reason
  79. $error = array('status' => "Failed", "msg" => "No detailes entered.");
  80. $this->response($this->json($error), 400);
  81. }
  82.  
  83.  
  84.  
  85. private function json($data){
  86. if(is_array($data)){
  87. return json_encode($data);
  88. }
  89. }
  90. private function jsonDecode($data){
  91.  
  92. if($this->isJson($data))
  93. {
  94. return json_decode($data,true);
  95. }
  96. }
  97.  
  98. private function isJson($string) {
  99. json_decode($string);
  100. return (json_last_error() == JSON_ERROR_NONE);
  101. }
  102. }
  103.  
  104. // Initiiate Library
  105.  
  106. $api = new API;
  107. $api->processApi();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement