$ this-> input-> post () возвращает NULL в реализации API codeigniter rest - PullRequest
0 голосов
/ 04 октября 2018

В файле RestClient.php строки $ uname = $ this-> input-> post ('username') и $ pwd = $ this-> input-> post ('password') возвращают NULL.

Это мой (View) loginform.php

    <?php echo form_open('api/RestClient/PostCurl'); ?>
    <div class="form-group">
        <label for = "username"><h5>Username</h5>
            <input type = "text" name = "username" class="form-control" required
                   oninvalid = "this.setCustomValidity('username is required')"
                   oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
        </label>
    </div>
    <div class="form-group">
        <label for = "password"><h5>Password</h5>
            <input type = "password" name = "password" class="form-control" required
                   oninvalid = "this.setCustomValidity('password is required')"
                   oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
        </label>
    </div>
    <div class="form-group">
        <a href="<?php echo site_url('api/RestClient/PostCurl'); ?>"> <input id="loginsubmit" type = "submit" class="btn btn-success" class="form-control" value = "Login"/>
        </a>
    </div>

Это мой контроллер API RestServer - LoginFormApi.php

    class LoginFormApi extends REST_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model('LoginFormModel');
    $this->load->model('EmployeeAccountModel');
}

public function LoginForm_post() {
    $userName = $this->post('username');
    $password = $this->post('password');
    $error = 'Invalid' . $userName;
    if (!$userName || !$password) {
        $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
    }
    $id = $this->LoginFormModel->Validate($userName, $password);
    if ($id) {
        $token['id'] = $id;
        $token['username'] = $userName;
        $date = new DateTime();
        $token['iat'] = $date->getTimestamp();
        $token['exp'] = $date->getTimestamp() + 60 * 60 * 5;
        $output['id_token'] = JWT::encode($token, "~~~JWT Auth Key !!!!!!~~~");
        $this->set_response($output, REST_Controller::HTTP_OK);
    } else {
        $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
    }
}

Это мой клиентский контроллер запросов с использованием cURL - RestClient.php строки $ uname = $ this-> input-> post ('username') и $ pwd = $ this-> input-> post ('password') возвращают NULL.

    class RestClient extends CI_Controller{
public function __construct() {
    parent::__construct();
    $this->load->helper('url','form');
   $this->load->library('form_validation');
    $this->load->library('Curl');
}

public function PostCurl(){
    $username = 'admin';
    $password = 'apitest@1234';
    $uname = $this->input->post('username');
    $pwd = $this->input->post('password');
    echo '~'.$uname.$pwd.'~';
    $curl_handle = curl_init('http://localhost:8085/ci-api/index.php/api/LoginFormApi/LoginForm');
    curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    $dataPost = array('username' => $uname,'password' => $pwd);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $dataPost);
    curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
    $data = curl_exec($curl_handle);
    curl_close($curl_handle);
    $result = json_decode($data);
    if(isset($result->status) && $result->status === 'success'){
        echo 'success' .$result;
    }else{
        $post = var_dump($dataPost);
        echo $post.'failure'.$result;
    }
}

}

1 Ответ

0 голосов
/ 05 октября 2018

Я попробовал следующий фрагмент js в представлении, и это сработало, данные поста доступны в контроллере RestClient.php:

    <script>
        function FormData(){
            var formData = {};
            formData.username = $('#username').val();
            formData.password = $('#password').val();
            $.ajax({
               url: 'http://localhost:8085/ci-api/index.php/api/RestClient/PostCurl',
               type: 'post',
               dataType: 'json',
               data: JSON.stringify(formData),
               ContentType: 'application/json'
            });
        }
    </script>
...