A Comprehensive Guide to Making cURL API Calls with PHP, REST, JSON, and HTTP Verbs (GET, POST, PUT, DELETE)

Umesh S
4 min readFeb 12, 2023

--

Photo by Douglas Lopes on Unsplash

cURL is a command line tool and library for transferring data with URL syntax, supporting various protocols including HTTP, FTP, and SMTP. It is widely used in the world of web development and is often used to make API calls.

In this post, I will explore how to make cURL API calls with PHP, REST, and JSON data. I will cover the following topics:

  1. Setting up cURL in PHP
  2. Making GET requests
  3. Making POST requests
  4. Making PUT requests
  5. Making DELETE requests

Setting up cURL in PHP

Before I dive into the various types of API calls, let’s set up cURL in PHP. To use cURL in PHP, you will need to have the cURL library installed on your system. You can check if cURL is installed by creating a PHP file and adding the following code:

<?php
if (!function_exists('curl_version')) {
die('cURL is not installed on your server.');
}
?>

If cURL is installed, you should see a message that says “cURL is installed on your server.” If cURL is not installed, you will need to install it before you can continue.

Making GET requests

GET requests are used to retrieve data from an API. To make a GET request using cURL in PHP, you can use the following code:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "API_URL",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

In this code, First I initialize a cURL session using the curl_init() function. Then, I set various options for the request using the curl_setopt_array() function. The options I set include the URL of the API, the encoding type, the maximum number of redirects, the timeout, and the HTTP version. I also set the request type to GET using the CURLOPT_CUSTOMREQUEST option.

Finally, I make the request using the curl_exec() function and retrieve the response. After I have the response, I close the cURL session using the curl_close() function.

Making POST requests

POST requests are used to send data to an API. To make a POST request using cURL in PHP, you can use the following code:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "API_URL",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT =>0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{"key1":"value1","key2":"value2"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

In this code, first initialize a cURL session using the curl_init() function. Then, I set various options for the request using the curl_setopt_array() function.

The options I set include the URL of the API, the encoding type, the maximum number of redirects, the timeout, and the HTTP version. I also set the request type to POST using the CURLOPT_CUSTOMREQUEST option.

Then I set the data I want to send in the request using the CURLOPT_POSTFIELDS option.

In this example, I are sending JSON data with two key-value pairs. Finally, I set the Content-Type header to “application/json” to indicate that I are sending JSON data.

Finally, I make the request using the curl_exec() function and retrieve the response. After I have the response, I close the cURL session using the curl_close() function.

Making PUT requests

PUT requests are used to update data in an API. To make a PUT request using cURL in PHP, you can use the following code:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "API_URL",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"key1\":\"value1\",\"key2\":\"value2\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

The code for making a PUT request is similar to the code for making a POST request. The main difference is that I set the request type to PUT using the CURLOPT_CUSTOMREQUEST option.

Making DELETE requests

DELETE requests are used to delete data from an API. To make a DELETE request using cURL in PHP, you can use the following code:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "API_URL",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

The code for making a DELETE request is similar to the code for making a GET or POST request. The main difference is that I set the request type to DELETE using the CURLOPT_CUSTOMREQUEST option.

Handling API Responses

After making an API request, you need to handle the response from the API. The response will usually be in the form of JSON or XML data. To handle the response in PHP, you can use the following code:

$response = json_decode($response, true);

if (isset($response["key"])) {
// handle response
} else {
// handle error
}

In this code, I use the json_decode() function to convert the response from a JSON string to a PHP array.

Then, I check if the key I are interested in exists in the response using the isset() function. If the key exists, I handle the response. If it doesn’t exist, I handle the error.

In this post, I have covered how to make API calls using cURL in PHP, and how to handle the response from the API. By using cURL, you can easily make HTTP requests to REST APIs and retrieve or modify data using GET, POST, PUT, and DELETE requests. You can use this information to build robust applications that interact with APIs to retrieve or modify data in real-time.

--

--

Umesh S

Experienced Software Engineer committed to helping others grow and succeed.