我想知道,如何将 Codeigniter 的表单验证与 Web 服务一起使用?我正在使用 REST 和 cURL 连接 Web 服务,并尝试在登录页面上使用表单验证。当用户名或密码字段为空时,我可以让它返回错误,但我不确定如何使用它来检查密码是否匹配。我的 cUrl 代码如下:
$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');
}
现在它返回两个错误通知,“未定义偏移量:1”和“未定义属性:Login::$db”。后者是因为,我假设,我没有使用模型连接到任何数据库。任何有用的建议都会非常出色。
编辑:所以我尝试了回调路由,但我收到了 500 个错误。到目前为止,这是我的整个控制器代码:
<?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;
}
}
}
我是 REST、cURL 和 Codeigniter 的新手,所以我的知识可以使用帮助。到目前为止,感谢你们俩。任何想法为什么我会收到 500 个错误?
新编辑: @Petre 所以这是我们网络服务的响应:
Array (
[loginAccounts] => Array (
[0] => Array (
[name] => ********
[accountId] => *******
[baseUrl] => https://demo.docusign.net/restapi/v2/accounts/*******
[isDefault] => true
[userName] => **** ********
[userId] => ********-****-****-****-************
[email] => ****@******.com
[siteDescription] =>
)
)
)
好像不是那种东西。