繁体   English   中英

使用codeigniter的PHP和json中的webservices

[英]webservices in PHP and json using codeigniter

我正在尝试创建和使用网络服务,该服务将在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'); 
        }
    }
 }

以下代码适用于我的Web服务:

   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。

我想从Web服务获取更多信息。 我如何在这里从webservice发送回来。

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

这个CI rest服务器非常好,并且在大多数情况下都可以使用: https : //github.com/philsturgeon/codeigniter-restserver您必须调整您的代码,但是也许这将对您有所帮助。 我绝对可以推荐它!

这应该做。 您需要回显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;

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM