Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
REST Introduction
REST Concept
REST is
• Representational State Transfer between Resource
• A style of software architecture
• A Virtual state-machine
A network of web pages (a virtual state-machine),
where the user progresses through an application by selecting links (state
transitions), resulting in the next page (representing the next state of the application)
being transferred to the user and rendered for their use.
REST Data Elements
• Resources and Resource Identifiers
• Uniform Interface (GET, PUT, POST, DELETE)
• Resource Oriented
HTTP
Method
CRUD
Desc.
POST
CREATE
Create
-
GET
RETRIEVE
Retrieve
Safe,Idempotent,Cacheable
PUT
UPDATE
Update
Idempotent
DELETE
DELETE
Delete
Idempotent
REST Data Elements
Representations
• HTML / XML / images / sounds / …
REST V.S. SOAP
SOAP
• Simple Object Access Protocol
• RPC protocol that go through firewalls
• Communication protocol between applications
• A format for sending messages
REST V.S. SOAP
REST
•“The Web is the universe of globally accessible information”
• Resource oriented
• User-driven interactions via forms
• Few operations (generic interface) on many resources
• URI: Consistent naming mechanism for resources
• Focus on scalability and performance of large scale distributed
hypermedia systems
SOAP
•“The Web is the universal transport for messages”
• Activity/Service oriented
• Orchestrated reliable event flows
• Many operations (service interface) on few resources
• Lack of standard naming mechanism
• Focus on design of integrated (distributed) applications
REST V.S. SOA
Two of most common styles of use of Web Services
•Service-oriented architecture
• “Message oriented” (SOAP)
• Contract provided by WSDL
•REST
• Focus on interacting with stateful resources, rather than
messages or operations.
Slim Framework
http://www.slimframework.com/
Slim Framework
Slim is a PHP micro framework that helps you quickly write
simple yet powerful web applications and APIs.
One of the features – powerful routing
• REST URI
Slim Framework
Install in c:\xampp\htdocs\slim
Access: http://localhost/slim/
Apache .htaccess (to allow routing access in the index.php)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Slim Framework
REST Hello World
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
//REST routing with inline function
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
In browser url: http://localhost/slim/hello/jb
In browser output: Hello, jb
Slim Framework
REST Calculator
function doSum($num1, $num2)
{
$total = $num1 + $num2;
echo "$num1 + $num2 = $total";
}
//REST routing with external function and 2 parameters
//Sample link: http://localhost/slim/calc/2/3
$app->get('/calc/:num1/:num2', 'doSum');
In browser url: http://localhost/slim/calc/2/3
In browser output: 2 + 3 = 5
Slim Framework
Returning JSON
function doSumJson($num1, $num2)
{
$total = $num1 + $num2;
$calc = new Calc();
$calc->num1 = $num1;
$calc->num2 = $num2;
$calc->total = $total;
echo json_encode($calc);
}
//Returning a json
$app->get('/calcjson/:num1/:num2', 'doSumJson');
In browser url: http://localhost/slim/calcjson/2/3
In browser output: {"num1":"2","num2":"3","total":5}