jtp*_*ult 3 rest post json curl codeigniter
我正在使用 CodeIgniter 开发 RESTful 应用程序,但无法访问控制器中的 POST json 数据。
我正在本地机器上通过 cURL 发布 json,而应用程序正在远程服务器上开发。
这是有问题的控制器代码:
class Products extends CI_Controller
{
public function __construct()
{
$this->load->model(products_model);
}
public function index($id = FALSE)
{
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
// fetch product data
$product_data = $this->products_model->get_products($id)
// set appropriate header, output json
$this->output
->set_content_type(application/json)
->set_output(json_encode($product_data));
}
elseif($_SERVER['REQUEST_METHOD'] == 'POST')
{
// debugging for now, just dump the post data
var_dump($this->input->post());
}
}
}
Run Code Online (Sandbox Code Playgroud)
GET 操作运行良好,并在从浏览器或通过 cURL 请求请求时返回适当的数据。但是,当尝试通过 cURL POST json 数据时,我始终bool(FALSE)从 index 函数的 POST 部分返回。这是我提出的 cURL 请求:
curl -X POST -d @product.json mydomain.com/restfulservice/products
Run Code Online (Sandbox Code Playgroud)
此外,这里是 product.json 文件的内容:
{"id":"240",
"name":"4 x 6 Print",
"cost":"1.5900",
"minResolution":401,
"unitOfMeasure":"in",
"dimX":0,
"dimY":0,
"height":4,
"width":6}
Run Code Online (Sandbox Code Playgroud)
我通过 cURL 进行了另一个 POST,不包括 json 数据并传递如下内容:
curl -X POST -d '&this=that' mydomain.com/restfulservice/products
Run Code Online (Sandbox Code Playgroud)
哪个返回
array(1) {
["this"]=>
string(4) "that"
}
Run Code Online (Sandbox Code Playgroud)
What gives? Something with the json? It's valid. I've turned off the global CSRF and XSS in application/config/config.php as I understand they require use of CI's form_open() and won't work properly without it. It's my understanding that excluding parameters from $this->input->post() will return ALL the post items yet I continue to get none. I've also tried going around CI's input library and accessing the data via PHP's $_POST variable, it has made no difference.
您的帖子数据不是查询字符串格式,因此您应该跳过处理 $_POST 并直接转到原始帖子数据。
尝试
var_dump($HTTP_RAW_POST_DATA);
Run Code Online (Sandbox Code Playgroud)
甚至更好
var_dump(file_get_contents("php://input"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3905 次 |
| 最近记录: |