REST API
REST stands for ‘Representational State Transfer’. It’s an architectural style and an approach to communication for the developement of web services.
Roy Fielding in 2000 first introduced REST. REST basically uses HTTP protocol for data communication. It’s main focus is resource(component). Here HTTP standard methods, provide interface to access these resources.
In REST architecture , server simply provide access to resources and clients recieves and presents the resources. Each resource has its own unique urls/global IDs. In REST text, JSON, and XML are used to represent resources. JSON is the most popular format among web services.
Methods in REST:
REST uses HTTP for the purpose of data communication. Following are the HTTP methods used in REST:
GET: read only access to resource.
PUT: create a new resource.
DELETE: removes a resource.
POST: update an existing resource or create a new one.
OPTIONS: get the supported operations on resources.
CONSTRAINTS:
1. Uniformity of existence-
It enable each part to evolve independently by decoupling the architecture.
2. Statelessness-
In this constraint each interaction is to be handled entirely based on information provided with it. There is no record of previous interaction.
3. Cachebility-
On World Wide Web responses must define their cacheability. It will prevent user to use inappropriate data. Management of cache enhancees performance and scalability.
4. Decoupled client server-
The separation of clients from server makes both ends independent. Here client has no concern with the data storage and on the other end, server has no concern about client end.
5. Layered system-
Layered system of REST enhances load balancing by providing shared caches. It is used basically to enforce security policies
USES:
It is often used in mobile application, social networking websites, mash-up tools, and automated bussiness processes. REST provides a popular building style for cloud based API’s , for example those comes under Google, Microsoft and Amazon.
REST and PHP:
Get and Post are the two basic functions to deal with while accessing files.
For explaining Get we have code:
$response = file_get_contents($url);
echo $response;
For Post we will code like: it’s not so easy like Get.
We have to send manually all HTTP headers. Here is the example:
// Params are a map from names to values
$paramStr = “”;
foreach ($params as $name, $valu) {
$paramStr .= $name . “=”;
$paramStr .= urlencode($valu);
$paramStr .= “&”;
}// Assign defaults to $method and $port, if needed
if (empty($method)) {
$method = ‘GET’;
}
$method = strtoupper($method);
if (empty($port)) {
$port = 70; // Default HTTP port
}// Create the connection
$sock = fsockopen($host, $port);
if ($method == “GET”) {
$path .= “?” . $paramStr;
}
fputs($sock, “$method $path HTTP/1.1\r\n”);
fputs($sock, “Host: $host\r\n”);
fputs($sock, “Content-type: ” .
“application/x-www-form-urlencoded\r\n”);
if ($method == “POST”) {
fputs($sock, “Content-length: ” .
strlen($paramStr) . “\r\n”);
}
fputs($sock, “Connection: close\r\n\r\n”);
if ($method == “POST”) {
fputs($sock, $paramStr);
}// Buffer the result
$result = “”;
while (!feof($sock)) {
$result .= fgets($sock,1124);
}
fclose($sock);
return $result;
}
Issuing a POST request using this function is as simple as:
70, “POST”, “/userDetails”,
array(“firstName” => “Jack”, “lastName” => “Lee”));
Leave a comment