'how to send api response array in code codeigniter?
I am creating api in codeigniter in which i am checking both email and mobilenumber field for updating & in that check it for both field. my single message is go to response but in both casess how can i send both email and mobile number is exists
if((isset($postData['email'])) && $postData['email']!="")
{
$message["email_unique"]='Email already exists';
return $this->response(array("status" => "400", "message" =>$message), 400);
}
if((isset($postData['mobileNumber'])) && $postData['mobileNumber']!="")
{
$mobilenumber=$postData['mobileNumber'];
$message["mobilenumber_unique"]='Mobile Number already exists';
return $this->response(array("status" => "400", "message" =>$message), 400);
}
Solution 1:[1]
Check this code
"400", "message" =>$message ]); exit; } else { $message['success'] = "Successful"; // success messsage if needed $data = null; // whatever data you need to pass in success resposne - either fetch from Database or hardcoded // validated flow echo json_encode([ "status" => "200", "message" =>$message, "data" => $data ]); } ?>==================================
Success resposne:
{"status":"200","message":{"success":"Successful"},"data":null}
Failed response:
{"status":"400","message":{"error_email":"Email address required.","error_mobile":"Mobile number required."}}
If email exists and mobile number empty then
Failed response:
{"status":"400","message":{"error_mobile":"Mobile number required."}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
