 Welcome back to the channel. The last time we were getting information from our server that we've been querying, you can look at the previous video that's up in the link, and someone made a comment and asked, is there a way to store this information in a JSON file, and actually yes there is by default using PHP, you can be able to store this information, but what I'm going to do is I'm actually going to extend this a little further and actually make an error log file, or save the information depending on what the response code is from my querying of the API. So we have a WP remote retrieve response, which actually accepts our response coming back from our query, which is a WP remote get function. We're going to check if we're getting, the code is usually 200 that shows that you're actually getting back the proper response from any server, even if it was our local server, the 200 code is actually what you get. So let me just actually show you that when I just do a small query here, if you go to inspect and go to the network tab, you'll see that you have a status of 200 because everything is loading from your CSS, from your JavaScript, everything is loading on the back end. That's why you're able to get all this 200. Now for the 304, those codes are for modified information that's coming back. So what we're going to do in our code is we're going to look for the code which is 200 and that is for our WP remote retrieve response code. If that is working well, then what we're going to do is we're going to actually vadam this information and throw it on the front end. Let me just try this here. Let's reload and we actually see that we get the response back in our browser on reload. So that means our code here is actually working. So what we want to do is that if there is any error, we also want to get what is wrong. We want to actually get something wrong and then we are going to append our error message. So what I'm going to do is I'm going to mess this up in jasemess.com meaning that we're not going to get a proper feedback from the server because that's wrong and we actually see that we have an internal error. Now I want to log all this information inside a file that is called just a file link is going to echo and we're going to use WP plugin underscore URL. We're going to use this global of WordPress and we're going to pass in and say we want it to come to our plugin called query APIs and then we're going to look for error.errorLog.txt that's what we want to look for and if the code is actually right instead of that dumping it what we're going to do is actually we're also going to find that and then we're going to find our file called data.json and we're going to dump it inside what's going to happen. So what I'm going to do now is actually write a new function just clean this up here and I'm going to call it function write to underscore file that's our function and it's going to expect some data or what we'll call a message that we are passing and we can also pass a file link inside it just to allow us to get everything working well now we're going to use functions from PHP straight up from PHP so we have what is called a fopen and we have fclose which closes and then we have fwrite which actually is basically firewrite or fileopen or fileclose and you can actually find from the PHP manual that there are very many other modes of writing that you do have for example we have the reading only we have reading and writing we have the write we have those that are just allowing us to append lines of code at the end of our file so we're going to use the A version and we're going to use the W version so why do we do that because we're going to look for if the file exists then we just append the content if our file does not exist then we're going to attempt to create it so how do we do this we're just going to first of all come and say we're going to have an if statement and then else we'll do the other so the first thing we're going to do is we're going to go for the file if the file exists what file exists if the file link exists then we don't have to do anything what we're going to do is we're going to use F open so we're going to pass in the file link and then we're going to say just append our message at the end of the file so what we're doing here is we're going to save this and say this is filing the action is filing so we're going to open that file and we're just going to write content at the end now how do we do that we're going to use the F write function now the F write function takes into a number of parameters so it's going to take in our filing variable it's then going to take in our message that we're going to pass straight into this file and then the next thing that we need to do on our message we're going to actually pass in some space so we do the slash and end and we are done so if the file exists let's get filing passing the message to write to our file but if that is not true then we're going to do the same process however we are going to change from just a penning at the end but we're going to attempt to write this file if it doesn't exist then the next thing we're going to do is we're going to use the F close function to say that after writing and doing everything that we want to do all we need to do is just close that file link which is our file that we pass on in through here so what I'm going to do now is actually make some changes to my error log I'm going to say my error message is actually equal to the date because I want to pass the date information to it so we'll add some strings some quotations here and so we want to get the day we want to get the month we want to get the year and then with the space we want to get the hour, the minute and the second then we'll append some space and after appending space we'll get our previous error message and just pass it in so that is our error message and what we're going to do now is we're going to call that write to file function and we're going to pass in our message which should actually be the error of our response so we're going to pass in the error message and we need to pass in the file link which is file link so this this could be anything it's just a variable we've stored now we also need to get this and say if everything comes back well then we're just going to file we're going to write to the file and our message at this particular point so we can just say message is going to be instead of vadamping this as a variable we're going to store all our response or our JSON and then I'll comment these two lines out so we get back our response write it to the file it checks for whether this file is available write it to that if that is not the case so if the response is not that we're just passing the response in here filter it out to the get error message as that and save so the first thing that I'm going to do is you realize that our URL is wrong so I'm going to reload and when I reload we have an error here because we're using HTTP to write to our file so that is something that WordPress does not allow us to do so what we're going to do is come back here oh so I made an error here we might be using the plugin URL so I'm just going to change this and call it wpir which is the plugin directory come back here and reload c line 71 fclose is asking for so the error is not closing the file link but rather we need to use file link because we're opening and then we're closing that so if I come back here and reload we're going to have this going through it's clean we'll see that our data.json is actually logged because our URL is correct and we have our information however if we mess this up and then save and come back to reload this and come back to we have a new error-log.txt and what it's doing is that it's getting the time, the day and then giving us the error of what's happening so we have an internal error server alert we actually see that that is an issue anytime we have the code response coming out well everything works out if we have a response error here then it's logged it is not any other response once if it's not 200 or it is a WP error then that would be the best way of handling this and yet relating it to what's happening here so thank you for watching this video if you liked it give it a thumbs up subscribe to the channel if you've not so that and turn on that notification so that you don't miss any new videos that do come out this challenge came from someone who wrote in the comments and it has helped me improve my code a little bit better and I think if you leave a comment we should be able to look at it so thank you for watching and happy coding