I was recently working on a project where I needed to integrate an external API using HTTP cURL requests. It was my first time doing this and I had a lot of problems figuring this out. I wrote this post so I can remember my cURL API calls for next time, and maybe it can help you as well.
PHP gained fame as easy to learn and at the same time quite a powerful programming language. Throughout its history, it went through several periods of ups and downs, but with the release of the seventh version, PHP confidently returns to lost positions and firmly holds the title of one of the most popular languages for web development.
An important part of many web apps is the interaction with external APIs to obtain the necessary data and extend the capabilities of the application. PHP provides a rich set of functions to work with APIs, and we will discuss these functions in more detail in this article.
Table of Contents
Overview of cURL:
To work with the APIs, we will use cURL and a free app that allows making HTTP requests in PHP.
The cURL project offers two sub-projects:
cURL
— command-line tool for sending HTTP requests from the terminal. For example,curl -X GET https://www.google.com/
command sends a GET request to the Google server, and if everything is alright, the server will send the contents of the search page in response. curl is a wrapper forlibcurl
.
Request Methods with PHP and cURL:
Types of Requests or Request Methods characterize what action we are going to take by calling the API.
There are four main types of actions in total:
- GET: retrieve information (like product information). This is the most common type of request. By using it, we can get the data we are interested in from API.
- POST: adds new data to the server. By using this type of request you can, for example, add a new review of a hotel.
- PUT: changes existing information. For example, by using this type of request, it would be possible to change the text and publication date in an existing blog post.
- DELETE: deletes existing information
<?php /****************************************************************************/ // Calling The Method /****************************************************************************/ // Request Header $requestHeader = array( 'Content-Type: application/json', 'token: '.$AuthToken.'' ); // Request Body $requestBody = array( array( "AuthToken" => $AuthToken, ), array( "ConsumerID" => $ConsumerID, "Name" => $Name, "Mobile" => $Mobile, "Email" => $Email, "Address" => $Address ) ); // Call API $requestHeaderBody = CallAPI("POST", "https://demoapi.example.com.pk/v2/", $requestHeader, $requestBody); // GET HEaders and Body In Return $requestHeader = $requestHeaderBody['responceHeader']; $responceBody = $requestHeaderBody['responceBody']; print_r($requestHeader); print_r($responceBody ); /****************************************************************************/ // REST API via CURL // Method: POST, PUT, GET etc // Data: array("param" => "value") ==> index.php?param=value /****************************************************************************/ function CallAPI($requestMethod, $requestURL, $requestHeader, $requestBody) { /* switch ($method) { case "POST": curl_setopt($curl, CURLOPT_POST, 1); if($requestBody) curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestBody)); break; case "PUT": curl_setopt($curl, CURLOPT_PUT, 1); break; default: if($requestBody) $url = sprintf("%s?%s", $url, http_build_query($requestBody)); } */ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $requestURL, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 1, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $requestMethod, CURLOPT_POSTFIELDS => json_encode($requestBody), CURLOPT_HTTPHEADER => $requestHeader, )); $response = curl_exec($curl); $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responceHeader = substr($response, 0, $header_size); $responceBody = substr($response, $header_size); curl_close($curl); $requestHeaderBody = array( "responceHeader" => $responceHeader, "responceBody" => $responceBody, ); return $requestHeaderBody; } ?>
Customization:
No need to customize it. Just copy-paste. Rest edit the code as per comments and need.
Troubleshooting the Errors:
Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.
Final Words:
That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave. created our own news search service.
Be the first to write a comment.