PHP REST API From Scratch [2] - Single & Create

preview_player
Показать описание
In this part of the series we will add functionality to fetch single posts as well as make a POST request to add posts

Code:

💖 Become a Patron: Show support & get perks!

Website & Udemy Courses

Follow Traversy Media:
Рекомендации по теме
Комментарии
Автор

Really great stuff! Just finished building API for a client that sends Woocommerce orders to their CMR. Before these 2 tutorials, I had no idea how to do that :). Of course a ton of StackOverflow reading was involved, but fundament came from here. Thank you very much for your great work!

somesome
Автор

If like me you are finicky about stuff, with the read_single function in the Post.php class you can add some code to stop the output of null JSON for when a user enters an ID that is not in the database (this may not be the best solution but it works when testing using Postman, alternatively it could be handled in the frontend I suppose) -

if(!$row) {
echo 'ID not found';
exit();
} else {
$this->title = $row['title'];
$this->body = $row['body'];
$this->author = $row['author'];
$this->category_id = $row['category_id'];
$this->category_name = $row['category_name'];
}

robhills
Автор

Great tutorial. I am very happy, that you finally use "ctrl + d" more often.
I always find php a little bit messy, but I am a big fan of the object oriented way. I would like to see this a little bit more in JavaScript.

PsychoGod
Автор

Hi Brad, thanks for sharing with us a great video!!
It would have been nice if there was a front-end part to this tutorial.
Maybe with a simple demo of authentication for POST.
Any plan to expand upon the series?
Would love to watch more.

designbox
Автор

If URL is SEO Friendly then this tutorial is marvelous

abhishekva
Автор

Before creating a post you should check if the data is set with isset($data->attr);

mtt
Автор

Hi Brad.. first let me mention that your tutorial illustrations are nothing short of AWESOME!!!!.. You rock!!..
Now I followed step by step but got stuck at $stmt->execute(). I have tried different techniques to catch the error num but to no avail. I am really lost.
If any of you have similar experiences please share what you did to overcome it please. Thank you.

devagunalan
Автор

thank Brad! just yesterday i was looking for php api for one of project,

God bless you and your family.

mohammadakbar
Автор

A bit of an older video but can you explain why youre encoding the input before its indexed in the db? I noticed in the first video you also decoded this information to read it. I think it is best to leave inputs structured how they were entered for storing in the db since they will have no affect on the db. That also allows the input to be used elsewhere (if needed) more easily.

sellmeawaffle
Автор

thank you kaka, I have looking for these with three month today i have the simple but good solution thank you again

isihakakiwory
Автор

This is a great tutorial. Please do with Allow Origin with authentication

jasper
Автор

I loved your guide, would you tell us how we can set routes for example: api/posts/1 (The no of the id on the url)

omeganetx
Автор

You are a life saver...Great tutorials..Keep going..

NEETHUMOHAN
Автор

Thank You! Very simple, clear and useful!

shevitza
Автор

.
.
.
WHERE p.id = :Id';

$stmt->execute(array(
'Id' => $this->id
));

// there is no bindParam needed!

Marco_Ris
Автор

so, from the full angel view, the api files are just like the normal files but the diff only overwrite the methods written in the original files in modules ?

abdqz
Автор

Do we need to install and use Apache or Nginx Http server to run REST API? or we can directly write HTTP code and call the data from SQL and display HTML

sunilpandya
Автор

Great video. Where we can learn more about those Headers info?

JulioWandekoken
Автор

Hi just tried ur instruction and code today 6-24-2022 it worked great. would it be a lot of trouble to use/add the ability for a api key that i assign to him and her etc.

aappbuilder
Автор

Is there a reason why you used print_r() instead of echo?

gownerjones
welcome to shbcf.ru