PHP Restful Web服务返回响应没有任何数据

时间:2015-08-22 10:37:23

标签: php mysql web-services rest

我创建了一个简单的restful Web服务,使用GET方法从单个表中返回所有列值。我使用Chrome restful Web客户端工具测试了该服务。它显示200 OK状态"响应没有任何数据"。

我的代码:

<?php
        require_once("Rest.inc.php");
        class API extends REST {
        public $data = "";      
        const DB_SERVER = "23.229.203.97";
        const DB_USER = "beta_user";
        const DB_PASSWORD = "beta@123";
        const DB = "beta02";        
        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);
            // $this->mysqli = new mysqli(self::DB_SERVER, self::DB_USER, self::DB_PASSWORD, self::DB);
        }       

        public function processApi(){
            $func = strtolower(trim(str_replace("/","",$_REQUEST['x'])));
            if((int)method_exists($this,$func) > 0)
                $this->$func();
            elsef
                $this->response('',404);                
        }

        private function getusers(){    

            if($this->get_request_method() != "GET"){
                $this->response('',406);
            }
            $sql = mysql_query("SELECT * FROM users", $this->db);
            if(mysql_num_rows($sql) > 0){
                $result = array();
                while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC)){
                    $result[] = $rlt;
                }

                $this->response($this->json('hai'), 200);
            }
            $this->response('',204);
        }
}

?>

1 个答案:

答案 0 :(得分:0)

除了API之外,您没有调用任何dbConnect类方法 在__construct()中添加一些内容,例如

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);
        $this->getusers();
    }
    else $this->response('error selecting database',200);
}

并查看是否返回任何内容