Advertisement
Guest User

Untitled

a guest
Oct 5th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. <?php echo form_open('api/RestClient/PostCurl'); ?>
  2. <div class="form-group">
  3. <label for = "username"><h5>Username</h5>
  4. <input type = "text" name = "username" class="form-control" required
  5. oninvalid = "this.setCustomValidity('username is required')"
  6. oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
  7. </label>
  8. </div>
  9. <div class="form-group">
  10. <label for = "password"><h5>Password</h5>
  11. <input type = "password" name = "password" class="form-control" required
  12. oninvalid = "this.setCustomValidity('password is required')"
  13. oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
  14. </label>
  15. </div>
  16. <div class="form-group">
  17. <a href="<?php echo site_url('api/RestClient/PostCurl'); ?>"> <input id="loginsubmit" type = "submit" class="btn btn-success" class="form-control" value = "Login"/>
  18. </a>
  19. </div>
  20.  
  21. class LoginFormApi extends REST_Controller {
  22.  
  23. public function __construct() {
  24. parent::__construct();
  25. $this->load->helper('url');
  26. $this->load->library('form_validation');
  27. $this->load->model('LoginFormModel');
  28. $this->load->model('EmployeeAccountModel');
  29. }
  30.  
  31. public function LoginForm_post() {
  32. $userName = $this->post('username');
  33. $password = $this->post('password');
  34. $error = 'Invalid' . $userName;
  35. if (!$userName || !$password) {
  36. $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
  37. }
  38. $id = $this->LoginFormModel->Validate($userName, $password);
  39. if ($id) {
  40. $token['id'] = $id;
  41. $token['username'] = $userName;
  42. $date = new DateTime();
  43. $token['iat'] = $date->getTimestamp();
  44. $token['exp'] = $date->getTimestamp() + 60 * 60 * 5;
  45. $output['id_token'] = JWT::encode($token, "~~~JWT Auth Key !!!!!!~~~");
  46. $this->set_response($output, REST_Controller::HTTP_OK);
  47. } else {
  48. $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
  49. }
  50. }
  51.  
  52. class RestClient extends CI_Controller{
  53. public function __construct() {
  54. parent::__construct();
  55. $this->load->helper('url','form');
  56. $this->load->library('form_validation');
  57. $this->load->library('Curl');
  58. }
  59.  
  60. public function PostCurl(){
  61. $username = 'admin';
  62. $password = 'apitest@1234';
  63. $uname = $this->input->post('username');
  64. $pwd = $this->input->post('password');
  65. echo '~'.$uname.$pwd.'~';
  66. $curl_handle = curl_init('http://localhost:8085/ci-api/index.php/api/LoginFormApi/LoginForm');
  67. curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
  68. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
  69. $dataPost = array('username' => $uname,'password' => $pwd);
  70. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $dataPost);
  71. curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
  72. $data = curl_exec($curl_handle);
  73. curl_close($curl_handle);
  74. $result = json_decode($data);
  75. if(isset($result->status) && $result->status === 'success'){
  76. echo 'success' .$result;
  77. }else{
  78. $post = var_dump($dataPost);
  79. echo $post.'failure'.$result;
  80. }
  81. }
  82.  
  83. <script>
  84. function FormData(){
  85. var formData = {};
  86. formData.username = $('#username').val();
  87. formData.password = $('#password').val();
  88. $.ajax({
  89. url: 'http://localhost:8085/ci-api/index.php/api/RestClient/PostCurl',
  90. type: 'post',
  91. dataType: 'json',
  92. data: JSON.stringify(formData),
  93. ContentType: 'application/json'
  94. });
  95. }
  96. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement