webservices in PHP and json using codeigniter
13,920
Solution 1
This should do it. You need to echo the json_encode
result back, not the $result
which is probably a boolean.
function getUser_post() {
$name = $this->post('login_id');
$password = $this->post('login_pwd');
$this -> load -> model('register_model');
// calling the function in our login model
$result = $this -> register_model -> get_user($name, $password);
echo json_encode( array('result' => $result ) );
return $result;
}
Solution 2
This CI rest server is quite nice and works in most cases: https://github.com/philsturgeon/codeigniter-restserver You'll have to adapt your code, but maybe this will help you nevertheless. I can definitely recommend it!
Author by
sandeep
Updated on June 04, 2022Comments
-
sandeep about 2 years
I am trying to create and use a webservice which will take input in json and provide output in same. But it is not able to provide the output correctly.
function login() { $data = array( 'login_id' => $this -> input -> post('login_id'), 'login_pwd' => md5($this -> input -> post('login_pwd')), ); $data_string = json_encode($data); echo $data_string; $ch = curl_init(base_url().'admin_service/getUser'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contenttype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); if ($result) { redirect(base_url() . '/success'); } else { redirect(base_url(). 'relogin'); } } }
Below code is for my webservice:
function getUser_post() { $name = $this->post('login_id'); $password = $this->post('login_pwd'); $this -> load -> model('register_model'); // calling the function in our login model $result = $this -> register_model -> get_user($name, $password); echo $result; return json_encode( array('result' => $result ) ); }
Problem is: I am not gettign proper json response back in controller. All I am gettting is 1.
I want to get additional information back from webservice. How can I send it back from webservice here.
Any help appreciated. Thank you.