'php codeginter 3 mobile restapi
I was working restapi chat message using Php Codeigniter 3 but the issue is that if the user invite to another user and accepted it then he directs to chat message but he did not work can you plz help me how to resolve this issue I have shared the code so plz help this code and how to test and check to chat message API using postman plz give me the guidance
public function sendmassage()
{
$key = $this->input->get_request_header('API-KEY');
if (!isset($key)) {
$this->output
->set_content_type('application/json')
->set_status_header(404)
->set_output('Api Key Not Found');
} else {
if ($key != 'y1y4g5x1z65x15z8x4z654x56z47x84z65x486z47x947z9') {
$this->output
->set_content_type('application/json')
->set_status_header(401)
->set_output('Wrong Api Key');
} else {
$invite_id = $this->input->post('invite_id');
$message = $this->input->post('message');
$user_id = $this->input->post('user_id');
$sender = $this->input->get('user_id');
$data = array(
'invite_id' => $invite_id,
'sender' => $sender,
'message' => $message,
'user_id' => $user_id,
'date' => date('y-m-d H:i:s')
);
$this->security->xss_clean($data);
$this->db->insert('massage', $data);
$massage_id = $this->db->insert_id();
if ($massage_id) {
$this->output
->set_content_type('application/json')
->set_status_header(200)
->set_output('Message send');
} else {
$this->output
->set_content_type('application/json')
->set_status_header(202)
->set_output('Message sending failed');
}
}
}
}
public function get_my_invite()
{
$key = $this->input->get_request_header('API-KEY');
if (!isset($key)) {
$this->output
->set_content_type('application/json')
->set_status_header(404)
->set_output('Api Key Not Found');
} else {
if ($key != 'y1y4g5x1z65x15z8x4z654x56z47x84z65x486z47x947z9') {
$this->output
->set_content_type('application/json')
->set_status_header(401)
->set_output('Wrong Api Key');
} else {
$chatarray = array();
$user_id = $this->input->get('user_id');
$query = $this->db->query("SELECT
users.*,
invite.*
FROM
invite
LEFT JOIN users ON invite.invite_from = users.user_id
WHERE
invite.status = 'pending' AND
invite.invite_to ='". $user_id."'");
$invites = $query->result_array();
foreach ($invites as $invite) :
$invite_id = $invite['invite_id'];
$chatnewval = array();
// AND users.user_id ='". $user_id."'"
//array_push($chatnewval, $chat);
$chatnewval = array_merge($chatnewval, $invite);
// $this->db->select("
// massage.message as message,
//massage.date as date,
// ");
//users.*
$this->db->where('invite_id', $invite_id);
$chatlastmessage = $this->db->get('massage')->last_row();
$chatnewval['lastmessage'] = $chatlastmessage;
array_push($chatarray, $chatnewval);
$this->output
->set_content_type('application/json')
->set_status_header(200)
->set_output(json_encode($chatarray));
endforeach;
}
}
}
public function get_messages()
{
$key = $this->input->get_request_header('API-KEY');
if (!isset($key)) {
$this->output
->set_content_type('application/json')
->set_status_header(404)
->set_output('Api Key Not Found');
} else {
if ($key != 'y1y4g5x1z65x15z8x4z654x56z47x84z65x486z47x947z9') {
$this->output
->set_content_type('application/json')
->set_status_header(401)
->set_output('Wrong Api Key');
} else {
$invite_id = $this->input->get('invite_id');
$sender = $this->input->get('user_id');
//if ($invite_id <= 0) {
// $invite_id = '0';
// }
// $query = "SELECT * FROM massage WHERE invite_id".$invite_id." AND sender".$sender." ORDER BY massage_id DESC";
$this->db->where('invite_id', $invite_id);
$this->db->where('sender', $sender);
$this->db->order_by("massage_id", "desc");
$query = $this->db->get('massage');
$data = $query->result_array();
$this->output
->set_content_type('application/json')
->set_status_header(200)
->set_output(json_encode($data));
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
