Php $this->;编码后点火器don';不能使用RESTAPI

Php $this->;编码后点火器don';不能使用RESTAPI,php,codeigniter,rest,post,Php,Codeigniter,Rest,Post,我尝试使用$this->post()获取post以json格式发送的数据。例如,$this->post('name'),我无法得到任何结果 代码如下: <?php require(APPPATH . '/libraries/REST_Controller.php'); class user_api extends REST_Controller { function user_post() { $user = array(

我尝试使用
$this->post()
获取post以json格式发送的数据。例如,
$this->post('name')
,我无法得到任何结果

代码如下:

<?php

require(APPPATH . '/libraries/REST_Controller.php');

class user_api extends REST_Controller {

    function user_post() {

                $user = array(
                    'id' => null,
                    'firstname' => $this->post('firstname'),
                    'lastname' => $this->post('lastname')
                );

                $result = $this->user_model->insert($user);
                if ($result) {
                    $this->response($result, 200); // 200 being the HTTP response code
                } else {
                    $this->response(NULL, 404);
                }
    }

}

?>
数据库中没有数据,无法从
$this->post('firstname')
获取数据


有什么想法吗???

json数据你不能通过post方法获得,你必须这样使用

require(APPPATH . '/libraries/REST_Controller.php');

class user_api extends REST_Controller {

    function user_post() {

       $params = json_decode(file_get_contents('php://input'), TRUE);

        $user = array(
            'id' => null,
            'firstname' => $params['firstname'],
            'lastname' => $params['lastname']
        );

        $result = $this->user_model->insert($user);
        if ($result) {
            $this->response($result, 200); // 200 being the HTTP response code
        } else {
            $this->response(NULL, 404);
        }
    }

}

我认为您没有使用正确的函数来获取所发布数据的值,但您正在尝试进入
$this->post('name')

您应该使用CI的方法来获得如下值

$this->input->post('name');


您应该使用CodeIgniter查看一下

,您可以这样访问post变量

$this->input->post('variable_name');
如果你有

$config['global_xss_filtering'] = TRUE;

在您的配置文件中,它将停止跨站点脚本编写,因此当您在URL中为POST to
CI RestController
传递参数时,您需要使用
//You should use

$array=array(
'firstName'   => $this->input->post("firstName"),
'lastName'   => $this->input->post("lastName")
);
$this->input->get('id')
而不是
$this->get('id')
[即,如果在url中传递了id]

$this->get('id')
不适用于POST URL参数(例如:
http://abcd.xyz.com/get_stuff?id=1


尽管由于某种原因,
$this->get('id')
似乎与get一起工作

在codeigniter中使用REST编写Web服务时,我遇到了同样的问题

解决方案==>您应该在头请求中设置内容类型

-因为您是以json格式发送post数据,所以应该将内容类型设置为application/json


-如果您在任何rest客户端中测试响应,请在rest API中将内容类型设置为“application/x-www-form-urlencoded”

=>若要使用get方法从服务器获取数据,请使用以下代码

$user = json_decode(
    file_get_contents('http://example.com/index.php/api/user/id/1/format/json')
);

echo $user->name;
=>若要向服务器插入/更新数据,则需要使用如下表单操作

<form method="post" id="validateFrm" action="http://example.com/index.php/api/create">
    <div class="row">
        <label>Name*</label>
        <input type="text" class="input-field" name="name" id="name" /> 
    </div>
    <div class="row">
         <label>Email</label>
         <input type="text" class="input-field" name="email" /> 
    </div>
    <div class="row last-row">
        <input type="submit" name="submit" value="submit">
    </div>
</form>

享受

这里存在两种情况。 如果您从服务器获取数据,则需要设置格式:

[适用于REST客户端]

$this->rest->format('application/json');
$this->rest->format('application/x-www-form-urlencoded');
[用于Android截击]

public Map<String, String> getHeaders() throws AuthFailureError
headers.put("Content-Type", "application/json; charset=UTF-8");
}
public Map<String, String> getHeaders() throws AuthFailureError
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
[用于Android截击]

public Map<String, String> getHeaders() throws AuthFailureError
headers.put("Content-Type", "application/json; charset=UTF-8");
}
public Map<String, String> getHeaders() throws AuthFailureError
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
publicmap getHeaders()抛出AuthFailureError
headers.put(“内容类型”、“应用程序/x-www-form-urlencoded;字符集=UTF-8”);
}

您应该这样编码:

$data=array{

'firstname' => $this->input->('firstname'),
'email' => $this->input->post('email')
}

$this->user_model->update($data);*emphasized text*
public Map<String, String> getHeaders() throws AuthFailureError
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
$data=array{

'firstname' => $this->input->('firstname'),
'email' => $this->input->post('email')
}

$this->user_model->update($data);*emphasized text*