 Welcome back to the channel. In today's video, I'm going to show you how you can debug or see the data that comes back from your API or a response when you try to call some data from an outside source or even from your database into a display page. Now there's a video that I've already made which is called a display queried API data which you can be able to see how we were able to make this short code which eventually will give us something that displays this kind of data. Now sometimes it's very hard for you to determine what kind of data you're going to be getting from the API because different APIs are built differently and they have different responses. So at one time you are going to be forced to debug that data or to debug the errors that are available there. Now there are two methods that I use while I'm doing all of this especially when it's PHP. So that is the VADAMP method and the other is where I go and actually error log the data. So first of all let me start off with the VADAMP. Now I'm going to open my code inside my editor right here and I'm going to go into this code that I've already built right here. It's just going to the jsonplaceholder.tipiko.com and you can actually find a link to this very code inside this video description. So you can be able to pick it. So essentially what we are doing is we are calling this URL using this remote get. We are passing the URL, we are passing the arguments which is just to get and then we are checking whether this has a WordPress error and we echo or return something like something went wrong with the error message and if nothing went wrong, if all is okay, we are retrieving the body and then passing in the wrist phones and we are calling that the result. So at this particular time what I'm going to do is I'm going to just return the results. Once I do that, if we are able to go back here and reload this page, you're going to see that we have this huge thing that's saying we have an error, we're trying to convert an array into a string. Now that in itself will tell us that we are dealing with a different data type in comparison to the string. So how can we see what we have here? How can we get this error showing up or how can we see the actual information? So what I'm going to do here is just use a var, dump and I'm going to copy these results variable and just paste it in here. I'll add some space to make this more readable and once I come back here and reload this page, you're going to see that we have this whole chunk of information coming in telling us that this is an array and so on. So I can make this a little bit cleaner by saying let me echo and I'm going to echo some pre tags in here. So pre, I'll duplicate this, move it down and then add a slash to close off the pre tag. Of course add semicolons to make sure that my code is executing well. I'll come back here, reload and you're able to see that we have an array and inside the array we are going to loop through and we're going to get objects all through. So that's how we know that when we are going for the data we are going to use this arrow to access this ID. So that's how we access information inside an object. So we get the result. So we are looping through each one of those results coming in from here. We get it as a result and on the result object we use this arrow which is just a dash and a greater than symbol and then we look for the ID. So basically this is what we are getting right here and so if we want to get the name we'll do the same thing and use this result and use the arrow then get the name. Now there can be something different. The way this API returns data is that inside there we have an array and in that array of 10 items we have different objects. So what I'm going to do is change I'm going to change for different API. Now this is one API and this is another API and I'm going to call them from WordPress. So these are APIs that are used inside the core of WordPress and this particular one checks for weather. The PHP version that you're running on your server is recommended and is acceptable or supported. So I'm going to copy that URL and I'm going to duplicate this here. I'll paste this and I'm going to comment out the first one that we had. Now this could play out in your own way that you want to use this very code and then make your own application but you're going to find that sometimes it's distorted. What do I mean by that? So I'm going to come back and reload this page and you're going to see that we actually have some errors here. This is coming back as an object. The data is an object. It's not an array. So that means we cannot loop through it because it's not an array. So all of this would be a mess. It would not be right. So the best way to tap the different data on each one of these different nodes is let's say if we want to get the recommended version all we need to do is come back here and say okay the body has been decoded we just want to get the result and you're going to chain on and say let's get the recommended version. So when I save this come back here and reload you're going to see we still have a bad dump here but when we look here we actually have 7.4 which is our recommended version. So it is important for you to know the structure of how the API returns the data. So I'm going to change to another URL. I'm going to copy this and I'm going to duplicate this again. I'll comment out this first one so that we can replace with this other. When I save this come back here and reload this page. You're going to see that this comes back now with an object that has two items. We have the object, we have the arrays. Now if I was to check this in the browser using a JSON formata you'll see that we have offers where we have different items and it's showing us the different information. So we have 26 items and we have translations here. So we have offers and then we have translations. So I would have to check inside the offers if I wanted to get back any information. So let me go and check the offers and then I can loop through each one of those offers. So here we're going to say the results. We get results, we decode them. I'm going to check in the results offers. So what we're going to do at this moment is we're going to loop through these offers. So we need to copy this, paste it here. I'll comment this out and we're going to try to get the different information on the different offers. So I'll come back here and check. So on each one of those array items we have what we call as a response. So I'll copy response and paste it here. I'm going to copy the download and I'll come and put it here in this place. I'll get the local, current and version. So I'll get the local, current and then we're also going to get the version. I'm going to remove all this other information. Just keep it simple and when I come back here in my front-end and reload this you're going to see that we have a nice table that's showing us whether this is an upgrade. It gives us the link. It tells us the local. It gives us the current version and then it gives us also the version as well. So you see we are playing with different APIs. We just need to know the type of data that it is so that we can be able to work with it. So that is how I use the VADAM. However, sometimes it's not possible to view the data in a visual form like this. Sometimes you're not working with a short code or you are not going to display this data on the front-end. Probably you're going to use it in some other complex function or whatever it is. So the trick that I use there is I actually come back into my code and for now I am going to undo the comment of this code. We're going to remove this here and we're going to have our return so that we stop executing this code. We want to see the data that comes back from the API and after we've got the body we want to see the results. So in here I use a PHP function that's called Eralog. Now inside Eralog you can be able to print this variable. So you come inside this function and say let's print. So we print underscore R and pass in our variable and we say let this be true. This print underscore R is a method that is used in PHP in most cases to just print out a string. Now if we don't want to have an error where it's saying we are playing with the different data types and we are trying to print them as strings all we need to do is pass the true as the other extra argument into this method and that will allow us to output the information and then we can Eralog it. By default this is always going to be false when we add true that's when we output the data. Now once we trigger our shortcode of course we're not going to get anything back here because we escaped earlier in the function. Now if we go back into our install of our WordPress right now I'm looking at the WP content folder right here inside here we have plugins we have themes, we have uploads and we have the index. Now right in between the uploads and the index.php we should be able to have a file called debug.log and that's basically a text file that captures all our Eralogs that we have from the site and then just saves them so that we can always go back and review them. Now in WordPress to turn that on to enable that we need to go into our WP-config.php which is at the base of our website and then the first thing we'll do is say let's turn on the WP debug to be true. The other constant that we have is the WP underscore debug underscore log and then we turn that to also be true. So once I hit save here come back run this again and go back to our code you're going to see that we now have a new file called debug.log and when I click on it you're going to see that it's saving all the information that's coming back from our API query. Let me go back here and change the API URL so that we can get something that is relatively shorter I'll come back, reload this and when we come back to our debug.log which is here you'll see that it's now giving us this standard object and it has this different nodes inside there so we can be able to access this information when we know the data type that we're working with and knowing that it's an object so we'll be using the arrow so knowing what is inside here we can be able to determine how we shall be using this information right here. So the main differences that you're going to get from APIs is that most data types will either come back as a string an array or as an object so you need to know what you're working with so that you can be able to pass it very well and then have it displayed on the front end. If you enjoyed this video or if you found it confusing please leave me a comment below asking and maybe I'll be able to clarify that a little bit better. If you have found it useful feel free to go and check out another API URL see the data that comes back try to play around with it try to error log it or to va-damp it so that you can get the right detail on your front end. Otherwise don't forget to give this a thumbs up because you enjoyed it and subscribe to the channel.