REST API
Data of your embedded device is stored in your database, you get your data from your embedded device, or to get data from your database to your website you need an interface; this interface is called API. API is a set of functions and procedures that allow the creation of applications that access the features or data of an operating system, application, or other service.
What is REST API?
REST stands for Representational State Transfer. It relies on a stateless, client-server, cacheable communications protocol and in virtually all cases, the HTTP protocol is used. REST is an architecture style for designing networked applications. The idea is that simple HTTP is used to make calls between machines.
Implementing REST API in PHP
In the next steps we will show one of the ways to implement REST API in PHP. First start with the desired architecture overview:
Your API
The simpelest form of an API is a PHP file that returns a JSON string. This JSON string can be read by your front-end. To get started with your API, create a file called api.php
in your web
folder. In this file you can write your PHP code.
1 2 3 |
|
To run this code, visit the page in your webbrowser. You can do this by going to http://localhost/api.php
in your webbrowser. You should see the text {"success":true}
on your screen. Now try to read this message from your WeMos.
Inserting data into your database using your API
To insert data you should do something like:
- Post data from your embedded device to your API.
- Read the data from the request in your API.
- Insert the data into your database.
- Return if the insert was successful or not.
To post data from your embedded device to your API you can make use of http.POST in your Arduino, some pseudo-code to make this work:
1 2 3 4 5 6 7 |
|
To read data from the request in your API you can make use of $_POST
in PHP. This is a global variable that contains all the data that was posted to your API.
1 2 3 4 5 6 |
|
To insert the data into your database you can make use of the mysqli
library in PHP. First you need to connect to your database and then you can insert the data into your database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Finally always make sure your API endpoints return a JSON string. This way you can easily read the response from your API in your embedded device.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Tips:
- To make your application safer, you should always escape your data before inserting it into your database. You can do this by using prepared statements.
- Sometimes a message like 'success' true/false is enough, but sometimes you want to return more data. You can do this by returning an array with the data you want to return. For example:
echo json_encode(array("success" => true, "data" => array("temperature" => $temperature)));
- To organize different endpoints you can create a folder 'api', and create files like 'insert.php', 'get.php', 'update.php', 'delete.php' in this folder. This way you can organize your code better.