$this->;后代码点火器don';不能使用rest api


$this->post codeigniter don't work with rest api

我尝试使用$this->post()来获取json格式的post发送的数据。到目前为止,我无法得到任何结果,例如$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);
                }
    }
}
?>

以json格式发送到链接的数据:http://mydomain.com/myapplication/user_api/user:

{"firstname":"test",
"lastname":"test"
}

数据库中没有数据,就像它无法从$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);
        }
    }
}
//You should use
$array=array(
'firstName'   => $this->input->post("firstName"),
'lastName'   => $this->input->post("lastName")
);

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

您应该使用CI的方法来获得类似的值

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

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

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

您应该看看输入类

使用CodeIgniter可以访问类似的后期变量

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

如果你有

$config['global_xss_filtering'] = TRUE;

在您的配置文件中,它将停止跨站点脚本,因此要小心

当您将POST URL中的参数传递给CI RestController时,您需要使用$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配合使用。

在代码点火器中使用REST编写Web服务时,我也遇到了同样的问题。

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

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

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

     $postdata = file_get_contents("php://input");
      header('Content-type: application/json');
      $obj = json_decode($postdata,true);
      $firstname=$obj['firstname'];
      $lastname=$obj['lastname']; and pass to an array

IN REST API

=>如果您想使用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>

现在,您可以在控制器/方法中使用所有表单元素,如

$name = $this->post('name');
$email = $this->post('email');

享受吧!!

这里存在两种类型的条件。如果您从服务器获取数据,则需要设置格式:

[适用于REST客户端]

$this->rest->format('application/json');

〔适用于安卓Volley〕

public Map<String, String> getHeaders() throws AuthFailureError
headers.put("Content-Type", "application/json; charset=UTF-8");
}

如果您将数据从客户端发送到服务器,则需要设置格式:[适用于REST客户端]

$this->rest->format('application/x-www-form-urlencoded');

〔适用于安卓Volley〕

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*