Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once("Rest.inc.php");
- class API extends REST {
- public $data = "";
- const DB_SERVER = "x";
- const DB_USER = "x";
- const DB_PASSWORD = "x";
- const DB = "x";
- private $db = NULL;
- public function __construct(){
- parent::__construct(); // Init parent contructor
- $this->dbConnect(); // Initiate Database connection
- }
- private function dbConnect(){
- $this->db = mysql_connect(self::DB_SERVER,self::DB_USER,self::DB_PASSWORD);
- if($this->db)
- mysql_select_db(self::DB,$this->db);
- }
- public function processApi(){
- $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
- if((int)method_exists($this,$func) > 0)
- $this->$func();
- else
- $this->response('',404); // If the method not exist with in this class, response would be "Page not found".
- }
- private function registerClient(){
- $entityBody = file_get_contents('php://input');
- $jsonDecoded = $this->jsonDecode($entityBody);
- //var_dump($entityBody);
- $jcname = $jsonDecoded["cname"];
- $jclocation = $jsonDecoded["clocation"];
- $jckey = $jsonDecoded["key"];
- $jencyKey = md5($jckey);
- /* $cname = $this->_request['cname'];
- $clocation = $this->_request['clocation'];
- $keyStr = $this->_request['clientKey'];
- $key = md5($keyStr);
- $registerStr = array("cname"=>$cname,"clocation"=>$clocation,"key"=>$key);
- $jsonArr = $this->json($registerStr);
- //$jsoncheck = $this->isJson($jsonArr);
- $jsonD = $this->jsonDecode($jsonArr);
- $jcname = $jsonD["cname"];
- echo($jcname);
- */
- // Input validations
- if(!empty($jcname)){
- $sql = mysql_query("INSERT into client(cname,clocation,key) values('$jcname','$jclocation','$jencyKey')", $this->db);
- echo "inside if condition";
- var_dump($sql);
- if($sql){
- $result = "Client Registered Successfully";
- // If success everythig is good send header as "OK" and user details
- }
- else{
- $result = "Unsuccessful Registration!";
- } // If no records "No Content" status
- $this->response($result, 200);
- }
- // If invalid inputs "Bad Request" status message and reason
- $error = array('status' => "Failed", "msg" => "No detailes entered.");
- $this->response($this->json($error), 400);
- }
- private function json($data){
- if(is_array($data)){
- return json_encode($data);
- }
- }
- private function jsonDecode($data){
- if($this->isJson($data))
- {
- return json_decode($data,true);
- }
- }
- private function isJson($string) {
- json_decode($string);
- return (json_last_error() == JSON_ERROR_NONE);
- }
- }
- // Initiiate Library
- $api = new API;
- $api->processApi();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement