PHP REST API Tutorial | RESTful Services

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video tutorial we are going to talk about REST API we are going to talk also about four different request methods that we can use and also we are going to create one simple REST API for ourselves so stay with me [Music] hey what's up so suckers see Nate here from cold past income calm the place where I help others to become a web developer much easy and fast and then they will do their own so if that is something that interests you make sure that you subscribe so before we start with this tutorial let's explain when we are going to use REST API and but it is so basic arrest API is a way to create at our website easily communicate with other applications and other websites so that lets say other developers can easily access our database so with any API we should create the four basics methods which is create read update and delete inside our database and for that we are going to use HTTP methods which we say request methods that are get post put and the leaf there are of course many other record requests HTTP methods but we are going to focus only on those four that are the most import now the next thing that I want also you to know that is most people when they create the Torah's or anything they always created in a way that they are using object-oriented approach in order to create REST API and yes that might be better if you are going to create some big API but for those of you who are just starting out I'm going in this tutorial just to use procedural programming and to really show you that in order to create API you don't need to use object-oriented approach so let's start with this so first off here inside my browser I want to show you something and basically what in our example we want to create is that let's say that we have one simple database and inside this database we have one table customers so we want to give other developers and ability to easily add new customers change their formations delete those etc so how we are going to do well first we want to make some URL like convention here so what to say that anytime when we get the request on ignore this part let's say that this is our domain and I'm going to say when we get the API slash customers slash anything that will depend what we are going to do so when we do only this and make get request that means that we are going to read all the customers that we have inside this database now if we provide here some ID let's say well in that case we want to get only the data about the customer twist with ID 12 now if we are going to send delete method that means that we are going to delete the customer ID 12 etc etcetera so pretty much we are always used the same a technician but depending on the request method that we are using we will do different things so let's see how to write the code achieve that so let's say here first thing I want to create a new directory I will say API and inside that API I'll create new PHP file so maybe let's say handler it can be any name okay and also what I want to do is I want to create one file in the root folder and I will say dot gives the access which means that in this file I'm going to define all those URL right so first thing LLC rewrite and time and I will say on and then we are going to set some rules so I'm going to say anytime when in the URL it starts with API customers and then maybe we can say like this which means that this character is optional because of those quote so yeah that's how you're going to do it and then we are going decide where we are going to resurrect the data so let's say what you are going to do is you'll say it will be inside API folder and file this handler dot PHP okay and let's say we'll define another one which is the same but this time we are going also to say that they are able to send the ID here so for this we are going to use regular expression so this means here that this ID can include the numbers from zero to nine and this plus sign means it can be more than one okay in that case you'll say that we will send to the hello dot PHP also will include ID equal and that ID will be basically this here and it will be stored in the variable one okay so this part here will get to the variable one so let's see for now I will just say here let's say echo sorry three and then I want to say Wadham and let's see what we have so now when we refresh and let's say I will go here and say API customers it shows that in the gate we don't have anything it's empty and then when we say for example number twelve you can see now that on our hello dot PHP we have in the get method we have variable ID and value twelve and that's all that we need to know so now let's create here inside the hello dot PHP let's detect different HTTP request methods so I will say if requires now say yes server requests metal people if you get okay you can say like this we are going to do something and then we can help like we said for different okay so I will say here if it's get boss put and delete and now as the name says here get we use only when we want to get data post when we want to add a new data put is when we want to update the data and database and the lead is of course when we want to delete the date now for the first two for the post and put we are able to send additional data and that means that when we sell additional data we will be able of course to use the data to do something whether it's inserting to date waste or it's updating in the database okay so now how the thing is now how we are able to send different methods so that we are able to properly test well we are going to use the tool that is called postman and it's here and for those of you that don't know you can go to the get postman comm /downloads and then download the one at the top of course here you will have the different option for different operating system its linux for me so that's why it's here okay so now what I'm able to do here is I am able to grab this URL and here I can set the type of the the request method that I want to use and then I am able to execute so now you can see here in the body there's isn't anything but what I'm able to do is I can say he echo get so this is just so we can see that actually works us we want put and then post so let's see now send says get we sent post-its post tell me delete okay cool and that's it now let's first build the get method so here at the top I want to create one variable so I want to access our database so my heart is localhost my usernames wrote password is MQ and database is press API so now please out of the people make mistakes because they get confused but this here so basically if I would copy this and let's say I will just open text editor you will be able to see how it looks like so basically those tags that you hear user name password database name doesn't actually exist in my code so this is just my ID that is helping me to know which parameters I should send so let's say here now when someone said get request right let's say that we are able to have two different get requests first one is get here which means he wants all the customers and if so so on sounds like API customer slash some ID that means he wants the details only about the eggs the customer with that ID so what I'm able to do here I can say here F is get and let's say i D he'll do this elves are going to do this maybe that's your size of it and we can say here okay our idea is to say connection and then of course real escape string and then you'll see get ID okay and all as well will be connection query and I'm able to say now select let's see name and age from customers where ID equal our ID [Music] okay and our data will be asked I'll fetch and let's say associative array okay and maybe for now I'll just say here exit and then with API we almost always return the data in the JSON format and for those of you that didn't watch or know how to do it I have the video that you can find here and you can watch it how to use the JSON and a tax so here I will say JSON encode the data and let's see now so when I use the postman I will say get and sending for the 12 you can see it now but let's see which ID we have it's number three and four so I'll say here slash three sand boom and now you can see that our server has returned that the custom with ID three is name is made age is 29 now if there isn't the ID that means that we will need to get for all customers sorry so I will copy this here and I'm going to delete this part okay and then here I will say one let's say DEA's data equal as well fact associative we can say that maybe here we are going to have one gray and you'll see here data okay and let's test now so now when we remove this you can see now that I I have got an array of all customers in the database and for each of those we have the details so this one is name this one is Johnny for each we have got the age and that's it so pretty much that how easily we are able to implement get method and of course here we are not using any authentication and obviously this API will be used by some admins because they should be able only to access the customers data and if you want to learn about at authentication with REST API make sure that you comment down below that you want that tutorial and if there is enough demand I of course create one for you okay so now let's move to the next one so let's say now that we want to talk about the post and we want to be able to insert new person through our API inside the database so what you want to do is first in case that there is a post we want to make sure that someone is sending us to us the data that we need and let see the only data that we require is named at 8 so what I can say here is f is a post that's a name and he said post age in that case we have enough data else we can say exit and we can return something like status let's say failure and then you say maybe reason okay check your input something like that okay and then here if we have everything we can say so I'll just quickly copy from here they still dance okay so help me and we are going to have eight and then all we need to do is we can see connection whirring which is a insert into customers and then heroes pacify what let's in name age and I think we have read on no it's Adam okay and the values will be named age and now and you can see here okay and that's it and maybe in that case we will just return like this so say stop us success okay and let's see so now in our postman if we say one post you are sending to the API customers send and since failed reason tracker inputs and that's because we don't have the name and age in our data so we are going to say to the body it will send new data and I'm going to see humane let's say Chris and age let's see 25 whatever send okay now there is some error let's see what one 19 20 yes it's here and because we are using here yet and needs to be post and let's see this guy didn't work so let's try again send say status success and now let me check out database it is working so now we also have fully functional the post method so basically we are able to add new customers and also we are making sure that we have all the data that we need in order to properly insert a person now the next thing is put method and basically we use this one when we want to update some customer so let's say now that we again will expect that someone sent at least name or age or maybe both so now with the put method we can access the data by saying something like data equal and then file get contents and then you can see PHP input and then I will just say here for now echo data let's see what will happen so in the postman instead of the post I will change to the put its send and now you can see that our data is named equal Chris and age equal 25 okay now what we also want is that when we execute put method we want that someone send us the ID so the how we are going to do this is inside the HT access we are able to define different rewrite rule if we want to or we can just check here for get so you can see if not he said get and let's see what cell is it ID yep get ID then in that case we can see again like this so let's see postman send and now you can see it says fail it check your inputs but if we say something like this let's say one now it works now it's one true okay now what issue here is that as you can see here our old data is in this format it's basically the string and in order for us to connect it to the array since there isn't some something like global variable put in the PHP we need somehow to create an array from all of these so for that reason we are going to write our own function so what we can do here is how maybe first say if let's say I want to check do we have at least one equal which means we have at least one pair so I can say here if in our data we have sign so there needs to be at least one pair else I won't tell that they need to check the data again okay so in this case let's see oh good so it's just for now so when I hit Send you can see it says all good but when I remove the data and hit send you can it says please take your inputs now if I hit that I want to send only a name and hit send it whoops again and it will work with eight as well so let's now write the code that will transform sorry transform this data to something meaningful some array that you are able to use so our data looks like this it says name evil what this innate and age equal twenty nine now first thing that I want to do is I want to create an array and we are going to export it by this so I will say our data is equal expose the data by cout and it's from the data so let's see what we will get always important to know so when I hit Send again you can see now that I have basically two arrays and inside I have array with two elements and first one is named Chris and the second one is 8 225 now what I want to do is I would love to go through entire array and each of those again split by e equal and create the pail so what I am able to do so let's say real quick you are able here let's say au pairs it's an array and now I will say for each data as what's it fail I can say that fail is equal explode evil okay and then I will in our all pairs store that key is pair zero which means it's our key it sorry so when we split by evil this will be index 0 this will be 1 so this is key and this is value so our key will be at 0 and value add 1 so I'll say evil fail 1 ok and now ok and now I can see far down pull fears and let's see what we have so when I hit Send you can see now that again I have 1 array with but this time it's associative array and I can directly access the name and a pretty cool now the next thing is I want to detect what data I have got and based on that I'm going to know which data in the database we are going to update so I will say F is sad all pairs name and he said four pairs age that means that someone in this request wants to update name and eight at the same time and then we can say else if he said all payers name that means that someone wants to update only the name else if he said oh there's need someone has added only the eight and then else if some other data has been sent and it's not name and age and we support only those two in that case we will just say again please check your inputs okay now for the name and age now all we need to do is we can see SQL query let us browse our connection query and then we'll see update customers set eight equal and named okay and then here we will need to insert so I will see here all pairs age then I'll just copy this name and then we will see where ID equal people our I need that we have got here but we didn't stored anywhere so we can say customer PI DC equal connection real escape string gay ID and then now we are going to use down below so I will see here where I the equal customer ID okay and that's it now let's maybe just use this one so we'll see well access that to success when it's success cool let's see now I want let's say for the customer with ID three changed the name so I will see in the postman I will remove age I will say it will be snail be and custom I this one and yeah we need to change also them the age but let's say 20 now only the same sand it's a success let's check out database nothing happened so let's see why so the provides this is ID one needs to be three says that the success again okay and it updated but it's not correct let's see why okay this should be fine Oh peers age no issues here should we come okay same rose and now it is looking good but as you can see now it is using the wild cards so we should do URL decode for every parameter but that's not that much important right now okay so if you don't understand what I mean basically here if we will do URL the code that should work yeah and that's it okay okay now this is for updating the name and email name and age at the same time now for only name we can the slightly modify this by removing age okay and for the age we can modify by removing the name right and that's it and this is going to work properly now the next thing and the last one that we need to do is we need to write the request method for the lead and basically this one is very simple so basically we need to check for the ID if we don't have one we will say please check your inputs if we do have one all we need to do is build us a connection query delete from customers where ID equal and let's say customer ID which I don't have at the moment but I will grab it from here okay and maybe what could be even better is that we right we can learn each time so maybe it's better if we leave as is so I'll do like this okay and then we will just return to success okay so let's make a test so I'm going now to say that I don't have any data because we are going to use the lead so I'll say delete and now I will remove the ID hits and says please check your inputs now when I add the customer ID send say success and when we check our database we can see now that snare doesn't exist anymore so guys pretty much that's it I hope so that you have enjoyed this video and you have found it very useful I have tried my best to explain all the different methods and how to create very simple REST API for your website if you have any questions or anything please ask in the comments below and of course please like it and share with your friends take care
Info
Channel: Coding Passive Income
Views: 11,529
Rating: undefined out of 5
Keywords: php, php tutorial, php 2019, php tutorial for beginners, php rest api, php rest api tutorial, rest api, restful api, php7, api, web development, json, php tutorial for beginners full, restful api tutorial, php restful api, php oop, restful, rest api tutorial, server side, what is an api, postman, api integration, api gateway, api testing, restful web services, web design, web developer, php for beginners, what is rest api, what is api
Id: OxdgtFs9Z_0
Channel Id: undefined
Length: 32min 10sec (1930 seconds)
Published: Mon Aug 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.