lec-php-09
...anyone want one?
is up
◉ Remember lab-01? You consumed an API!
◉ What's a prepared statement - and why should I use them?
◉ Can we clean this up at all?
◉ How do I do user authentication for the Project?
I didn't have time to cover how to add and delete rows in DB tables using PDO...and you totally need to do that for the Project!
Looks like you've got some investigation to do....
Yeah. You have to make that.
You might be wondering what it needs to hold.
Are you crazy?!?!? You NEVER store passwords in a database table!
Why is that a bad idea? 🤔
What should you store instead? 🤔
What's that? 🤔
It's what produced by a one-way math function that takes in text and spits out...hashed text.
hashing-playground/hash.php
It's a super-convenient method - that you must use!
hashing-playground/verify.php
⚠️Look for opportunities to make helper functions!
Where'd this word come from?
Let's go there twice.
I'll go once with a Guest session.
Then once again in my "normal" session.
I have a browser extension that will affect how the response is displayed.
Compare this to what we see if we go to
https://random-word-form.herokuapp.com
This time, we'll go here with the Thunder Client extension that we have in our lab and Project Codespaces.
Going to the headers in the extension, we again Content-Type is application/json, not text/html
https://rickandmortyapi.com/api/character/
https://rickandmortyapi.com/api/character/?name=morty&status=dead
-and-
In both of these examples, we have NOT been visiting web pages!
Instead, we are consuming a RESTful API.
An API - Application Programming Interface - is basically a set of rules for communicating with another system.
With our adjectives and Rick/Morty examples, the "rules" were URLs with a specific format.
Remember the Java API? Those are rules for communicating with the Java programming language.
The other side says, "Sure - as long as you go to the right URL, I'll give you the data you want."
BTW - we'll just use "API" from now on. The RESTful part will be assumed.
We'll call this "right URL" the API endpoint.
1. Validate the request.
2a. If the request isn't valid, respond with some kind of error in the desired format and with the desired status.
2b. If the request is valid, gather the information being requested, then respond in the desired format and with a 200 status.
Lab-05 gives a reasonable example of this.
The Project, however, needs some work!
I'll get on that soon.
How does https://random-word-form.herokuapp.com/random/adjective do it?
How does https://rickandmortyapi.com/api/character do it?
What content type are we getting? 🤔
What status are we getting? 🤔
How does https://random-word-form.herokuapp.com/random/adjective do it?
How does https://rickandmortyapi.com/api/character do it?
What content type are we getting? 🤔
What status are we getting? 🤔
Let's build an API endpoint that delivers how much of a given cheese is in stock at a given store.
If either parameter is missing or has an unknown value, we'll respond with a 404 status and the
JSON { "message": "Bad request."}
Otherwise, we'll respond with the
JSON {
"cheese": "<requested cheese>",
"stock": #
}
api/stock.php