PHP Classes

File: api.php

Recommend this page to a friend!
  Packages of Arun Kumar   Simple REST Library   api.php   Download  
File: api.php
Role: Example script
Content type: text/plain
Description: Example
Class: Simple REST Library
Process requests to work as a REST server
Author: By
Last change:
Date: 14 years ago
Size: 2,051 bytes
 

 

Contents

Class file image Download
<?php

    require_once("Rest.inc.php");
    
    class API extends REST {
    
        public $data = "";
        
        const DB_SERVER = "localhost";
        const DB_USER = "root";
        const DB_PASSWORD = "";
        const DB = "";
        
        private $db = NULL;
    
        public function __construct(){
            parent::__construct();
            $this->dbConnect();
        }
        
        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(function_exists($func))
                $this->$func();
            else
                $this->response('',406);
        }
        
        private function login(){
            if($this->get_request_method() != "POST"){
                $this->response('',406);
            }
            $email = $this->_request['email'];
            $password = $this->_request['pwd'];
            if(!empty($email) and !empty($password)){
                if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                    $sql = mysql_query("SELECT user_id, user_fullname, user_email FROM users WHERE user_email = '$email' AND user_password = '".md5($password)."' LIMIT 1", $this->db);
                    if(mysql_num_rows($sql) > 0){
                        $result = mysql_fetch_array($sql,MYSQL_ASSOC);
                        $this->response($this->json($result), 200);
                    }
                    $this->response('', 204);
                }
            }
            $error = array('status' => "Failed", "msg" => "Invalid Email address or Password");
            $this->response($this->json($error), 400);
        }
        
        private function users(){
            $sql = mysql_query("SELECT user_id, user_fullname, user_email FROM blauk_users WHERE user_status = 1", $this->db);
            if(mysql_num_rows($sql) > 0){
                $result = array();
                while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC)){
                    $result[] = $rlt;
                }
                $this->response($this->json($result), 200);
            }
            $this->response('',204);
        }

        
        private function json($data){
            if(is_array($data)){
                return json_encode($data);
            }
        }
    }
    
    $api = new API;
    $api->processApi();
?>