Using cURL API calls with PHP
cURL stands for ‘Client URL Library’ and it allows you to connect and communicate with different types of servers with many different types of protocols (HTTP, https, FTP, proxy, cookies, etc.)
One of the most common problem is how to retrieve data using a curl and post? Well here is the solution.
But before we start with the that, let’s go through some of the basic. Firstly need to know our cURL setup, I’ve added a simple example of a plain cURL request. The request will return the API response as a string.
// create & initialize a curl session
$curl = curl_init(); // set our url with curl_setopt()
curl_setopt($curl, CURLOPT_URL, "api.example.com"); // return the transfer as a string, also with setopt() curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // curl_exec() executes the started curl session and $output contains the output string
$output = curl_exec($curl); // close curl resource to free up system resources and (deletes the variable made by curl_init)
Note — our curl_exec() is stored in a variable $output. This $output variable is still available in our program even after we closed it with curl_close(). So after we did our call and closed the connection, we can still access the result using our $output variable.
PHP cURL setup
Implementing an external API into your project is probably going to take more than just one API call and from different pages in your project. This is why I’ve created a ‘simple’ PHP script that allows us to call this function, with a set of parameters, and a cURL request will be done.
Make sure to put this code into a file or place that can be accessed by your entire app or website.
Note — This is a basic setup for doing a cURL call and I’m using a
switch statement to check if the API call will be a POST, PUT, or something else (get or delete).
PHP cURL GET request
The most simple API call is the GET call, so let’s start with that! Our callAPI function expects 3 parameters: $method, $url and $data. We need to give those parameters to all our API calls, so for a cURL GET, we can just set $data on false because we are not passing any data with a GET call.
$get_data = callAPI('GET', 'https://yourapi.com/get_url/', false);
In-case you are receiving the response over json, you can further decode it:
$response = json_decode($get_data, true);
PHP cURL POST request
A snippet of code using the above function to sent the data via API to server.
data_array = array("data" => "some_record_data");
$make_call = callAPI('POST', 'https://yourapi.com/post_url/', json_encode($data_array));
$response = json_decode($make_call, true);
PHP cURL PUT request
A snippet of code using the above function to update the data via API.
$data_array = array("data" => "some_update");
$update_plan = callAPI('PUT', 'https://yourapi.com/put_url/', json_encode($data_array));
$response = json_decode($update_plan, true);
PHP cURL DELETE request
The delete request is very simple. We can just hit the API url with the $id we want to remove and poof… it’s gone forever.
callAPI('DELETE', 'https://api.example.com/delete_url/' . $id, false);
Note — The url and the way the data needs to be deleted can be changed according to the API.
If this post was helpful…it would mean a lot to me if you could click on the “claps” icon…up to 50 claps allowed — Thank You!