I was wondering, how would one go about using Codeigniter's Form Validation with a Web Service? I'm using REST and cURL to connect with a Web Service and was trying to use the Form Validation on a login page. I can get it to return an error when either the Username or Password fields are blank, but am not sure how to use it to check that a Password matches. My cUrl code is as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->form_validation->set_rules('username', 'USERNAME', 'required|is_unique[username]');
$this->form_validation->set_rules('password', 'PASSWORD', 'required|matches[password]');
if ($this->form_validation->run() == FALSE) {
curl_close($ch);
$this->load->view('error_login');
} else {
$response = json_decode($output, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
$userName = $response["loginAccounts"][0]["userName"];
curl_close($ch);
$username = $userName;
$docusignURL = $baseUrl;
$docHeader = $header;
$loggedIn = array('is_logged_in' => true);
$this->session->set_userdata('username', $username);
$this->session->set_userdata('docusignURL', $docusignURL);
$this->session->set_userdata('docHeader', $docHeader);
$this->session->set_userdata($loggedIn);
redirect('/create_envelope', 'refresh');
}
Right now it's returning two Error Notices, "Undefined offset: 1" and "Undefined property: Login::$db". The latter because, I'm assuming, I'm not using a model to connect to any database. Any helpful advice would be outstanding.
EDIT: So I tried the callback route, but I'm getting 500 errors. Here is my entire controller code so far:
<?php
class Login extends CI_Controller {
function index() {
$this->load->view('al_login');
}
function authenticate(){
$post = $this->input->post();
extract($post);
$integratorKey = '****-********-****-****-****-************';
$header = "<DocuSignCredentials><Username>" . $username . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
$url = "https://demo.docusign.net/restapi/v2/login_information";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = json_decode($output, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
$userName = $response["loginAccounts"][0]["userName"];
curl_close($ch);
$username = $userName;
$docusignURL = $baseUrl;
$docHeader = $header;
$loggedIn = array('is_logged_in' => true);
$this->session->set_userdata('username', $username);
$this->session->set_userdata('docusignURL', $docusignURL);
$this->session->set_userdata('docHeader', $docHeader);
$this->session->set_userdata($loggedIn);
$this->form_validation->set_rules('username', 'USERNAME', 'required');
$this->form_validation->set_rules('password', 'PASSWORD', 'required|callback_password_check');
if ($this->form_validation->run() == FALSE) {
$this->load->view('al_login');
} else {
$this->load->view('/create_envelope');
}
}
function password_check($str){
// send the user name to the service
if ( $this->authenticate($str) == true ) {
return true;
} else {
// customize the message with something
$this->form_validation->set_message('password_check', 'The %s is incorrect. Please try again');
return false;
}
}
}
I'm new to REST,cURL and Codeigniter, so my knowledge could use help. Thanks to both of you so far. Any ideas why I'm getting 500 errors?
NEW EDIT: @Petre So this is the response from our web service:
Array (
[loginAccounts] => Array (
[0] => Array (
[name] => ********
[accountId] => *******
[baseUrl] => https://demo.docusign.net/restapi/v2/accounts/*******
[isDefault] => true
[userName] => **** ********
[userId] => ********-****-****-****-************
[email] => ****@******.com
[siteDescription] =>
)
)
)
Doesn't seem to be anything of the sort.