Back-end
The HTML, CSS and Javascript are the front-end of your website, there you have the look, feel and the user interaction. But to get the data from your embedded device to your website you need a back-end. The back-end is the part of the website that you cannot see and interact with. The back-end is the server side of your website, it is the part that communicates with the database and the embedded device.
In the Individual Project of Internet of Things you are building your back-end with PHP.
What is PHP?
PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP scripts can only be interpreted on a server that has PHP installed, for you this is already done in your Docker container. To get started you only have to create a '.php'-file in your web
folder, and visit this page in your webbrowser.
Hello world!
To get started with PHP, create a file called hello.php
in your web
folder. In this file you can write your PHP code. To print something to the screen you can use the echo
function.
1 2 3 |
|
To run this code, visit the page in your webbrowser. You can do this by going to http://localhost/hello.php
in your webbrowser. You should see the text Hello world!
on your screen.
Tips
You can use PHP to handle your API-requests. You can think of:
- From PHP you can connect to your own database to store or read data.
- (Make sure you use
mariadb
as hostname in your PHP-file to connect to your database!)
- (Make sure you use
- You can make a HTTP Post request to your PHP-file and read the data from the request by using the
$_POST
variable. - Every request from your API should return a JSON string, you can use the
json_encode
function to convert an array to a JSON string. - Don't mix PHP with other languages, if you need data in your front-end make use of AJAX/fetch/....
- Need a database connection in multiple PHP-files? Create a
db.php
file and include this file in your other PHP-files.