Codeigniter没有收到POST数据

时间:2015-05-11 07:13:34

标签: php codeigniter post

我正在对我的网站发出POST请求,但是当我调试$_POST$this->input->post()时,它们都是一个空数组。我将global_xss_filtering设置为FALSE。我还在index.php的顶部放了一个调试行,但是那个也返回一个空的$_POST数组。

我的POST请求的标头(从postcatcher.in收到):

{
    "content-length": "164",
    "total-route-time": "0",
    "x-request-start": "1431326903012",
    "connect-time": "1",
    "via": "1.1 vegur",
    "x-forwarded-port": "80",
    "x-forwarded-proto": "http",
    "x-forwarded-for": "213.124.141.66",
    "x-request-id": "d7f56276-afb5-463c-acf1-9632acc27d9d",
    "accept-encoding": "gzip",
    "user-agent": "Dalvik/2.1.0 (Linux; U; Android 5.1; Nexus 5 Build/LMY47I)",
    "content-type": "application/json;charset=UTF-8",
    "accept": "application/json",
    "connection": "close",
    "host": "postcatcher.in"
}

我发送的POST数据:

{
    "phonenumber": "1234356",
    "organisation_id": 0,
    "location_id": 0,
    "lastname": "Bergmans",
    "id": 0,
    "firstname": "Bart",
    "email": "my@emailadress.com",
    "active": false
}

我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Api extends MY_Controller {

        private $client;

        private function connectSoap()
        {
                if($this->client == null) {
                    $this->client = new SoapClient($this->config->item('wsdl_location'));
                }
        }

        public function editUserData($id) {
               error_log(print_r($this->input->post(),true));

               $return = array();
               $return['success'] = 'true';
               $this->outputJson($return);
        }

        private function outputJson($array) 
        {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                            $array
                    ));
        }
}

1 个答案:

答案 0 :(得分:1)

contentType接受application / json,这意味着请求正在发送json数据,这在你的情况下是不正确的,这就是没有收到数据的原因。

你有$ _POST空。如果您的Web服务器想要以json格式查看数据,则需要读取原始输入,然后使用JSON解码对其进行解析。

你需要这样的东西:

$json = file_get_contents('php://input');
$obj = json_decode($json);