PHP Classes

File: appphp/send_message.php

Recommend this page to a friend!
  Classes of Rodrigo Faustino   Chat SSE   appphp/send_message.php   Download  
File: appphp/send_message.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Chat SSE
Exchange chat messages using server-sent events
Author: By
Last change:
Date: 1 month ago
Size: 769 bytes
 

Contents

Class file image Download
<?php

require 'vendor/autoload.php';
use
Chat\App\Chat;

header('Content-Type: application/json');

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
   
$data = json_decode(file_get_contents('php://input'), true);
   
$message = $data['message'] ?? '';
   
$user = $data['usuario'] ?? '';
   
$chat = new Chat();
   
$success = $chat->sendMessage($message,$user);

    if (
$success) {
        echo
json_encode(['status' => 'success', 'message' => 'Mensagem enviada com sucesso.']);
    } else {
       
http_response_code(500);
        echo
json_encode(['status' => 'error', 'message' => 'Falha ao enviar a mensagem.']);
    }
} else {
   
http_response_code(405);
    echo
json_encode(['status' => 'error', 'message' => 'Método de solicitação não permitido.']);
}