 Alright, welcome to the channel again. This time around I had promised you that I'd be sharing with you how to work with external APIs and in particular we're going to be building a plugin that sends SMSes to the mobile phone. But basically I wanted to start you off with understanding what APIs are and how best we can do. And by going through this series, I'm going to show you that the best way that we can do is by starting up with a plugin. Basically what a plugin is is an extension of WordPress. WordPress allows us to tap into its powers by allowing us to use what we call plugins. Now plugins have different hooks that we can get to tap into the events that WordPress does have. For example, when you click a button and you save a page, when you create a new page, those are different events that can be tapped into using WordPress. Now WordPress in itself has a number of functions that help us query APIs. For example, we have a WP remote get and remote post that allow us to pass in a URL and pass in some arguments. For example, how much time should be taken before our query stops? Let's say if the server is very slow, how much time should we stop at that? And we can pass a number of arguments into our post method and right here in the documentation we actually see that we get an example of what it looks like to use this particular function. The beauty of WordPress is that all its inbuilt functionality is safe and has been looked at by a number of experts. So making it the one choice that we do have to do wonderful applications by using the inbuilt functions of WordPress. So we have something like a WP remote post that we use to send information to a server instead of trying to get this time we are sending information to a server. And then we can also use that to retrieve the information that we get from our raw query. So we're going to be using this particular function that we want to get and we're also going to be using the WP remote retrieve body so that we can be able to get that information and tap. And if you browse on the internet you'll find that there is a wonderful JSON API that is free of charge and free to use from one of the open resources available like users here. When we click on it we're able to find that we get a JSON response that has all these users. So let's dig into let's talk the theory and get practical with our coding. Right now we have logged into my demo development site and I have two plugins that I installed just I just wanted to show you the difference and what we do have. So the one on top is called query APIs is the one that we're going to use and you can see it has custom settings and links that have been input there. And this is basically what WordPress tells us I'll just open this up in my editor using VS code and I'm just going to use this file. That is in our plugins folder and I'm going to use it to open up and so that we can edit our file. So you'll see that in that particular folder that I did have I just have only one file in there which is called query dash API PHP and what it has it is that it has a number of things that are put inside it. But not to worry I'm going to run you through this step by step so that you you know what's going on. So by default WordPress requires us to have these particular settings like the name of the plugin which we're calling query APIs. We have the plugin URI. Well, this is just a link of where we're using the plugin. It's known it's you're not going to be stopped from using this plugin because you have this plugin URI but it's just an identifier like URI says. So we have a description of what it does and this one is going to be working with external APIs in WordPress. We give it a version of a 0.01 since it's our initial I'm the author and this is my URL as the author and it's of course going to have a license that is similar to that of WordPress which is a general public license. And this license can be actually found on this particular link. So the first thing that we deal with in any plugin that we do have is that we know that on the Internet there's so many people who are looking out at our code or heart or hack our sites. So we put some basic security. Again, this is also defined in WordPress. And it simply says if this is defined as an absolute path, let's say if someone tries to put in their URL, they put your website name and then they try to go to this particular plugin straight away by using the browser. And what's going to happen is that it's going to echo unauthorized access to this particular URL. So you are protected. Nobody can be able to actually hack your plugin directly from a browser by you just putting in this particular functionality. Next is we're going to be now using what we call hooks. And I just want to show you one of the hooks that I like in WordPress and which we're going to use and it's called admin init. It's basically what this hook does for us. If I go to the codex, what this hook does for us is that every time someone logs into the admin area of our website, if you look at this, you'll be able to see what it does. Every time someone logs in the administration area, or this particular hook can be tapped if you're using the Ajax URL, that's admin dash Ajax. Or if when you use an admin dash post, the PHP, then you can tap into this particular hook. Basically, it's a functionality that is default in WordPress that allows us to tap into an event that is happening the moment someone let's say logging into the administration area like this. So practically what I'm going to do is I'm just going to uncomment this. And what I'm going to do is I'm going to tie this to a function that's called a callback function just basically. Let's just do that. So what I want to do when I log into all if I refresh a page in the administration area, I would like for this to just echo a simple statement. I would use single quotes, but says you are now logged into the admin area. So that's what I would like to do. So going to hit save for that and watch what happens when I reload this administration area. So when I do that, sorry, my computer is relatively slow this day. But you get to see nothing happens and that's because we have not activated our plugin. Now remember the first things we put in allowed us to have this information here. So I'm just going to activate this plugin so that it can run the code. And once it's activated, we're going to be able to see the information that we did have here. So we typed, I'll just start writing this. I'll use the inspector tools just to help me see this information clearly. So if we do this, you'll be able to see that message that we actually echoed out previously. You're now logged into the administration area. And I'll just add something silly to show that this is true. So I'll just give that a save and I'll just try to refresh this. And you're going to see that the admin init hook, when it is triggered, it is going to also trigger our function that we called a function callback. That we called function callback function name. It's being triggered. So we know that this is going to help us to test our APIs. And I would like to jump right into that. So what I'm going to do is I'm just going to take this away and prepare ourselves for what's going to come. So I'll just uncomment this last bit of our function that we have here. So our function is open. And the one thing that I'm going to do is I'm going to store a variable. And this variable is over URL that we do have here. We're going to be calling this particular URL. So if I just copy it and then save it in this variable, we see that nothing changes. So the next thing that we're going to do is we're going to apply our WP remote get functionality. Because we want to get information from the server, we know that this particular function is actually here. So what we're going to do is we're going to basically steal what we do have here. So I'm going to store the response in a variable called response. So we go WP remote get. We'll add our URL, which is this. Basically you can just put that in, but I just like to keep this clean and smart. And then we have a second option of passing arguments here. So you might as well as say, let me call these args or arguments or whatever name you would like to call them. Does it matter? And say, I'm going to have an array of arguments. Let's say like what we see right here, we see that we're given an option of adding so many arguments that are in an array, maybe the headers or compress or whatever it is. But I'm just going to pass a method called get because I want to get a response from that server. So the next thing that we're going to do is just passing our arguments and save that. The next thing that we do is once we've sent our URL to pick that information, we need to find was there an error on us in getting that URL. And WordPress has some default methods that one is there an error in WP and where is the error going to? It's going to be coming from the response that we do get that we've stored. So is there an error in our response? And if there's an error, let's store the message coming in from the response. And let's get that error message. And I would like us at the end of the day to echo something went from and then give what error message that we do have in our response. And if everything goes well, I would like us to just throw out our information. First of all, this will use the pre tag which allows us to format our code in HTML, but we're going to dump the response that we do get. And I'm going to use the WordPress inbuilt functionality WP remote retrieve body and then passing our response that we get from querying this so that we see the information that we do get. So when I just go back into our area, remember using the admin init hook. If I refresh this as an administrator, I expect to see a dump of information coming from the server and just exhibiting similar information. I think you're seeing the information coming in here just like we have when we run to this URL. So I believe this was a success. It was a good test for us to see what functions that we can use in WordPress to query different APIs. And we see that the information that actually came back from the server is similar to the one that we get according to the documents of what our server says. So we get this information and we are ready.