【问题标题】:CodeIgniter Passing POST data from RestClient to RestServer APICodeIgniter 将 POST 数据从 RestClient 传递到 RestServer API
【发布时间】:2014-09-01 04:06:18
【问题描述】:

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

所以,当直接将数据从我的视图直接传递到 RestServer 时,POST 可以正常工作。但是,RESTServer API 无法找到从 RestClient 发送的 POST 数据。

这里是sn-ps:

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 接收数据。

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

提前谢谢你。

【问题讨论】:

  • 尝试使用只有 json 的 set 格式,例如 $this->rest->format('json');

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


【解决方案1】:

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

// 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

控制器

function postNewProject()
{
    $data=array(
        'id' => $this->post('id'),
        'name' => $this->post('name')
        );
      print_r($data);
}

【讨论】:

    【解决方案2】:

    我弄清楚何时传递了空的第三个参数(或者当你删除第三个参数时)然后它就可以工作了。

        //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);
    

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

    【讨论】:

      【解决方案3】:

      确保您收到数据,使用

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

      确保来自帖子

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

      【讨论】:

        【解决方案4】:

        用于发布方法的内容类型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');
        

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 你能告诉我如何访问 RestServer 上的$param 吗?
          • 其实在 CodeIgnitor 中,你应该可以通过 $this->post('id');因此,我被认为可能请求的其余 url 格式错误。您是否有权访问 RestAPI 服务器日志以查看请求的 url?
          • RestAPI 服务器日志是指 codeigniter 日志,对吗?我确实检查过,我的 api 没有错误。
          • 您是在运行 apache 还是 nginx 作为 Web 服务器?检查访问日志...这样您就可以看到服务器上正在访问哪些 url...这样我们就可以查看传入的 API 请求并检查 url 格式
          猜你喜欢
          • 2018-02-03
          • 2017-08-09
          • 2016-03-08
          • 2014-12-29
          • 2015-08-17
          • 1970-01-01
          • 1970-01-01
          • 2013-10-21
          • 2018-02-17
          相关资源
          最近更新 更多