php - form validation always returns false
I have the following method in a controller and i invoke the call to this method by doing the following
<?php
class dcode {
public $api_url = 'http://www.jasafollower.com/apii/'; // API URL
public $api_key = 'bXV6Z2ttNzNkUysyb1dlcmlpL2FuZz09'; // SECRET KEY
public $api_id = '745b10-edd123-8d71b3-cd2187-f48a74'; // API KEY
public function profile() {
return json_decode($this->connect($this->api_url.'profile', array('api_key' => $this->api_key, 'api_id' => $this->api_id)));
}
public function services() {
return json_decode($this->connect($this->api_url.'services', array('api_key' => $this->api_key, 'api_id' => $this->api_id)));
}
public function order($data) {
return json_decode($this->connect($this->api_url.'order', array_merge(array('api_key' => $this->api_key, 'api_id' => $this->api_id), $data)));
}
public function status($order_id) {
return json_decode($this->connect($this->api_url.'status', array('api_key' => $this->api_key, 'api_id' => $this->api_id, 'id' => $order_id)));
}
private function connect($end_point, $post) {
$_post = Array();
if (is_array($post)) {
foreach ($post as $name => $value) {
$_post[] = $name.'='.urlencode($value);
}
}
$ch = curl_init($end_point);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if (is_array($post)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
$result = curl_exec($ch);
var_dump($result);
if (curl_errno($ch) != 0 && empty($result)) {
$result = false;
}
curl_close($ch);
return $result;
}
}
$api = new dcode();
// cek profil
$profile = $api->profile();
var_dump($profile);
print('<br /><br />');
here is the method
public function profile() {
// filter input = 1
header('Content-Type: application/json');
$result = ['status' => false, 'data' => 'Permintaan salah'];
if ($this->input->post()) {
$this->form_validation->set_rules('api_id', 'API ID', 'required');
$this->form_validation->set_rules('api_key', 'API KEY', 'required');
if ($this->form_validation->run() == true) {
I am pulling out my hair on figuring out why form_validation->run() always returns false here.. any idea why ?
Answer
Solution:
You did not load the form_validation library:
$this->load->library('form_validation');
Source