Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Struttura corretta

  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2006
    Messaggi
    3,072

    Struttura corretta

    Ciao a tutti, ho cercato alcuni esempio on line per capire la soluzione ideale per strutturare un'API. Tra tutte, la più completa mi sembra questa (allegata) ma non capisco come "utilizzarla".... inteso come chiamare il comando (quindi passare l'azione da eseguire all'API) (esempio deleteuser) e ritornare un JSON con un elenco di valori. Anche seguendo il commento iniziale mi perdo.

    Mi potreste dare qualche dritta?

    Codice PHP:

    <?php
        
        
    /* 
            This is an example class script proceeding secured API
            To use this class you should keep same as query string and function name
            Ex: If the query string value rquest=delete_user Access modifiers doesn't matter but function should be
                 function delete_user(){
                     You code goes here
                 }
            Class will execute the function dynamically;
            
            usage :
            
                $object->response(output_data, status_code);
                $object->_request    - to get santinized input     
                
                output_data : JSON (I am using)
                status_code : Send status message for headers
                
            Add This extension for localhost checking :
                Chrome Extension : Advanced REST client Application
                URL : [url]https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo[/url]
            
            I used the below table for demo purpose.
            
            CREATE TABLE IF NOT EXISTS `users` (
              `user_id` int(11) NOT NULL AUTO_INCREMENT,
              `user_fullname` varchar(25) NOT NULL,
              `user_email` varchar(50) NOT NULL,
              `user_password` varchar(50) NOT NULL,
              `user_status` tinyint(1) NOT NULL DEFAULT '0',
              PRIMARY KEY (`user_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
         */
        
        
    require_once("Rest.inc.php");
        
        class 
    API extends REST {
        
            public 
    $data "";
            
            const 
    DB_SERVER "localhost";
            const 
    DB_USER "root";
            const 
    DB_PASSWORD "arun";
            const 
    DB "users";
            
            private 
    $db NULL;
        
            public function 
    __construct(){
                
    parent::__construct();                // Init parent contructor
                
    $this->dbConnect();                    // Initiate Database connection
            
    }
            
            
    /*
             *  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 method for access api.
             * This method dynmically call the method based on the query string
             *
             */
            
    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".
            
    }
            
            
    /* 
             *    Simple login API
             *  Login must be POST method
             *  email : <USER EMAIL>
             *  pwd : <USER PASSWORD>
             */
            
            
    private function login(){
                
    // Cross validation if the request method is POST else it will return "Not Acceptable" status
                
    if($this->get_request_method() != "POST"){
                    
    $this->response('',406);
                }
                
                
    $email $this->_request['email'];        
                
    $password $this->_request['pwd'];
                
                
    // Input validations
                
    if(!empty($email) and !empty($password)){
                    if(
    filter_var($emailFILTER_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);
                            
                            
    // If success everythig is good send header as "OK" and user details
                            
    $this->response($this->json($result), 200);
                        }
                        
    $this->response(''204);    // If no records "No Content" status
                    
    }
                }
                
                
    // If invalid inputs "Bad Request" status message and reason
                
    $error = array('status' => "Failed""msg" => "Invalid Email address or Password");
                
    $this->response($this->json($error), 400);
            }
            
            private function 
    users(){    
                
    // Cross validation if the request method is GET else it will return "Not Acceptable" status
                
    if($this->get_request_method() != "GET"){
                    
    $this->response('',406);
                }
                
    $sql mysql_query("SELECT user_id, user_fullname, user_email FROM 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;
                    }
                    
    // If success everythig is good send header as "OK" and return list of users in JSON format
                    
    $this->response($this->json($result), 200);
                }
                
    $this->response('',204);    // If no records "No Content" status
            
    }
            
            private function 
    deleteUser(){
                
    // Cross validation if the request method is DELETE else it will return "Not Acceptable" status
                
    if($this->get_request_method() != "DELETE"){
                    
    $this->response('',406);
                }
                
    $id = (int)$this->_request['id'];
                if(
    $id 0){                
                    
    mysql_query("DELETE FROM users WHERE user_id = $id");
                    
    $success = array('status' => "Success""msg" => "Successfully one record deleted.");
                    
    $this->response($this->json($success),200);
                }else
                    
    $this->response('',204);    // If no records "No Content" status
            
    }
            
            
    /*
             *    Encode array into JSON
            */
            
    private function json($data){
                if(
    is_array($data)){
                    return 
    json_encode($data);
                }
            }
        }
        
        
    // Initiiate Library
        
        
    $api = new API;
        
    $api->processApi();
    ?>

  2. #2
    Ma, principalemente, sai come funziona un API???

    Gli "standard" dell'API che vuoi realizzare li puoi definire tu, quindi sa che se in chiamata hai VAR1 e VAR2, in risposta avrai una certa risposta!

    Invece di utilizzare qualcosa di già fatto, prova a fare qualcosa tu

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2006
    Messaggi
    3,072
    Mi piaceva questa struttura molto ordinata e completa.

    Mi potresti suggerire come utilizzare questo WS?

    Grazie

  4. #4
    Originariamente inviato da Valeria75_bis
    Mi piaceva questa struttura molto ordinata e completa.

    Mi potresti suggerire come utilizzare questo WS?

    Grazie
    ma che vuol dire "mi potresti suggerire come utilizzare questo ws"? Devi fare un client rest che usi le API che il tuo script espone. Probabilmente dovrai fare le richieste con curl visto che devi specificare il metodo da usare (POST/GET/DELETE/PUT) come segnalato nelle api (vedi ad esempio il metodo login)
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.