13 May 2018

How to consume or call REST API in PHP

REST API call in PHP

It was very tough days for me after developing RESTful Web service in PHP using SLIM framework, I was excited to use it in my PHP project itself like we consume REST API in Android development. But I have no idea how can I consume or call REST API from my PHP project. 

I searched and talk to PHP developer friends a lot but there is no luck because all suggesting to me to use framework for this. But I want to use it into PHP core for better understanding. Because I have no background in PHP, I am an Android Application Developer. I learn PHP for fun or my own project. 

I learn a lot in this process. The solution lies inside PHP itself. Php has a library which is called cURL, used in calling RESTful web service or REST API.

What is cURL

The cURL library (or, 'libcurl' which is the package name on the server) is often used in PHP to access data from outside web pages. Common uses include reading RSS feeds or accessing third-party API systems.

Without wasting time let's start learning how can be call REST API using PHP. You would like:
Note:- If you have no understanding about PHP and class. Then first learn from here.

How to consume RESTful web services in PHP

To make code organize and easy to maintain. I have created a separate util class for calling cURL each time. This class after process returns the REST API value. So, My PHP project structure is like that

--htdocs (I am using XMPP server)
  |
----just (project name as folder name)
     |
------CurlClass.php file
------Testing.php file

CurlClass.php file has following class

testing.php file code as below
<?php
//way to include external file in current code
include ('CurlClass.php'); 

//url of REST Web service
$url = "http://localhost/selfdev/SlimApi/public/login"; 
$method = "POST"; //http request type
$data = array("email" => "ilvtechnology@gmail2.com", "password" => "123456");

$callApiResult = new CurlClass($url, $method, $data); //calling CurlClass class

//calling CurlClass function using CurlClass object. This function echo json string so consider it as return
$result = $callApiResult->apiCallReturn(); 

$callApiResult->connClose(); //close Curl after use

print_r($result); // will print what rest api will return

$decoded = json_decode($result); // json string convert to Std object
print_r($decoded);
$resultSuccess = $decoded->error; // get value of std obj as show in code
$resultResponse = $decoded->userDetail; 
$email = $resultResponse->email;

$email2 = $decoded->userDetail->email;
echo $email2;
print_r($resultSuccess);
print_r($resultResponse);
print_r($responseinarray);
print_r($email);
  
//How to get Data from JSON when JSON has a list

//$result is string that is returned by REST API call
$decoded = json_decode($result); //get stdClass object

$allDetail = $decoded->allDetail; // get stdClass object array from allDetail property
for ($x = 0; $x <= count($decoded); $x++) {
    $name = $allDetail[$x]->fname;
    echo "The name is: $name";
}
Here is a link that has a nice explanation to get data from JSON in PHP. Any doubt regarding code, feel free to ask. You know REST API call in PHP using different
way, must comment and acknowledge me and others.

You know a better way to call web service or API using PHP then do comment and acknowledge me and others.

 

No comments:

Post a Comment

Ads Inside Post