How to consume REST Web Service in PHP

Hi all,

As promised on my last technical post https://blog.niteshapte.com/2013-11-08-how-to-consume-rest-web-service-in-java.htm I am posting the same thing in different technology/language. So, this post is about Consuming REST web services using PHP. Let’s start it again….

So, first thing first. What is Web Service?

Web Services is a technology which provides an efficient way to share application logic across multiple machines running various operating systems and using different development environments.

If you guys want to know more about Web Services, then Google up ;-). But one thing, don’t jump into advance stuff. Clear you basics first or things could get messy 😛

Moving ahead, web services are of two types:

1. SOAP
2. REST

In this post I will cover only REST web service. So, what is REST Web Service?

Representational State Transfer (REST) is an architectural style that abstracts the architectural elements within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements.

In 2000, Roy Fielding wrote his doctoral dissertation about architectural styles and designs of network-based software architectures. Within it, he speaks of Web architectures where any information or concept that can be named, referred to as a resource, is identified by a resource identifier, such as a URI. In common terms, you can think of the Yahoo! home page as a resource with a resource identifier of http://www.yahoo.com/.rest-featured

Components in this architecture perform actions on the resource using its representation. For instance, using a browser to request the resource, a server transfers the current state of a Web page to the browser. The browser is then able to perform the action of rendering the representation. Simply put, the server sends the data for the requested page in its current state, and the data is rendered. Navigating to any of the links within the rendered page causes a state transition because the next page, which could be considered another state of an application, is transferred to the browser for rendering.

For more details on REST Web Service, please check the article by Roy Fielding – http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Let’s check out the code now. First let use file_get_contents() method of PHP

<?php

$url = 'https://www.restwebserviceurl.com';

// prepare the body data. Example is JSON here
$data = json_encode(array(
'description' => 'Inspiring Poetry',
'public' => 'true',
'files' => array(
'poem.txt' => array(
'content' => 'If I had the time, I\'d make a rhyme'
)
)
));

// set up the request context
$options = ["http" => [
"method" => "POST",
"header" => ["Authorization: token " . $access_token,
"Content-Type: application/json"],
"content" => $data
]];
$context = stream_context_create($options);

// make the request
$response = file_get_contents($url, false, $context);

Easy, isn’t it?

The second most famous approach in PHP is cURL.

<?php
 $url = 'http://restwebserviceurl.com/';
       $curl = curl_init($url);
       $data = json_encode(array(
'description' => 'Inspiring Poetry',
'public' => 'true',
'files' => array(
'poem.txt' => array(
'content' => 'If I had the time, I\'d make a rhyme'
)
)
));
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_POST, true);
       curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
       $curl_response = curl_exec($curl);
       curl_close($curl);
?>

rest-featuredThis is the most basic implementation. The reason to choose this example is to tell that REST web service work on HTTP and uses HTTP methods, PUT, GET, POST, DELETE, HEAD, OPTIONS, TRACE and CONNECT. So, it is like just accessing some URL through your code (using different HTTP methods). There are several frameworks/tools available which help you to consume REST web services. You can try those too. It will be fun.

 

That’s all folk.

Critics/suggestion are very much welcome.

Have a nice day ahead.

Loading

1 Reply to “How to consume REST Web Service in PHP”

  1. Could you change the font color for the PHP code to a different color than white? It’s really hard to see with the yellow-ish background on it. Thanks for the article! 😀

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Follow Me