Before everything, if you want to understand the term API, it’s need and types of API, we will suggest you to check our existing post on same What is an API, Which API Method is Best, all about Rest API.
cURL is a command-line tool or a library in PHP used to transfer data from a server to another server, using supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP) .
how to Implement a REST API using CURL in PHP
How to Impliemnet cURL for REST API
To Initializing cURL we have a single like code:
$ch = curl_init();
Passing the URL in CURL, here URL stands for ( Uniform Resource Locator ) or a server link from where you want to grab data:
curl_setopt($ch, CURLOPT_URL, "https://www.YourLinkToCallRESTAPI.com/");
The next step is to get call back headers returned by the URL mentioned above.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Before moving ahead let’s talking about variable we’re using here $ch.
why are we repeating this again and again in all the lines?
as above in the first line, we initialized $ch wile starting CURL call, now in every curl_setopt method first argument will always remain as $ch that will only grab whole data after execution. so, let’s move ahead now
Checking in called cURL URL is correct?
here we’ll use curl_exec($ch) to grab returned data from $ch and put it under a condition and validate if returned data is TRUE or FALSE?
if(curl_exec($ch) === flase){
as above after putting curl_exec($ch) under a condition if it returns FALSE, that means your URL is incorrect or your URL is correct but it returned an error message.
or
if it returned TRUE move to else statement where we can move ahead in the call.
Authenticating cURL Call
some APIs are open for all without authentication and some require credentials. For calls related to authentications, you will need a username and a password that you will only get from the API Provider.
IF and After getting your required username/password you can apply those as below in the code:
curl_setopt($ch, CURLOPT_USERPWD, “username:password”);
next is to specify by which protocol your cURL call should be authenticated and executed:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
At the END but not LEAST:
Grabbing data from cURL Call
$data = curl_exec($ch);
after everything, your data will be moved into your variable in the form of XML or JSON.
By collaborating whole data in the above post, your code will be looking like below:
function callingRestAPI($URL, $username = NULL, $password = NULL){
$ch = curl_init();
//calling rest API from url using Var Name $URL
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($ch) === flase){
echo 'our CURL request returned an error:' . curl_error($ch);
}else{
if($username != NULL){
//Passing our Username/Password for Authentication
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
//Passing Options to Specify which method to use and check our Username and Password
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
$data = curl_exec($ch);
curl_close($ch);
}
}
and after all above please don’t forget to Close the Call by using the method:
curl_close($ch);
after above code, the $ch variable will be released free. 🙂
1 Comment