Php CodeIgniter将POST数据从RestClient传递到RestServer API
我花了一整天的时间试图解决这个问题。在这里发布这个问题是我最后的希望。我希望有人能帮助我继续我的第一份工作 因此,当直接将数据从我的视图传递到RestServer时,POST工作正常。但是,RESTServer API无法找到从RestClient发送的POST数据 以下是片段:Php CodeIgniter将POST数据从RestClient传递到RestServer API,php,codeigniter,codeigniter-2,rest-client,codeigniter-restserver,Php,Codeigniter,Codeigniter 2,Rest Client,Codeigniter Restserver,我花了一整天的时间试图解决这个问题。在这里发布这个问题是我最后的希望。我希望有人能帮助我继续我的第一份工作 因此,当直接将数据从我的视图传递到RestServer时,POST工作正常。但是,RESTServer API无法找到从RestClient发送的POST数据 以下是片段: RestClient API: $ver = 'v1'; $config = array('server' => base_url()."v1",
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请求,RestServer仍然无法从我的RestClient接收数据
如果你还需要我提供什么,请询问
提前感谢。在RestServer API上,它将无法访问POST数据,RestClient API中的POST数据通过$param数组传递到RestServer API,RestServer API将接收该数组。在RestServer API中,您应该能够通过$param数组访问这些值,假设RestServer API函数使用$param数组并将其传递。我计算出传递第三个参数的时间和空值(或删除第三个参数的时间),然后它就可以工作了
//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);
如果有人有更好的解决方案,我可以奖励赏金 使用此方法可以使用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);
//控制者代码雇主/postNewProject
控制器
function postNewProject()
{
$data=array(
'id' => $this->post('id'),
'name' => $this->post('name')
);
print_r($data);
}
确保收到数据,使用
echo '<pre> all data received: '.print_r($_REQUEST,1).'</pre>';
echo“接收到的所有数据:”。打印($请求,1)。”;
确保是从邮局寄来的
$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');
echo“post数据:”。打印($\u post,1)。”;
内容类型应用程序/x-www-form-urlencoded用于post方法
post方法的内容类型json
将为post更改RestServer API
您能告诉我如何在RestServer上访问
$param
吗?实际上,在CodeIgnitor中,您应该能够通过$this->post('id')访问post变量;因此,我相信可能请求的其余url格式不正确。您是否有权访问RestAPI服务器日志以查看请求的url?RestAPI服务器日志是指codeigniter日志,对吗?我检查过了,我的api没有任何错误。您是作为web服务器运行apache还是nginx?检查访问日志。。。这样您就可以看到服务器上正在访问的url。。。这样,我们就可以查看传入的API请求,并仔细检查url格式,尝试使用set格式,只使用类似json的$this->rest->format('json')代码>
$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');
public function postNewProject_post()
{
$entity = file_get_contents('php://input', 'r');
print_r($entity);
}