Microservice Magic: PHP & JavaScript

preview_player
Показать описание
Let's create a simple example of a microservice using Plain PHP and Plain JavaScript. We'll build a basic API that returns a greeting message.

1. *Backend (PHP):*

?php

// Define a function to generate a greeting message
function getGreeting() {
return "Hello from the microservice!";
}

// Check if the request is for the greeting
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'greeting') {
header('Content-Type: application/json');
echo json_encode(['message' = getGreeting()]);
} else {
http_response_code(404);
echo "Not Found";
}
?


2. *Frontend (JavaScript):*

!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
meta name="viewport" content="width=device-width, initial-scale=1.0"
titleMicroservice Example/title
/head
body
h1Microservice Greeting/h1
p id="greeting"/p

script
// Fetch the greeting from the microservice
.then(data = {
})
.catch(error = {
});
/script
/body
/html


That's it! You've created a simple microservice using Plain PHP and Plain JavaScript. Feel free to expand on this example by adding more functionality or integrating with a database. 😊
Рекомендации по теме
join shbcf.ru