Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CSE 2034Y

Internet Technologies and Web


Services

Lecture

Version: 1.0-15
RESTful Web Services – Part 2

Mr Shehzad Jaunbuccus
University of Mauritius

Email: s.jaunbuccus@uom.ac.mu
Overview

» RESTful URI design

» Experience scenario

» WS Client and cURL

» Client structure

» Server resp

» .htaccess configuration file

2
RESTful URI design

Good RESTful URI design should:


• Be Short and easy to read
• Meaningful (give clue about the type of
resource it represents. E.g., A book or person,
etc)
• Be predictable (showing the scoping
information is a consistent manner) such that
the link to a particular resource can be
guessed.
3
Experience scenario

You want to create a RESTful web service to show


the number of years of experience lecturers have
for particular modules at the CSE department.

Note: Same scenario as the SOAP web service

4
Experience scenario: resources

The structure of URIs for a RESTful service is very


important.

Before designing the URIs, it is important to know


the resources they will represent.

Hint: look for nouns and consider if there is a need


to keep data about them.

Possible resources for the Experience scenario:


Lecturers, Modules, Experiences*

5
Experience scenario: RESTful URI design

The URIs list the resource you want to operate on


E.g., the following link states that you want to operate on
module CSE1023Y
http://www.shehzad.edu/modules/CSE1023Y

Tips:
List resources in their plural form (e.g., modules instead of
module unlike entities)
The URI should be as simple as possible (user friendly). Do not
allow characters which require URL encoding E.g., spaces in
your URLs. They will have to be encoded with %20 and will be
hard to read. Instead use _ (underscore)or – (hyphen) to
represent spaces in your URLs

6
WS Client: cURL (client URL Request Library)

CURL wraps the libcurl library which is a multi-


protocol file transfer library.
This not only allows transmission over http, but
other protocols such as https, ftp, etc.
Q1: How is this any good for working with our
web service application?
A1: Libcurl was designed to work without user
interaction.
Which is exactly the purpose of web services!
Note: You need to enable CURL extension on your
application server. Min php req: php 4.02
7
cURL: main functions

Like our soap web service, the REST web service


will have a client which will pass on requests to
the server on the user’s behalf.
cURL function name Description

curl_init() Initialises a curl session

curl_setopt() Sets the options for the curl session. RESTful web services use http
methods and this function is used to set the type of method a
particular request will use. E.g, CURLOPT_POST will be used for
sending a request through POST.

curl_exec() This function is usually called after curl options have been set and it
allows the request to be sent to the server

curl_close() This closes the curl session which was initialised

8
cURL: main options (used with curl_setopt)
Setting the right curl options is very important since this
will determine the http method used to send the request.
Remember, RESTful URLs do not contain functions name
or the http method name!
cURL function name Description
CURLOPT_URL The location of the resource to process

CURLOPT_{POST|PUT} CURLOPT_POST, CURLOPT_PUT are used for their respective http


methods.
CURLOPT_CUSTOMREQUEST This is used to implement DELETE http method.
CURLOPT_POSTFIELDS Specifies the data which is sent in the request body. This is used in
conjunction with CURLOPT_POST, CURLOPT_PUT mostly
CURLOPT_RETURNTRANSFER When this option is specified, the response from the server is not
directly printed on console. It can be stored in a variable for more
processing. E.g., apply XSL etc.
Other options More options are available at:
http://php.net/manual/en/function.curl-setopt.php
Make sure you check them.
9
Client request: basic structure

1. Initialise the curl session


2. Set the required options
1. Specify the service location
2. Specify the request method
3. Disallow dumping of result on console
4. etc
3. Send the request
4. Close the curl session

10
Server processing: basic structure

1. Get the http verb by using


$_SERVER["REQUEST_METHOD“]
2. Get the parameters/data from the URL
3. Initialise DB connection
1. Use PDO library to deal with DB processing
4. Create response in appropriate format
XML/JSON
5. Send back response.

11
Configuration: mod_rewrite for Apache
mod_rewrite is an Apache module that allows for server-side
manipulation of requested URLs.

Where does the link below


http://www.shehzad.edu/modules/CSE1023Y points to? Which php
page processes this on the server?

Place a .htaccess configuration file in the WS server folder containing


the following code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ server.php

This will route all requests to server.php

12
References

[1] Leonard Richardson and Sam Ruby, RESTful


Web Services, O’Reilly Media, Inc., 2007.
[2] Client URL Library, Barry & Associates, Inc.,
http://php.net/manual/en/book.curl.php
[Accessed March 2015]

13

You might also like