CodeIgniter将POST数据从RestClient传递到RestServer API

时间:2014-07-10 17:01:17

标签: php codeigniter codeigniter-2 rest-client codeigniter-restserver

我花了一整天时间试图找出这个问题。在这里发布这个问题是我最后的希望。我希望有人能帮助我继续完成我的第一份工作。

因此,当直接将数据从我的视图传递到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接收数据。

如果您需要我提供其他任何信息,请询问。

提前谢谢。

5 个答案:

答案 0 :(得分:7)

我想出什么时候和第三个参数被传递(或当你删除第三个参数时)然后它就可以了。

    //Not working
    $user = $this->rest->post('employer/postNewProject', $param, 'json');
    //when i change to this ,it's working
     $user = $this->rest->post('employer/postNewProject', $param.'');
     //or 
    $user = $this->rest->post('employer/postNewProject', $param);

如果有人有更好的解决方案,那么我可以奖励赏金。

答案 1 :(得分:7)

使用此方法使用post方法将Rest客户端数据发送到rest服务器。阅读每一行的评论

rvm get stable --auto-dotfiles

//您的控制器代码// 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);

<强>控制器

employer/postNewProject

答案 2 :(得分:4)

确保您收到数据,请使用

 echo '<pre> all data received: '.print_r($_REQUEST,1).'</pre>';

确保来自帖子

echo '<pre> post data: '.print_r($_POST,1).'</pre>';

答案 3 :(得分:1)

  

用于发布方法的内容类型 application / x-www-form-urlencoded

 $param = array(
            'id' => $this->input->post('id'), // works fine here
            'name' => $this->input->post('name')
            );

 $user = $this->rest->post('employer/postNewProject', $param, 'application/x-www-form-urlencoded');
  

发布方法的内容类型 json       RestServer API将更改为post

public function postNewProject_post()
{  
   $entity = file_get_contents('php://input', 'r');
   print_r($entity);
}

答案 4 :(得分:-2)

在您的RestServer API上,它无法访问POST数据,RestClient API的POST数据通过$ param数组传递到RestServer API,RestServer API接收该数组。在RestServer API中,您应该能够通过$ param数组访问这些值,假设RestServer API函数使用$ param数组并将其传递给它。