How to Create RESTful API in PHP | RESTful API With PHP & MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys so this is another web technology informational video tutorial series on how to build a REST API on PHP now let's go to the details of what is an API so basically an API stands for application program interface so application program interface and that's a very broad term so there's all kinds of API but we're specifically talking about web api here so there is an api in your smartphone or an even in your refrigerators and so this is a very generalized but essentially an api is a contract provided of or provided by one piece of software to another piece of software and it usually consists of structure structured request and then a structured response so one piece of software it says give me this kind of information or this information formatted in this way and I'll give you this data or this function or whatever that response may be so in order for us to better understand what an API is so let's have a look on the analogy that I provided here of what is an API so this is the analogy that we have here so we have customers sitting on the table so consider this customers as a client or web application and then the kitchen is the server or the service that that process certain requests well the waiter is like an API so it is formatted to take a certain order in a specific way and then bring back a certain meal and this would be the data or the response that you saw that you requested so an API is basically a messenger or a waiter between running software think of this as the connection between the client or the request requesting client and the server so this is the API at the center providing a way of communication between two software so now let's talk about rest okay so basically it is a representational state transfer and it is an architectural style for designing Network applications so it relies on stateless client-server protocol which is HTTP and you're quite familiar with HTTP right so it treats server objects as resources that can be created or destroyed and can be used by virtually any or virtually any programming languages so rest can be used by JavaScript PHP Python C sharp and other languages that are available today so HTTP has different methods or methods that are being used so we have this methods number one is to get so the purpose it the purpose of this is to retrieve data from a specified resource we also have post meaning we submit data to be processed to a specified resource and get imposed basically these are the two methods that are basically used on a form get in post and then we also have this put so the put method is an update to a specified resource and also we have the delete so delete a specified resource and then we also have this three other methods which has had options and patch but we're not going to go into details on on this tree so we will be familiarizing only the get post put and delete so now we're going to look at the some end points or example end points so this is the you are URI or URL where our API or service can be accessed by a client application so this is just an example on how the endpoint works so we have get so get and then the URL order you are URI that the API or service can be accessed so anyway we can always choose the record in setup by using API we can have always the option to use different directories a for example API access it depends on your preference we also have get and then specify the ID and then we also have post so as you can see post and get have can have different or have the same URI but we don't have to worry about that because we specify the post or the cat which is the method to use so it doesn't matter if they have different or do you have the same URI that were using then we have put and then this is the example URI specifying the ID and of course the delete the same thing we'd get put and delete they have the same URI being used but the different methods get put in delete now let's try to have a look on how to setup our PHP rest project so I'm going to show you how to properly arranged or configure your rest project so for the purpose of this video tutorial series I'm going to use one server so it is consisting of Apache PHP MySQL and MariaDB so it is a Windows web development environment and if you want to use swarm server you can go ahead and download one server and install it on your local development machine so actually you can also use other server that you want or you prefer so you can use lamp Sam or other versions of this server so we will also use video or PHP data objects so it is basically an extension that will provide as consistent interface for accessing databases in PHP it will be helpful for us to use PHP data objects okay and also I'm going to use for our code editor I'm going to use visual code or visual studio code as our primary code editor and it is open source and can be run every where you can download for Windows Mac OS and Linux so you can use that but anyway you can also use your preferred a DES tour you can use sublime you can also add on but yeah for the purpose of this video tutorial I'm going to use Visual Studio code so for the layout of our project PHP project so I have created the directory PHP rest that will be the name of our project and then you can it is created on the root directory of our server so PHP rest and under here we will just create few directory for our files so current includes and then we also have no not under includes delete this you need to delete this okay core so in core we will be creating few files I'll be creating the initialize that PHP so basically the purpose of this file is to initialize all other all our settings in our project and then of course in include we will create also another file which is config that PHP so config that page we will contain our database connection for our project and before we continue with our tutorial let's go ahead and try to see a real world example of REST API so as you can see we are in the github developer page and we can see here different options and different information on how to implement a certain API so basically we can see here the different methods also that we have talked from the previous video and we can see here the endpoint so this is basically the root endpoint of the API of github.com so we can issue get request to the root endpoint to get the all in point categories that the REST API version 3 supports so we can use squirrel HTTP slash slash API that github.com and here we can also see the HTTP redirects okay so we have status code 301 302 so basically we can implement this type of redirects into our own page where a state and also we talk about HTTP verse before our methods right so we have had you get and then post we have patch we have also put in delete ok and then we also have different options here like the pagination okay so if we have like thousands of records or yeah thousands of Records and then we don't want our user to be overwhelmed with information or overwhelmed with data so we might want to paginate the data in our database by using some sort of like a parameter so page like page two and then every page consists of 100 records so we can do that with our own PHP REST API so I'm just showing you here all the possibilities that we can add into our PHP REST API okay we also have this like link header so if we want that we can also implement that into our PHP REST API and then we can also have like limiting to our request every day to our API so here as you can see they have like five thousand requests per hour limit so yeah we can also do that if we want to limit the access to our API so now let's go back to our code editor and try to complete and start the project and and actually write some code to implement our own API in PHP so this is our base project page P rest and as you can see we have two more directories the core and includes the core has the initialize that PHP file and also includes has config dot PHP so in config dot PHP we're going to add the settings and we'll try to connect to our database so let's try to do that here so there are a couple of things that we need we're going to have or we're going to define the user this is for our database user and we also have to define the password for that ok and also you need to define first our database name so maybe we'll just try to claim it as PHP rest and we can try to end then this just to make it more readable ok and then here we're just going to define another variable DB and then you this will be the actual connection for our database and in here we need to add MySQL and then the host s you can say 127.0.0.1 and then the database name will be of course equal to are defined database name so that's DB underscore name and of course another one for our character set so char set equals utf-8 and we're going to supply also the user or DB user and of course our DB password okay so that's the connection for our database and then we're going to set some attributes here leave the attributes so DB set attribute attribute and then let's say video colin colin and then add a TDR emulate prepares we'll just set it to false and we can copy this set attribute and this will be my SQL attr use buffered query okay so this will be for setting up our database connection and this will make the database connection later on to be faster than a normal database connection so attr error mode will be video and the error mode is exception okay and then I think that's it for our database connection so we have set the database attributes we have set the connection and we can use this class or I mean this particular config file to actually connect our database and then we're just going to use this in all our files later on when you try to have like get all the data coming from our database and actually in our config that page maybe we can try to define some constant here so for example define that's this define and so for example app name we'll just say PHP REST API tutorial okay and then in our initialize that PHP we can have - or we can use this config that PHP file and try to connect our database so in here and initialize that PHP we'll just going to define some constant here so we're just going to check first a vs this is for our directory separator so D s so we're just checking if TDS or directories operator is defined so if it is defined so we're just going to do nothing or if it is not defined let's go into let's just go ahead and define it by saying D s and directory separator okay and then semicolon and then that's right let's try to define also site root for our project so the same thing we're going to check if the site route is defined if it is already defined and let's just say none and if it is not then to find it by just using site route or site underscore route here and then D s so D s and then the directory of our file or our project so in my case I'm using one and after that one another directory separator and then the triple W folder that will host our files and then another directory separator and the name of our project which is PHP rest okay so we have defined our site root and then we supplied the directory that our directory of our project so now let's just define the include path or our files so let's define and then I nc then path so it will just load all the necessary files in our include folders ok so i NC path and then we need to bring in the site root ok and then the directory separator and course the folder glutes okay so basically we're just saying here that we want to go or we want to define the include path as the side road which is 164 and then triple w and then HP rest and of course the includes directory which is we have created the config dot PHP file okay and of course we also need to do the same with our core directory okay so basically we're just defining here different directories in our project so it will make more sense later on when we try to include all this files or all the files and under this directories so core path and then it should be instead of using include it should be core okay so since we have this lines already we can just load the config file first so to do that we need to require once ok by using the constant ing path that we have defined above and then the in the directory separator and then the file or the actual file for our config which has config data HP ok and of course we can also include the core classes here so let us say required ones and then the core path which is our constant and then directory separator and of course say for example we have session or any other classes from our core directory so so for example we will create another one here say post post that PHP okay so in our initialize that paid page we will just say or path and then directories operate or and of course the post at PHP okay so after that we need to go to our post add PHP file and configure the actual content of our post so basically we're just going to create a class here so PHP and class post and in here we need to add the database stuff here so that will be private in the end a connection and we also have private and table which will be post okay and then of course we need to add also the post properties so that will be public I mean public category ID public vagary name public title of our post and body of a post and the author or authors were post and also the date that the post has been created so there you go and of course we need to initialize this class or particular class by using the construct or with the DB connection okay so that will be public function and then construct and of course we need to supply the database connection here and we need to set the connection or the attribute of this particular class connection to the database connection that we have supplied in our contra method or function okay so here we go and below this construct a construct method we need to have the function to actually read the record from our database so public function and read okay so this is for getting the posts from our database and in here we need to create the actual query of our read function so that will be a query and then the Select statement okay so we're just going to have this formatted so it is easy to read so select and then see that name is category name and B that I did this is for between category name or D category and of course the post so we that category ID the great idea here and post title Bo's body and of course the post also or author of our post and the date okay and in here we need to specify the table which are what table yeah this table which has we have initialized it into post and then the name which is P or the alias of our table is P and then left join categories so this will involve two tables the post and categories so alias of categories C and on P that category ID equals C that ID and then we need to have this ordered by ordered by P decorated created at it should be created ad descending to actually get the the latest post and we need to close this here okay and in here we need to prepare the statement we prepare the statement or repair statement okay and that's stmd and then using the connection that we have and of course repair method and the app shell query of our post and execute carry by calling stmd and then using the execute function okay and then we're just going to return the STM T for an hour or the statement okay so save this and then we also need to add another file or another directory here so no not there order directory for our API so in our API we need to have the lead that PHP file okay so let's open up our PHP tag in here and since it is a PHP REST API we're going to allow HTTP request here so we're going to add headers here so that will be header and then inside we need to define access control and then allow region and then colon and then of course the asterisk which means we're just allowing anyone without the problem with authentication or oath or something like that and then also we need to specify the content type here so that's content content and then type Colin so this will be the mime type is application slash JSON format okay so in here we need to include the initialize PHP file so that's our read that PHP file is inside the API key directory so we need to back or up one directory so that's period period and then slash and of course our initialize core and then initialize that PHP so what we're doing is that we're just calling the initialize that PHP file which contains all the constant that we have defined the directory separate or the site root the include path in the core path and of course it will load the config file so which is our database connection so we already have the DB connection here so we can just use it in our read that PHP file so this is initializing initialize seeing our API so as i've said this include the config of our database the connection and also all the files that we have initialized in our initialize dot PHP so say for example the post we require once the post dot PHP file which is the class post that includes all the necessary properties for this particular class so in here we need to instantiate the post class so that will be instance shaped post so will be post and then new post and then of course because we specify that our construct function takes in a DB connection so that will be post equals new post and then of course our DB connection so if you're wondering where is this DB connection or this DB variable it is in our config that PHP file as you can see here we have defined the DB or the deep database connection so next is to query the actual blog post so blog post theory so we're going to do that by using the post object and then by using the read function so the read function we have defined it in our post that PHP file which is our class post so read and then of course we're going to get the row count so that will be numb and then result and then raw count and we're going to check if num is greater than 0 so that will be posed post array or yeah RA and then we're just going to initialize it as an empty array so post our and then we're going to have this data equals array okay and then we're just going to add the else statement here and in here we're just going to iterate the row equals the result and then we're going to fetch the PDO associative array so fetch a sock and then curly braces and then we're just going to say here extra the row okay so here we're going to say post item equals array and then inside this particular array we're going to define the attributes that we to return for this particular post so ID and then the title of our post so tie it down okay and then body of a post also so each time entity called the value per post and then of course the author of the post okay offer and also the category ID okay so category ID and last one will be the category name so category underscore name and category underscore name and of course after that one we yeah we have instead of using the comma we use the semicolon so there you go so in here after the iteration or before the iteration closest you need to push data into our array so re push the array which is post array and then beta and of course we're going to push the post item and then after that one after the iteration of the records were just going to push blue data okay so or I mean we're trying to convert convert to JSON and then output so echo and then we're going to use the PHP function JSON underscore in code and our post array there you go and if the number or the number of count or the number of result is less than zero so we just need also to define a message here so that will be echo and then JSON encode encode and message is we just set this to no post fountain okay echo Jason and we need to add semicolon at the end of the line coach's son of course we need also to define that it should be inside the array okay so there you go and in this part right here we're going to create our database and other tables and of course the records that we're going to read from our API so let's go to our PHP myadmin and in here that's just create a new database well that will be PHP rest okay as you can remember that's the name of our database that we have defined in our config dot PHP file and also we're going to add here a table or a couple of table here so categories and there will be three columns or three attributes for this particular table so that will be ID and the length is then autoincrement and their there will be named and var car which has 255 is lent and created odd and this should be a date time or is a date time and yeah I think that's it and then saved so we have now a table categories and also we need to add a post table so that will be post and ID land is 11 Auto increment and of course we have the category ID so this will be int 11 and then we have title so varchar' 255 and the body also will be text then we add two more for our author or author of our post which will be bar chart 255 also and of course the 8 that I created add and this will be a daytime daytime there you go and then save so we now have 2 tables in our database so in categories we need to insert some values here so the first one will be technology and then ate it and then created ad will be the date today so that will be 31 and the R is 10 ok so the second one will be gaming and the day the same and our is Stan okay so go and let's just an add another categories of our post so on auto and then the date if you want I can place or I can add the database in the description you can just download it and import it in your PHP myadmin so it will save you a lot of time instead of making all this unnecessary part of creating the database okay so you always have the option to download the database coming from the description of this particular video so I took the liberty of just populating the post table here so you can go ahead and try to download the my SQL file in the description box below to save you some time of adding all this unnecessary or I mean adding all this record store database so let's go back to our code editor and let's try to check our project for the last time before we test it and our postman software so we have here the read dot PHP file and we have the headers and we initialize our API by using the initialize that PHP file and of course this initialize that PHP file include all the core files or core files of our project and then and here we have associated the post okay and passing in the database connection and of course in here we have iterated the records coming from our database and displaying it or converting it to JSON format so now let's try to test this on our postman software so let's go ahead and search for postman and by the way guys this is the software that we're going to use to test or PHP API and if you have like if you have work in the future that involves API you can use postman to configure your request so this is the postman a postman software that we're going to use this is good for testing an API so let's just try to navigate to our projects of local host PHP arrest and then I think we're using HP press and of course API and then our read that page B so PHP RS API and then read that page B and then make sure that it is the the method that we're using is get and then try to send it or PHP API so we have here the I think we have some error here so let's try to read it in our yeah so we have an error in our SQL syntax check the manual and I think we don't have space here so let's try to do that navigate or post that PHP on line 39 so post that PHP and 9:39 so where is it ordered by oh so I think we have misspelled or ordered by instead of using ordered it should be ordered by okay so save this and let's go back to postman and send our request so we have a new error column not found be created in order Kloss so that's easy because great that should be created at so there you go same and we have now the result of our PHP REST API reading and under data we can see all the records coming from our database so as you can see the result of our request is on JSON format ok so now that we have read that PHP to get all the information or to get all the records from our database so what we want next is to create another read for a single record from our database so so for example we want to specify a certain ID of a post and then we should be able to get all the information associated by that particular ID so let's go back to our code editor and in our post that PHP file so right below the public function read and make sure you're still under or inside the post class so in here I'm just going to add another function so public and then function read underscore single so this will be just getting a single post from our database so as for the query so we're just going to almost we're going to use almost the same query and then we're just going to change something here instead of using order by so we should remove this because we're only getting a single post so there's no point of ordering it by the ID or created add attribute and of course we want to get a scene pause so we need to specify that it should be a particular ID and of course we need to limit the return of this query into one because yes we only want to have a single pose to be returned and in here we need to prepare also the statement for this particular function so that's prepared statement and in our statement here below you need to get the actual data coming from our database so statement and then fetch and then video fetch fetch associative array so and then below here we need to set the properties of our post so this title will be our row and then of course the return title from our query and then we're going to copy this one two three four okay it should be for our body this one is our author okay and then we also have the category ID category underscore ID so copy and paste it here and then category name gory name okay so here we go and we need to bind a parameter for this particular statement here so we're post id equals so instead of executing it directly we need to bind a firearm so that's buying parameter and then we're just going to specify the first one and ID should be the post ID okay and of course this is finding the bottom right and the next one is executing the execute the query so that's STM T and then execute okay so stmd execute and then row is the MD fetch video fetch assault or associate or associative array so I think that's it for our public function read underscore single and under our API let's just create a new file here so that's new file and then read single that PHP what happened there so read single dot PHP and then we're just going to copy here there read that big file and we're just going to delete the content of this below only the header so we're just going to use the same headers access control and of course the content type then we initialize our file and then instantiate a post object and we need to assign the post ID so that's post and then ID equals we're just tracking if the get ID is set so return the get ID otherwise exit or die and then post read single so that's the function that we have defined in our read that PHP file and of course we want to create an array of our values so that will be post post just like what we have in our read that page P so array and then of course we need to specify also the ID okay so post ID we're just going to copy this six by ten of our post the body of our prose and of course the author of the post category ID okay and then category the our name so we're just going to copy this title for this so this one and also this one you know so this one and so with this one and we need to make make a JSON okay and print our and of course Jason in cold the post array so post array so I think that's it and we can use this in our post months off there so let's go ahead and let's try that so I'm just going to copy this and create a new request so this should be API and then read underscore single dot PHP and then specify the ID say for example I did three okay so we have an error so let's try two so we have an error here because we misspelled the fetch so post fetch okay so let's go back to postman and send category name ID you find property post single single category name ID okay save and again so that's it we now have the post three with all the information coming from our database so you try to go with another one ID too so we can get all the information associated with ID number two and in this video guys were you going to discuss how to create or implement a create record in store page previous API project so as a sort of review guys in the previous video we also have implemented the reading of single record coming from our database so we have laid out the function read underscore single and of course we created a file read single dot PHP and then we're querying the record by using its ID and of course returning all the data related to that particular ID and now let's continue working on with our post that PHP file and just below the read underscore single function we're going to create another function here so the name of this particular function will be public function and then create okay so in this particular function we're going to add some lines in order for us to create another or create a record by using our API project so for us to do that let's go ahead and build first our query so here we need to create our query okay and that will be query and force the actual query of our function which will be insert because we're trying to create a record into our database and of course we'll just concatenate our table name here so this and then the table of this particular class so I think it's yeah table okay that should be table and right after that is another string set title equals placeholder title okay and then body equals the placeholder of the body of a post okay another one for our author and the placeholder for our author and then of course the category ID so category ID and then the placeholder for our category ID Hey so and then we need to prepare our statement here below so prepare statement so that will be STM tea and using the connection that we have so that will be connection and then prepare and of course the query a and then we need to clean the data next because the information that will be saved into our database is coming from the user Earl input of the user or the requests coming from our user so that's why we need to clean the data clean data so for our title we need to set the title equals to HTML special special chars ok and then strip tags okay and then this that or this and then the attribute title a so this line it only means that we just want to remove all the unwanted problems with our data so so this will be for our body okay this will be for the author okay and of course this will be for our category beggary ID hey there you go so we just want to make sure that it will be the same also for this particular part so category underscore ID and yes we just need to make sure - and then this or to align this to make our code readable Hey and then right after that one we need to bind the parameter or the params for our like a placeholder for a title body author and category ID so this part right here will be binding of parameters okay so as TMP and then bind param specifying the placeholder over it of our title and then the actual value of our item so there you go and then we'll just copy this one for the other attributes as well for our category ID for our author and of course for our body or body of the post and of course we need to change also the placeholder for this this and so with this category ID okay so after that one we need to execute the query of course after binding all the forums cleaning the data so the last part will be executing or execute the query so all we have to do is call the STM T and then execute okay and then if the execution of this particular query is successful then all we have to do is return true okay otherwise we print an error here print error if something goes wrong so that will be print F a winter and then error will be % s okay and the error our statement or the query that we have and then return false something like that hey so basically guys we only prepared our public function or public function crate and this will be used for our files especially in our API so in under or API directory we need to create another file so that will be great so let's go ahead and create a new file and then new file and then we need to say here create that PHP so in our create that page we guys we just want to add some headers also the same with ours reading I mean the read that page P okay so we might as well copy all this all this color right here and then paste it here and then we just need to remove okay starting from here and then guys as I've said we need to add some other either either we need to add some other headers here so we have access control allow region and we have content type so another one header is access control access control and then allow methods okay so this is save or create query or we need to create a record to our database so this will be a post method meaning the request the request should have data for us to save int or data and then we also have to add another header here so this will be access and then control and then allow headers so basically we're just specifying here what are the header is that I needed so that will be this one we just need to duplicate this okay and content type also okay another thing is the access control alone methods okay and also the authorization okay so basically we're not going to touch authorization in this particular series but for the sake of this tutorial we're going to add authorized ation there so maybe in the future you want to implement an authorization for your PHP API and the last one will be the X requested to it okay so these are all the headers that are required or specified as the allowed headers and in this video guys we're going to continue working with our create that PHP file and at the end of this video we're going or we should be able to insert record into our database by using our PHP API so let's go ahead and try to modify our cade that PHP so last time we we just added the header you know of course the function in our post at PHP so we have here the great function and we should be able to use that in our create that PHP file so in here we have initialized our API by using our initialize that PHP so basically in initialize that PHP we connected our project into our database so the next step is to instantiate the post so we have we have that here so instantiate the post by using the post class and of course applying the database connection so after that we need to get the posted data okay so of course the data should be coming from a request okay so we need to decode that by using file get contents and of course specify that it is a PHP slash slash input okay and then by that we can get the actual data so post we're going to set the title by getting the data and of course the attribute title okay so copy and paste this for the other attributes as well so we have the body the author and then we also have the category ID so copy and paste okay okay so after that we can just create the post create post and then we can use the function that we have defined in our post that PHP which is the grade so in order for us to do that let's add the post and then call the function great so we're just checking if it is successful or meaning the function returning true okay so if the function return true then we're going to echo a message here or meaning display a message so JSON encode and this is going to be an array so specifying the message which will be post corrected okay and then of course if the return is not true then we will return I mean we will display another message okay already you can just copy and copy and paste this so the message will be post not created okay so let's try this in our postman software so first month and this will help us to develop API so if you're serious about working with the API so make sure to download and use postman software so in here we just need to add a another tab okay so instead of using the read underscore single then we're going to add or change this into create that PHP and of course it should be post because we're sending data into our API and for the headers let's just say content type so as you can see if we try to type in we can have the options and then we can see content it should be content type content type and it should be application slash JSON and of course in our body we can choose raw and then we can specify here the data that we want to pass into our API so we need title for that so basically we're just going to around our values the key in values with double goat so sample API post and then comma and then body sample body of an API goes from a request and then we have another one for our author so Brad Stevens and third or the fourth one will be the category ID a regular ID and then we can say like there are I think we have four categories right now so basically we can choose from one two three and four so we'll just say three and we can send this try and see if our API works so we have an error here we have an error in our SQL syntax so this is posted PHP on line 81 so this is probably the query so post that PHP and then we have the actual query here at the top so I think this one made the error so save that and let's try to test it by sending another request so now we have field created that doesn't have a default value that's because we need to edit the attribute of our post table so as you can see here we have the different attributes category ID title body and author and created that so let's go to structure and now you created at date time doesn't have a default value so we're going to change that and the default will be the current timestamp okay and then save and let's go back to our postman API I'm in postman software and send another request to test our API so send so sending request and we have the response which says message that depots has been created okay so in order for us to test that there are other ways for us to test this so we can just read all the records so send and there you go we have here the data that has been added recently so the ID is five and the sample the title is sample API post which we have specified here so sample API post and then we also have the body so sample body of an API post from a request okay the same and of course the author is Brad Stevens the category ID is three okay that's what we have specified also here and the category name is Otto so I think that concludes our create that PHP or create function or create feature and we have successfully added that into our API so the next step is to create or add the feature for updating the records and in this video guys we're going to discuss the update so basically we're just going to add the update function or feature into our page be restful api so we have here degrade function as you can see from our post that PHP file and then we're going to copy the whole function because the idea of create and update is almost the same so we're just going to copy all this included in this particular create function and then right below that we can paste the update so we're just going to name our function in to update okay so this is the update post function okay and of course the query will be a bit different because we're updating a record now so we'll just say update this table and set all the necessary attributes so for example the title the body the author you know of course the category ID and since we're updating a record we need to set the ID or like filter by ID because we don't want to update all the records so for now let's just say we're IB equals we can use the question mark or we can also use the placeholder : ID and of course we can also do the cleaning of data of the ID so setup category ID and also here we're just going to say ID and then indent this on line with the other attributes okay and then of course we need to bind the param parameter so we're using the IDS placeholder for our ID and the value will be this and then ID and then it should also execute the query and then return true if the query is successful otherwise print the error message and of course return false so in our project we need to create another create another file for our API so we have already create that PHP also the read underscore single dot PHP and then read that PHP so now we need to add another one for our update so that will be update that PHP and of course also a sort of basis from for this update dot PHP we can just copy all the lines of codes that we have added in our case that PHP and then paste it in or update at PHP because I as I've said the logic behind or the theory behind the create and update is almost the same okay so but instead of using post it should be good okay and then we also need to have the ID here so post title and then the ID because we're specifying the ID of the post or the record that we want to update and of course instead of using her aid we should call the method update and of course it should be post updated and post not updated ok so so I think that's all there is to it for this update that PHP file so let's try that in our postman software so we have here the create that PHP so we're going to use update that PHP and then specify the ID of the record that we want to update so ID and then Colin and then let's write let's try to see which post or record that we want to update so we have added this sample API post by using our create that PHP file right before or from the previous video so the ID of this is five so let's just use that so ID is five five and then let's just say updated updated API post sample body of an API sample body of an updated update API pose from a request so we have the same author category ID and also the ID that we're going to specify or update and of course because in our update dot PHP we specify that it should be a put method so of course we need to use that in our postman in order for us to properly update the data okay so send and let's try that and the record or the response says that the pose has been updated so let's try to read all the data so as you can see here we have updated the title and of course we have updated the body of our post so as you can see the update function are the update feature of our PHP rest or the PHP API that we are developing as working so the next step is to create another one or another feature we will be adding another feature in store page by REST API so basically we all have the reading of all the data reading of a single data or single record and then also we have creating a record and also the updates so the last one will be the delete of a certain record okay so basically the same idea we will be adding the function here for our delete so that will be delete or public function delete it should be an easy function right here so right below the update function we will have to add the delete function so that's public function and then delete okay and we just need to create the query here so that will be query and then of course we leaped from and then this table and of course we're ID we need to specify the ID of the record that we want to delete and then we need to say we are preparing the statement prepare statement so SD empty and using the connection prepare query and of course we need to clean clean the data so we can just copy the this particular line so clean data and yeah ID and of course we need to bind the parameter so SD empty and then vine parameter specify the ID and of course the actual value and we're going to execute the query the same thing here so just like that so basically we're just returning true if the statement has been successful otherwise we need to print the error and return false so public function delete and we have set up the query to delete the record from our database we have prepared statement and then we also have the mechanism to clean the data so as you can see and then this right this part right here is binding the ID parameter to our query and and this part is to execute the actual query and if there is an error so we should be printing or the error here and guys the next step is to create another file here under our API so new file and then this should be delete that PHP okay and in here we're going to add some logic and add some lines and and I think we could just copy what we have in our update dot PHP and as you can see here we have the code or lines of code for our update so let's go ahead and copy all this and paste it in or delete that page 55 so in here we're gonna change a couple of things so the for example the allow methods instead of using put it should be delete right there and of course initializing our API by using the initialize that PHP instantiate the post class by using this particular line and supplying the database connection that we have configured with our initialize that PHP and then of course we're getting the data that that has been posted by the user or by the request so as you can see we are getting the data by using the code and file get contents and PHP input so I think other information just like the category ID the author and the body and of course the title is not needed here because we're only concerned with the ID that has been passed by the user or the request itself so we're citing the attribute ID by getting the data ID just like that and as you can see we have created the delete function so we're just going to update our delete dot PHP instead of using update function because this is a feature for delete it should be of course the delete function there and change the message so instead of updated it should be post deleted okay and of course this one will be post not deleted so I think that's all for this file delete that page B so just say the post that page B and of course the delete that page B so we'll try real quick this particular feature with our postman API so let's go ahead and open up our postman software and try to check if our feature really works as I've said guys from my previous videos if you're working with an API and we're trying to get around or to be comfortable with an API be familiar with how API works so post months off there can really help you we of the activities that you want with an API so basically we have read that PHP you have read single we also have the update dot PHP so if you try to run or read that PHP we should be able to get all the records for coming from our database as you can see here and of course we're dealing with delete maybe we could just create a certain record for it now or maybe we'll just delete a certain post just like this one ID equals to 5 so we'll try that ID is 5 so we'll just copy this and then create a new tab here in our postman software make sure that the method is delete and of course the the file that we want is the belief that PHP and of course we're going to supply the IV of the record that we want to delete and as you can see we have the ID is 5 ok so we're just going to go grab that and set the ID goes to 5 just to check if we can really delete the post with the ID 5 so send so we have an error here let's try to trying to get property of a non object so line 17 over delete that PHP 9 17 post ID and theta ID so also we're getting the idea from data ID and as you can see we're using PHP input so instead of supplying directly the ID here this should be declared in the body of the request so let's try that local host PHP yeah let's go to our body of our post and then raw okay so in here we're going to supply the ID instead of directly setting the ID equals here so this will be ID and then of course the value which is five okay so send a request so what happened as you can see we have here the message post deleted so we should not be able to or we won't be able to view or get the data that is that has the idea of five so try that so as you can see the record that has the idea of five is now gone okay so basically we're just deleting the record by supplying the parameter in our request body so I think that concludes our PHP REST API tutorial and ii c we have fully functions or we have now the full function of our PHP rest api so we have to create delete read single read that PHP and of course the update so if you want because as you can see there is also the category for our post so we have to cut the post and then the category of our post so we could also create another file for our category to be able to have the read update the read single and also the delete function for our categories but it I think it's a it is pretty easy because the category has only two or three attributes the ID and then the name and of course the created add attribute so you can if you want you can create it on your own you can start by creating the read that PHP update that PHP for this particular model category that PHP so I think for now I'll just guide you for the single or read that function for our category that PHP so we're going to create another class for this so I'm just going to copy right here and then here so where's our get their diary that PHP and instead of using the post it should be a category so this is for our category table so we have construct or the pigs and the DB connection and of course the read and we need to close the territory class right there okay and instead of using the post table we should be able to or we should use the category category table in our database and of course as I've said there are only three properties for our category so the ID the name and of course the create of that create that okay okay so I think this is Pythagoras instead of category categories there you go and create to that and then we have constructor with DB parameter or the do connection parameter and in here instead of having this join query we can just create or query all the attributes from our table name categories and we don't want or we don't need this particular part right here so select all from categories and then we're executing the query there you go and in here in our API we can just say read all categories that PHP and of course we can just copy the code that we have here in our post dot read dot PHP so that will be read all the categories and instead of using post here we should say category okay and then passing the DB connection and then reading or what is the method so I think we also use read write so read okay and then we have the ID the name and of course I think we only have great add in our table in our database and you can just move this crate out okay and Preta that it should be created that so basically we can just delete all others and we should be able to see our categories okay so category is no categories found if there is no record phone for our categories so let's do that and try that with our postman or post months off there okay and try to check if we have a functional read for our categories so here new tab it up there you can copy this and make sure it's another request API and we have the file read all underscore categories and then it should be get and then send so we have an error class category not found let's try that because we haven't initialized our class category in our initialized at PHP so let's do that with this line category that PHP and say that and go back to post once off there and send undefined variable crated add in all categories 26 that's because huh 26 8 and I so it should be create all right okay and then send so there you go as you can see we can now read the categories from our database so guys it's your turn now to make the other functions like the read the update reading of a single one and of course deleting a single record from our database so guys that concludes our PHP rest api tutorial and I hope you enjoyed this series and if you are new here in our youtube channel and you want content about programming so I am inviting you to subscribe and press the notification bell so you will get updated whenever we have a new upload regarding programming
Info
Channel: ProgrammingKnowledge
Views: 46,799
Rating: undefined out of 5
Keywords: User Registration Form, PHP, MySQL, HTML, CSS, Bootstrap, GET, POST, Create a HTML and CSS registration form, PHP Registration Form, Login Signup page in PHP, PHP User Registration Form, How To Create a Register Form, Simple Registration System, CRUD, Create, Read, Update, Delete, PHP CRUD, Bootstrap 4, INSERT, SELECT, PHP REST API, REST API, RESTful
Id: dlGtSoigdB0
Channel Id: undefined
Length: 103min 36sec (6216 seconds)
Published: Sun Apr 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.