Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
get方法在php rest web服务上不起作用_Php_Mysql_Rest - Fatal编程技术网

get方法在php rest web服务上不起作用

get方法在php rest web服务上不起作用,php,mysql,rest,Php,Mysql,Rest,对不起,我尝试使用请求方法POST和GET来创建restful web服务,我最终成功地使用GETmethod在mysql上查看数据,但当我使用POST方法搜索数据或删除数据时,使用POST方法的任何操作都不起作用。我不知道为什么。。。查清这个 private function users(){ // Cross validation if the request method is GET else it will return "Not Acceptable" status

对不起,我尝试使用请求方法POST和GET来创建restful web服务,我最终成功地使用GETmethod在mysql上查看数据,但当我使用POST方法搜索数据或删除数据时,使用POST方法的任何操作都不起作用。我不知道为什么。。。查清这个

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
}
users()和deleteUser()函数分别专门检查GET和DELETE的REQUEST_方法。因此,如果在POST完成时调用这些函数中的任何一个,它们都将返回
406

更新:在回答评论时:我应该怎么做才能解决这个问题

如果期望相同的函数与POST以及它们各自的主请求_方法一起使用,则需要允许验证POST请求方法

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->get_request_method() != 'POST')  {
        $this->response('',406);
    }

对于
deleteUser
函数,我假设
$this->\u请求
来源于$\u请求,并且可以从查询字符串或表单post获取
id


因为您只展示了这两个函数,所以如果请求是POST,我不能确定是否有其他代码会将操作重定向到其他地方。

您如何发出POST请求?另外,不要使用
mysql
扩展,使用
mysqli
PDO
和extensions-prepared语句代替。我发出一些POST请求来删除数据,我使用rest客户端chrome插件来测试我的web服务。我使用mysql作为数据库,什么是PDO扩展??抱歉,我真的是一个新的。就像mysql和mysqli是允许您访问mysql数据库的扩展一样,PDO是一个允许您访问任何具有相应方言扩展的数据库的扩展。取消php.ini中的
extension=php_pdo_mysql.dll
(或*.so)的注释,以启用,并使用链接中的类通过preparedStatements或查询访问数据库。很抱歉,我应该怎么做才能修复此问题?更新以显示进行后期处理所需的修改。感谢所有反馈,我终于成功删除了方法,我像这样更改$id:$id=ISSET($\u GET['user']),然后我将请求方法更改为GET not DELETE,:)那么删除HTTP请求方法web服务的功能是什么???如果我们可以使用GET HTTP request通过WEB服务删除数据,我们是否应该使用DELETE request_方法通过WEB服务删除数据??我不确定不使用GET删除web服务的数据。仅使用DELETE或POST(带有特定操作)删除web服务的数据。并使用GET获取web服务的数据。
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->get_request_method() != 'POST') {
        $this->response('',406);
    }