php – CodeIgniter将POST数据从RestClient传递给RestServer API

我花了一整天的时间来解决这个问题.在这里发表这个问题是我最后的希望.我希望有人可以帮助我继续从事我的第一份工作.

因此,直接将数据从我的视图传递到RestServer时,POST工作正常.但是,RESTServer API无法找到从RestClient发送的POST数据.

以下是代码段:

RestClient API:

        $ver = 'v1';
        $config = array('server'          => base_url()."v1",
                        'api_key'         => 'xxxxxx',
                        'api_name'        => 'X-API-KEY',
                        'http_user'       => 'admin',
                        'http_pass'       => 'xxxxx',
                        'http_auth'       => 'basic',
                );

        $this->rest->initialize($config);
        $this->rest->format('application/json');
        $param = array(
                'id' => $this->input->post('id'), // works fine here
                'name' => $this->input->post('name')
                );
        $user = $this->rest->post('employer/postNewProject', $param, 'json');


        //if (isset($user->errors)) show_404('admin');
        $this->rest->debug();

RestServer API

class Employer extends REST_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->lang->load("application");
        $this->load->library("users/auth"); 
        Datamapper::add_model_path( array( APPPATH."modules" ) );
    }

    public function postNewProject_post()
    {  
        // I tired $this->post() and $this->input->post() but both not finding the data
        $message = array("id" => $this->post("id"), "name" => $this->input->post("name"));
        $this->response($message, 200); // 200 being the HTTP response code
    }
}

结果:

Response when using $this->post('id');
{"id":false}

Response when using $this->post();
{"id":[]}

注意:我用硬编码的数据替换了POSTS请求,而RestServer仍然无法从RestClient接收数据.

如果你需要我提供其他的东西,请问.

先谢谢你.

使用此方法使用post方法将Rest客户端数据发送给rest server.阅读每一行的评论

// initialize you setting 
     $config = array('server' => base_url() . "v1",
            'api_key' => 'xxxxxx',
            'api_name' => 'X-API-KEY',
            'http_user' => 'admin',
            'http_pass' => 'xxxxx',
            'http_auth' => 'basic',
        );
        $this->rest->initialize($config);
// Set method of sending data

        $method = 'post';
// create your param data

        $param = array(
            'id' => $this->input->post('id'), // works fine here
            'name' => $this->input->post('name')
        );
// url where you want to send your param data.

        $uri = 'employer/postNewProject';
        // set format you sending data
        $this->rest->format('application/json');

// send your param data to given url using this

        $result = $this->rest->{$method}($uri, $params);

//您的控制器雇主/ postNewProject的代码

调节器

function postNewProject()
{
    $data=array(
        'id' => $this->post('id'),
        'name' => $this->post('name')
        );
      print_r($data);
}
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/24682347/codeigniter-passing-post-data-from-restclient-to-restserver-api

转载注明原文:php – CodeIgniter将POST数据从RestClient传递给RestServer API