In this section, we're going to cover REST APIs.
We alluded to in the introduction.
So we're going to show how to create a REST API using PHP.
In particular, we're going to write some PHP code,
dive into the database,
pull some data, and then format it as JSON,
and return that as a JSON structure.
Admittedly, we're not going to cover this in too much detail,
we're just going to give you the framework PHP code.
But you can build upon this to do fancier stuff.
So Sergio will show you how to do that.
So we're logged back in to the EC2 instance that we had.
And I've created a database if we log
in here called "myapplication."
So we're going to use that one. And we have a table called "MyGuests".
So, if I show what's in here,
you can see that there's a table with user ID,
first name last name,
email, and the date it was created.
And there's three entries for now.
So that's what we're going to try to return in our JSON request.
Sorry, the request it's going to be returning this in JSON through the browser.
And it could be through anywhere. As long as you have this link,
it's going to return exactly this as a text.
I've created the file "show-all.php",
and what this is is the actual PHP that's going to return it.
It uses MySQLi, which is just basically MySQL for PHP.
And up here I've said what it needs,
the server name-we're on localhost.
I could have put the IP address that we use here earlier,
but it's just easier to write localhost because it's on the same machine.
The username is root. The past is the past with that I had denoted.
Now, it's important to also state here that you're explicitly writing a password here,
so it'd be important to make
sure that this user does not have write access to the database.
It's something that would be simple as going into it and removing the write access.
So you're only able to read,
so anyone who might somehow stumble upon
this PHP code won't be able to edit your database.
Or, another thing you could do is embed
this PHP somewhere so the code is not viewable at all.
And all it is is a button you click and it returns your JSON.
And the database name,
as I showed earlier, was "myapplication".
Right here, it creates the connection using the MySQLi that I was talking about.
You just input server name,
username, password, and the database name.
It does a quick check to make sure that it works and that it doesn't fail.
In case it does fail, it'll tell you why.
And here's the actual SQL statement.
It's the same one that I did earlier.
It's select all from "MyGuests",
and it's just going to return every entry in that "MyGuest" table.
The way you actually query it is you get your instance,
which I called "conn" for "connection",
and you say "conn - >query",
and then you write in here your SQL query.
I called it "SQL". So, you store that in something,
I'm calling that something "result."
I'm storing this query into "result".
And the way you work with it from here,
you can massage it into whatever you want it to be.
You can edit the table for whatever reason, you can add to it,
but I strictly just grabbed everything that it
returned and entered it into an array called "data".
That's what this while loop does here.
It just inserts all that data into the array.
And to create JSON from an array in PHP is as a simple as saying JSON in code,
and that's what we're going to return.
So if we run this function,
you'll notice that I put it in "users/show-all.php".
So if I type that in, /show-all.php it
returns this JSON object where it is the three entries that were in my database.
You could see this as user one,
this would be user two over here,
and then this would be user three on the bottom.
And that's how you do a very basic JSON.
To grab from the database,
turn that information into JSON and return it through your app.
In doing so what you have created is you have created your first REST API,
so if you highlight the URL there,
this is what you call to get back the JSON.
Admittedly, in this case,
we're not taking any parameters as input.
It'll return the same thing every time you call it.
However, you can modify it so it takes in some parameters,
GET POST parameters and based on the input it returns a different JSON.
But, bottom line, this how you'll create REST APIs,
and you would consume these APIs in your DragonBoard code,
you'll call this URL here, and you're going to get back this JSON,
and you process this JSON using a framework of your choice,
and you run with it.
So that's REST API,
how to create a REST API in a nutshell.