PHP : REST API – GET data using cURL

In this tutorial, we will learn how to GET API data using cURL functions.

What is cURL?

cURL (stands for client URL) is a command-line tool to transfer data between servers. It supports various protocols such as HTTP, HTTPS, FTP, FTPS, IMAP etc. Most programming languages support cURL commands using some libraries or extension.

cURL is one of the popular methods to interact with REST API data. PHP use cURL library (module) to provide access curl functions within PHP. By default, most PHP installations come with cURL support is enabled.


How to use cURL in PHP

The basic process of PHP cURL can be divided into four parts.

  • Initialize a cURL session using $curl_handle = curl_init();
  • Pass URL option in the cURL session curl_setopt($curl_handle, CURLOPT_URL, $url);
  • Execute cURL session & store data: $api_data = curl_exec($handle);
  • Close cURL session: curl_close($curl_handle);

Sample REST API data in JSON

We will use Dummy REST API Example website to work with live API data in our examples. This website provides a number of routes or URLs to read, create, update, delete employee’s data using API. We will use following to get employees data.

GET Employees data using API URL: http://dummy.restapiexample.com/api/v1/employees

Above URL provides employee’s profile data in JSON format something similar to following:

[{"id":"110","employee_name":"Robin","employee_salary":"54000","employee_age":"65"},
{"id":"112","employee_name":"Deepak","employee_salary":"12350","employee_age":"29"},
{"id":"114","employee_name":"Ankit","employee_salary":"90000","employee_age":"18"}


PHP program to GET REST API data using cURL

In the following PHP program, we will use a sample PHP CURL function to get employee’s data and displays them.

<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();
 
$url = "http://dummy.restapiexample.com/api/v1/employees";
 
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
 
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
 
// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);
 
curl_close($curl_handle);
 
// Decode JSON into PHP array
$response_data = json_decode($curl_data);

// Print all data if needed
// print_r($response_data);
// die();

// All user data exists in 'data' object
$user_data = $response_data->data;
 
// Extract only first 5 user data (or 5 array elements)
$user_data = array_slice($user_data, 0, 4);
 
// Traverse array and print employee data
foreach ($user_data as $user) {
	echo "name: ".$user->employee_name;
	echo "<br />";
}
 
?>

Code Explanation

First, we need to initiate a curl and pass your API URL. Then execute CURL & store received data. As API URL returns JSON data, we need to decode it using json_decode. Now you have all data as an array which you can traverse using foreach and display accordingly.

In the next chapters, we will use cURL methods to perform an update, create and delete operations using API data.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.