2

我正在尝试创建和使用一个 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。

我想从网络服务中获取更多信息。我怎样才能从这里的网络服务发回。

任何帮助表示赞赏。谢谢你。

4

3 回答 3

1

这个 CI 休息服务器非常好,在大多数情况下都可以工作:https ://github.com/philsturgeon/codeigniter-restserver你必须调整你的代码,但也许这会对你有所帮助。我绝对可以推荐它!

于 2013-09-12T20:22:43.900 回答
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;

}
于 2013-09-12T17:42:35.197 回答
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');
    }

}
于 2017-12-14T09:41:35.053 回答