Skip to main content

Consuming RESTful Web Services using PHP

In our previous tutorial, we have explained how to Make Simple RESTful API with PHP. In this tutorial, we will explain how to consume RESTful API using PHP.

REST (Representational State Transfer) API has been getting popularity as it’s very simple and easy to use. The REST is stateless architecture that generally consists of clients, servers and HTTP operations known as request methods (GET, POST, PUT, DELETE).

Currently most of web applications are implemented with REST API as it’s best solution when the same data used by different applications. If you’re a PHP developer, you’re well aware about the importance of REST API when developing mobile applications.

So if you’re developing web application using REST API and looking for solution to consume RESTful web services, then you’re here at right place.

Also, read:

We will cover this tutorial step by step to consume RESTful API methods (GET, POST, PUT, DELETE) with live REST API examples. As in our previous REST API tutorial, we have created RESTFul API with (GET, POST, PUT, DELETE) to perform operation on product data. So we will consume this RESTful API in this example.

Step1: Create Function To Call REST API Methods

If you’re implementing a web application with REST API, you just need an API call for different methods. So we need to create a simple PHP function to allow REST API HTTP calls for different methods. We will create a function requestApi($method, $apiUrl, $data) with three parameters to make REST API HTTP request for different methods (GET, POST, PUT, DELETE) and get response.

function requestApi($method, $apiUrl, $data){
   $curl = curl_init();
   switch ($method){
      case "POST":
         curl_setopt($curl, CURLOPT_POST, 1);
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         break;
      case "PUT":
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);			 					
         break;
      default:
         if ($data)
            $apiUrl = sprintf("%s?%s", $apiUrl, http_build_query($data));
   }
   
   curl_setopt($curl, CURLOPT_URL, $apiUrl);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'APIKEY: 123456789',
      'Content-Type: application/json',
   ));
   
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
   
   $resultData = curl_exec($curl);
   if(!$resultData){
		die("Invalid API Request!");
	}
   curl_close($curl);
   return $resultData;
}

Step2: Make GET Method REST API Call

We will make API call with GET method to get all product records. We will call method requestApi() and pass three parameters API request method, API URL and data if needed. As there are no need set data with GET API call, so we just pass it as false. The function requestApi() will make HTTP request and get all products response data.

<?php
$response = requestApi('GET', 'https://coderszine.com/demo/api/product/read.php', false);
print_r($response);
?>

We can also make API call for a specific product by just passing product id.

<?php
$pid = 1;
$response = requestApi('GET', 'https://coderszine.com/demo/api/product/read.php?id='.$pid, false);
print_r($response); 
?>

Step3: Make POST Method REST API Call

We will make API call with POST method using function requestApi() with details to create a new product record. We will pass required POST data as JSON data to create a new product record. As the API POST method require JSON data, so we always need to pass JSON data otherwise it will return error. The response data will have API request status.

<?php
$postData = array (
	"sku_id"=> "1234567ui",
	"category_id"=> 3,
	"product_name"=>"Camera",
	"price"=>"$4000",
	"brand"=>"Sony",
	"material"=> "Steel",
	"size"=>30,
	"qty"=> 12,
	"created_date"=> "2019-10-27 04:30:00"
);

$response = requestApi('POST', 'https://coderszine.com/demo/api/product/create.php', json_encode($postData));	
print_r($response);
?>

Step4: Make PUT Method REST API Call

We will make PUT method REST API call using function requestApi() with JSON data to update the existing product record. We will JSON data with update values and product id to update the specific record.

<?php
$data = array (
	"id" => 17,
	"sku_id"=> "1234567ui",
	"category_id"=> 3,
	"product_name"=>"Nice CAmera",
	"price"=>"$5000",
	"brand"=>"Sony",
	"material"=> "Steel",
	"size"=>30,
	"qty"=> 12,
	"created_date"=> "2019-10-27 04:30:00"
);

$response = requestApi('PUT', 'https://coderszine.com/demo/api/product/update.php', json_encode($data));
print_r($response);
?>

Step5: Make DELETE method REST API Call

We will make DELETE method API call by using requestApi() function and pass JSON data with product id to delete it.

<?php
$data = array (
	"id" => 17		
);
$response = requestApi('PUT', 'https://coderszine.com/demo/api/product/delete.php', json_encode($data));
print_r($response);
?>

Step6: Conclusion

This is a simple tutorial to consume simple RESTful API with methods (GET, POST, PUT, DELETE) using PHP. You can also make changes to this API call method to use flexible headers etc according to your requirement.

You may also like: