我正在尝试创建和使用一个web服务,它将在json中输入并提供相同的输出。 但它无法正确提供输出。
function login() {
$data = array(
'login_id' => $this -> input -> post('login_id'),
'login_pwd' => md5($this -> input -> post('login_pwd')),
);
$data_string = json_encode($data);
echo $data_string;
$ch = curl_init(base_url().'admin_service/getUser');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contenttype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if ($result) {
redirect(base_url() . '/success');
} else {
redirect(base_url(). 'relogin');
}
}
}
以下代码适用于我的网络服务:
function getUser_post() {
$name = $this->post('login_id');
$password = $this->post('login_pwd');
$this -> load -> model('register_model');
// calling the function in our login model
$result = $this -> register_model -> get_user($name, $password);
echo $result;
return json_encode( array('result' => $result ) );
}
问题是:我没有在控制器中获得正确的json响应。我得到的只是1。
我想从webservice获取更多信息。我怎么能从这里的webservice发回来。
任何帮助表示赞赏。 谢谢。
答案 0 :(得分:1)
此CI休息服务器非常好用,在大多数情况下都可以使用:https://github.com/philsturgeon/codeigniter-restserver您必须调整代码,但这可能会对您有所帮助。我绝对可以推荐它!
答案 1 :(得分:0)
这应该这样做。您需要回显json_encode
结果,而不是可能是布尔值的$result
。
function getUser_post() {
$name = $this->post('login_id');
$password = $this->post('login_pwd');
$this -> load -> model('register_model');
// calling the function in our login model
$result = $this -> register_model -> get_user($name, $password);
echo json_encode( array('result' => $result ) );
return $result;
}
答案 2 :(得分:0)
function contact()
{
if ($this->isPOST())
{
$this->form_validation->set_rules('nMobile', 'mobileno', 'trim|required');
$this->form_validation->set_rules('vEmail', 'email', 'trim|required');
$this->form_validation->set_rules('vMessage', 'message', 'trim|required');
$this->form_validation->set_rules('nUserId', 'Userid', 'trim|required');
//$this->form_validation->set_rules('isActive', 'isActive', 'trim|required');
if ($this->form_validation->run())
{
$nMobile = $this->input->post('nMobile');
$vEmail = $this->input->post('vEmail');
$vMessage = $this->input->post('vMessage');
$nUserId = $this->input->post('nUserId');
date_default_timezone_set('Asia/Kolkata');
$dCreatedDate = date('Y-m-d H:i:s');
//$isActive = $this->input->post('isActive');
if(!$nMobile || !$vEmail || !$vMessage || !$nUserId)
{
$this->response("Enter Complete User contact information to Save. Field Missing", 201);
}
else{
$result = $this->User->usercontact(array( "nMobile"=>$nMobile, "vEmail"=>$vEmail, "vMessage"=>$vMessage, "nUserId"=>$nUserId, "dCreatedDate"=>$dCreatedDate, "isActive"=>1));
if($result === 0)
{
$this->response("User contact information could not be saved. Try again.", 404);
}
else{
$this->status = Ws_Controller::HTTP_OK;
$this->responseData = array();
$this->message = "User contact information successfully inserted";
}
}
} else
{
$this->status = Ws_Controller::HTTP_BAD_REQUEST;
$this->message = strip_tags($this->form_validation->error_string());
}
}
else
{
$this->status = Ws_Controller::HTTP_METHOD_NOT_ALLOWED;
$this->message = lang('err_invalid_method');
}
}