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

17

我整天都在尝试解决这个问题。在这里发布这个问题是我的最后希望。我希望有人可以帮助我继续做我的第一份工作。

所以,当直接通过视图将数据传递给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":[]}

注意:我已经将POST请求替换为硬编码数据,但仍然无法从我的RestClient接收到数据。

如果你需要我提供其他任何东西,请告诉我。

谢谢你的帮助。


尝试使用仅JSON格式的设置格式,例如$this->rest->format('json'); - jagad89
5个回答

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

如果有更好的解决方案,我会授予奖金。

7
使用此方法使用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);
}

4
确保您接收到数据,使用
 echo '<pre> all data received: '.print_r($_REQUEST,1).'</pre>';

确保来自post请求

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

1

内容类型application/x-www-form-urlencoded用于post方法

 $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格式进行POST请求 RestServer API将会进行更改

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

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

你能展示一下如何在RestServer上访问$param吗? - Sobiaholic
实际上在CodeIgnitor中,你可以通过$this->post('id')访问post变量。因此,我认为可能请求的rest url格式不正确。你是否可以访问RestAPI服务器日志以查看请求的url是什么? - zbot
通过 RestAPI 服务器日志,您指的是 CodeIgniter 日志,对吗?我已经检查过了,我的 API 没有任何错误。 - Sobiaholic
你正在运行 Apache 或 Nginx 作为 Web 服务器吗?检查访问日志...这样你就可以看到服务器上正在访问的 URL...这样我们就可以查看进来的 API 请求并仔细检查 URL 格式。 - zbot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接