Welcome back.
Now it's time to work on the logic of getting the data from an API service, and to do that, we need
to create a file that's going to accomplish that.
So I'm going to give you here and you'll file, I'm going to click on this icon new file and then I'm
going to create a file called functions functions that B2B.
So this functions functions that BGP.
This functions file functions.
This functions file is going to contain functions that will interact with the API service and get data
from that API service.
And we need to be very organized because they're more organized.
You are the simpler it's going to be because if you are organized, you will work on your project easily
and you can maintain your project easily and later whenever you want to fix something in the project.
It's going to be, trust me, it's going to be super simple.
But on the other hand, if you don't follow best practices, things will be very complicated.
So let me here start by opening up a B2B script, so I'm going to open a script.
And in that script, the first function that I'm going to create, I'm going to create a function function.
The first, the first function that I'm going to create is a function called Initiate Initiate API any
yeat API API.
So this function is going to contain only a few things.
It's going to contain the URL of the service that I'm going to interact with, as well as the the basic
function, the basic code that we want in order to initiate a call to the service.
So first, we need to create a valuable code.
You are ill.
So you are out.
This variable will contain that you want ill will store, then you are ill off the API service.
So this is very, very important this you are able to be watching and we will get the order later.
So this is the first step.
The second step is to do to initiate the request.
So in order to initiate the request, I'm going to I'm going to use this in your library.
So I'm going to say here, see, you are able underscore initiates in it and is in it for initiate.
So here see C or IL in it and this C or L in it takes the you what else.
So we need to pass it, that you are able to do it.
And you might be wondering how can I possibly or l while it's empty?
Don't worry about this because later when I feel this your ill with the when a bus or a store, the
API, you are ill to it.
It's going to work and we need to store the result.
In a C, you are l valuable if cold cereal.
This valuable is the most important valuable because this variable will initiate the call to a server
and we can use it in order to do pretty much everything, any and everything.
So this is going to start.
A C or an ill object?
Or initiate the call.
Initiated call, the first thing that we need to do is that we need to specify all the options.
So in order to do that, we need to use a function called C or an ill set up C or L underscore set up.
And this function takes two or three parameters.
The first parameter is always going to be the C.
You are L the second parameter.
The second parameter is the option that you want to have.
So the option always starts with the C, you are L C, you are L and then opt c you are l up and then
the the the option that you you want.
So as you can see, there are so many options you can select from and you can change the value of these
options.
So in our case, we want to say C or L opt underscore you are L.
So we want our case and we want to pass the audio.
So we need to set the C or L underscoring your L to the you are L to this your name.
This is the first thing or the first option that we want to set.
The second option is going to be first.
We need to say C or L up.
And then as as as the first one, we need to pass the or L.
Then the second parameter we need to we need to use the C u r l underscore up to underscore underscore
three um return transferee turn transfer.
This is going to return the value here.
As you can see here, it gives you a good explanation.
It says that your value should be a Boolean for the following values.
And if you if you um, if you hover over this, it's going to give you the explanation of this.
So if you say this to true, which um, which I am going to say that the true because if you say that's
true, it's going to retain the transfer as a string.
And I want that.
I want to return the the of the the response as a string and then I'm going to convert that string into
another.
So I'm going to set this return transfer to through that way.
I'm going to get it as a string and then we can set two optional to other options.
And these, by the way, these are optional.
You don't have to do this, but I recommend doing this because you can use them later for up or other
scripts.
If you if you want to display advanced errors.
So I'm going to say C or L Opt, and their score is if IL is L verified.
SSL verify host.
And then I'm going to say this true false.
This is optional.
And similarly, I'm going to say set up.
See, you are ill and see you are ill.
Verify verify is essential first and then verify peer for the verify because I'm going to say false.
I don't want you verify the whole story here.
And this is for for the for this function.
So the first function is going to be the Initiate API.
This is going to contain this code and it's going to contain the options that we want to hear.
These are the options.
I'm going to have your comment.
I'm going to say options.
So this is going to make it get you.
What you need to know is that this is going to meet and get requests.
So this is going to make a great request.
And here, what we need to do after all of this is that we need to make the request now it's time to
make the request.
So to make the request, we need to say, see you are ill execute and then we need to pass the sea.
You are ill viable with your ill viable.
And then I'm going to store the valuable in a new viable cold result because this C or L excuse is going
to return.
True or false?
True in case of success and false in case of error.
So this is going to make the request or send the request, same request or make request after we make
the request.
What we want to do if you want to follow best practices is that you need to close connection.
This is a very important E to cause the connection and a classic conviction.
You can call the C or L underscore close and pass the C U R L object.
This is going to close the connection.
Many developers, unfortunately, don't use this or forget to use this, and it's considered one of
the best practices.
And if you don't, if you don't use this, you are.
You are not following best practices and it's unfortunate to do that.
So you need to follow best practices if you want to become a professional developer.
Lastly, we want to return the result, so we can't we have two options.
The first option is to convert the the response further into any form of data we want or we can return
the result, as is, we can return it like this return result.
And then outside the function outside this function, we can create another function that converts this
to all sorts of any sort of data.
We can convert it into an array or extract something from it, depending upon the structure of this
data.
And I recommend using this because this is going to return the data, the the raw form of the data,
and then you can do whatever you want with the data outside the function.
I recommend doing this.
So this is the the the most important function which is initiating or making the request to the API
service and then it's going to return the result here.
Finally, don't forget to click on file and then save or save all.