gpt4 book ai didi

php - $this->post codeigniter 不适用于rest api

转载 作者:行者123 更新时间:2023-12-03 19:15:16 24 4
gpt4 key购买 nike

我尝试与 $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);
}
}

}

关于php - $this->post codeigniter 不适用于rest api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17490174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com