SlideShare a Scribd company logo
1 of 26
Download to read offline
REST –Web Services
Web Basics: Retrieving Information using HTTP
GET
• The user types in at his browser: http://www.amazon.com
• - The browser software creates an HTTP header (no payload)
• - The HTTP header identifies:
• - The desired action: GET ("get me resource")
• - The target machine (www.amazon.com)
Amazon
Web Server
http://www.amazon.com
GET / HTTP/1.1
Host: http://www.amazon.com
Web Basics: Updating Information using HTTP
POST
• The user fills in the Web page's form
• The browser software creates an HTTP header with a payload
comprised of the form data. The HTTP header identifies:
The desired action: POST ("here's some update info")
The target machine (amazon.com)
The payload contains: The data being POSTed
Credit Card: Visa
Number: 123-45-6789
Expiry: 12-04-06
Credit Card: Visa
Number: 123-45-6789
Expiry: 12-04-06
Amazon
Web Server
What is REST?
• RESET stands for Representational state transfer
•" REST " was coined by Roy Fielding in his Ph.D. to describe a
design pattern for implementing networked systems.
• In the REST architectural style, data and functionality are
considered resources and are accessed usingUniform Resource
Identifiers (URIs), typically links on the Web.
Creating RESTful Web Service
Learn by example
• We are going to create a web service for a library -so that it will manage
request from clients and will respond with the details of the book
requested
• We will have the following files in our project (in www/library/)
– Functions.php // has an array of books and will return the price of book requested
– Index.php // will handle requests from clients and respond with data
– Request.php // will act as the client who request the data
<?php /* functions.php */
function get_price($find) {
$books = array('java'=>300,
'php'=>200,
'c'=>100);
foreach($books as $book=>$price) {
if($book==$find)
{
return $price;
break;
}
}
}
?>
Functions.php
• We have created a simple php
page named “function.php”
• Inside the page we have defined
a function named “get_price()”
that will accept book name as
argument and will return with its
price
Index.php
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found", NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request", NULL);
}
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
Created a header mentioning the
response will be in json format
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
Including the page functions.php
that we have created earlier
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
Checking whether the URL request
contains the name of book or not?
If the URL request doesn’t have the
book name returns error message
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
Accepting the argument into a
variable named “$name”
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
Calling the function get_price() with
the book name and stores the price
returned in a varaible named $price
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
If the price is empty – calls a
function named deliver_response
with the “book not found”
arguments
Creating RESTful Web Service
<?php
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name'])) {
$name = $_GET['name'];
$price = get_price($name);
if(empty($price))
deliver_response(200, "book not found",
NULL);
else
deliver_response(200, "book found", $price);
}
Else {
deliver_response(400, "Invalid Request",
NULL);
}
If the price is not empty – call the
same method deliver_response
with “book found ” arguments
Creating RESTful Web Service
function deliver_response($status,$status_message,$data)
{
header("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
?>
Sets the header witht the
status code and message
Creating RESTful Web Service
function deliver_response($status,$status_message,$data)
{
header("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
?>
Creates an array of status,
messafe and the data(ie
price of book)
Creating RESTful Web Service
function deliver_response($status,$status_message,$data)
{
header("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
?>
Create a json object using
an inbuilt function in php
named json_encode() and
passing the array we just
created as argument
Creating RESTful Web Service
function deliver_response($status,$status_message,$data)
{
header("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
?>
Echo the json object so
that any clients request
the book will be get the
details as a json object
How to call a Web Service – request.php
<?php
$url= 'http://127.1.1.0/library/?name=php';
$response = file_get_contents($url);
$data = json_decode($response, true);
var_dump($data);
?>
Creates a url to which we pass an
argument name=php
How to call a Web Service – request.php
<?php
$url= 'http://127.1.1.0/library/?name=php';
$response = file_get_contents($url);
$data = json_decode($response, true);
var_dump($data);
?>
Makes the request with the url we
created and stores the result in a
variable named $response(obviously
the result will be returned in json
format)
How to call a Web Service – request.php
<?php
$url= 'http://127.1.1.0/library/?name=php';
$response = file_get_contents($url);
$data = json_decode($response, true);
var_dump($data);
?>
Decodes the json result into array
format and displays it on the screen
Questions?
“A good question deserve a good grade…”
Self Check !!
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

What's hot

PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databaseLeon Fayer
 
Zf Zend Db by aida
Zf Zend Db by aidaZf Zend Db by aida
Zf Zend Db by aidawaraiotoko
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
Getting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainGetting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainDrewAPicture
 
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway ichikaway
 
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa HallPitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hallhannonhill
 
Manage catalog Configueation In Sharepoint PowerShell
Manage catalog Configueation In Sharepoint PowerShellManage catalog Configueation In Sharepoint PowerShell
Manage catalog Configueation In Sharepoint PowerShellChitexe Marcos Maniche
 
Image upload in php MySql
Image upload in php MySqlImage upload in php MySql
Image upload in php MySqlIshaq Shinwari
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?Maksym Hopei
 
Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Kris Wallsmith
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery PresentationSony Jain
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORMYaroslav Muravskyi
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-KjaerCOMMON Europe
 
Ruby sittin' on the Couch
Ruby sittin' on the CouchRuby sittin' on the Couch
Ruby sittin' on the Couchlangalex
 
The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...React London 2017
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax pluginsInbal Geffen
 
How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!
How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!
How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!Ricardo Signes
 

What's hot (20)

PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a database
 
Zf Zend Db by aida
Zf Zend Db by aidaZf Zend Db by aida
Zf Zend Db by aida
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Getting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainGetting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, Again
 
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
 
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa HallPitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
 
Manage catalog Configueation In Sharepoint PowerShell
Manage catalog Configueation In Sharepoint PowerShellManage catalog Configueation In Sharepoint PowerShell
Manage catalog Configueation In Sharepoint PowerShell
 
Image upload in php MySql
Image upload in php MySqlImage upload in php MySql
Image upload in php MySql
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?
 
Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery Presentation
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORM
 
Assetic (OSCON)
Assetic (OSCON)Assetic (OSCON)
Assetic (OSCON)
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-Kjaer
 
Ruby sittin' on the Couch
Ruby sittin' on the CouchRuby sittin' on the Couch
Ruby sittin' on the Couch
 
The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
Php summary
Php summaryPhp summary
Php summary
 
How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!
How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!
How I Learned to Stop Worrying and Love Email::: The 2007 PEP Talk!!
 

Viewers also liked

REST & RESTful Web Service
REST & RESTful Web ServiceREST & RESTful Web Service
REST & RESTful Web ServiceHoan Vu Tran
 
Java Script Object Notation (JSON)
Java Script Object Notation (JSON)Java Script Object Notation (JSON)
Java Script Object Notation (JSON)Adnan Sohail
 
RESTful web
RESTful webRESTful web
RESTful webAlvin Qi
 
REST - Representational State Transfer
REST - Representational State TransferREST - Representational State Transfer
REST - Representational State TransferPeter R. Egli
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServicesPrateek Tandon
 
Understanding REST
Understanding RESTUnderstanding REST
Understanding RESTNitin Pande
 
ReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedDhananjay Nene
 

Viewers also liked (14)

REST & RESTful Web Service
REST & RESTful Web ServiceREST & RESTful Web Service
REST & RESTful Web Service
 
Java Script Object Notation (JSON)
Java Script Object Notation (JSON)Java Script Object Notation (JSON)
Java Script Object Notation (JSON)
 
REST Presentation
REST PresentationREST Presentation
REST Presentation
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
RESTful web
RESTful webRESTful web
RESTful web
 
Json tutorial
Json tutorialJson tutorial
Json tutorial
 
REST - Representational State Transfer
REST - Representational State TransferREST - Representational State Transfer
REST - Representational State Transfer
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServices
 
Understanding REST
Understanding RESTUnderstanding REST
Understanding REST
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
RESTful API Design, Second Edition
RESTful API Design, Second EditionRESTful API Design, Second Edition
RESTful API Design, Second Edition
 
ReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedReST (Representational State Transfer) Explained
ReST (Representational State Transfer) Explained
 
JSON and REST
JSON and RESTJSON and REST
JSON and REST
 

Similar to Intoduction to php restful web service

Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011Alessandro Nadalin
 
JavaScript Testing for Rubyists
JavaScript Testing for RubyistsJavaScript Testing for Rubyists
JavaScript Testing for RubyistsJamie Dyer
 
Simplify AJAX using jQuery
Simplify AJAX using jQuerySimplify AJAX using jQuery
Simplify AJAX using jQuerySiva Arunachalam
 
How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfAppweb Coders
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConRafael Dohms
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)andrewnacin
 
DEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceDEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceCtvrtkoncz
 
Intro to php
Intro to phpIntro to php
Intro to phpSp Singh
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersKacper Gunia
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needKacper Gunia
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011John Ford
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Kacper Gunia
 
Rapid Prototyping with PEAR
Rapid Prototyping with PEARRapid Prototyping with PEAR
Rapid Prototyping with PEARMarkus Wolff
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowKacper Gunia
 
Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017
Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017
Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017Codemotion
 

Similar to Intoduction to php restful web service (20)

Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 
JavaScript Testing for Rubyists
JavaScript Testing for RubyistsJavaScript Testing for Rubyists
JavaScript Testing for Rubyists
 
Simplify AJAX using jQuery
Simplify AJAX using jQuerySimplify AJAX using jQuery
Simplify AJAX using jQuery
 
How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdf
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
 
DEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceDEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent Interface
 
Intro to php
Intro to phpIntro to php
Intro to php
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
WordPress Plugin & Theme Security - WordCamp Melbourne - February 2011
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
Rapid Prototyping with PEAR
Rapid Prototyping with PEARRapid Prototyping with PEAR
Rapid Prototyping with PEAR
 
JavaScript JQUERY AJAX
JavaScript JQUERY AJAXJavaScript JQUERY AJAX
JavaScript JQUERY AJAX
 
Rails is not just Ruby
Rails is not just RubyRails is not just Ruby
Rails is not just Ruby
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017
Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017
Tomer Elmalem - GraphQL APIs: REST in Peace - Codemotion Milan 2017
 

More from baabtra.com - No. 1 supplier of quality freshers

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 

Intoduction to php restful web service

  • 2. Web Basics: Retrieving Information using HTTP GET • The user types in at his browser: http://www.amazon.com • - The browser software creates an HTTP header (no payload) • - The HTTP header identifies: • - The desired action: GET ("get me resource") • - The target machine (www.amazon.com) Amazon Web Server http://www.amazon.com GET / HTTP/1.1 Host: http://www.amazon.com
  • 3. Web Basics: Updating Information using HTTP POST • The user fills in the Web page's form • The browser software creates an HTTP header with a payload comprised of the form data. The HTTP header identifies: The desired action: POST ("here's some update info") The target machine (amazon.com) The payload contains: The data being POSTed Credit Card: Visa Number: 123-45-6789 Expiry: 12-04-06 Credit Card: Visa Number: 123-45-6789 Expiry: 12-04-06 Amazon Web Server
  • 4. What is REST? • RESET stands for Representational state transfer •" REST " was coined by Roy Fielding in his Ph.D. to describe a design pattern for implementing networked systems. • In the REST architectural style, data and functionality are considered resources and are accessed usingUniform Resource Identifiers (URIs), typically links on the Web.
  • 6. Learn by example • We are going to create a web service for a library -so that it will manage request from clients and will respond with the details of the book requested • We will have the following files in our project (in www/library/) – Functions.php // has an array of books and will return the price of book requested – Index.php // will handle requests from clients and respond with data – Request.php // will act as the client who request the data
  • 7. <?php /* functions.php */ function get_price($find) { $books = array('java'=>300, 'php'=>200, 'c'=>100); foreach($books as $book=>$price) { if($book==$find) { return $price; break; } } } ?> Functions.php • We have created a simple php page named “function.php” • Inside the page we have defined a function named “get_price()” that will accept book name as argument and will return with its price
  • 8. Index.php <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); }
  • 9. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } Created a header mentioning the response will be in json format
  • 10. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } Including the page functions.php that we have created earlier
  • 11. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } Checking whether the URL request contains the name of book or not? If the URL request doesn’t have the book name returns error message
  • 12. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } Accepting the argument into a variable named “$name”
  • 13. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } Calling the function get_price() with the book name and stores the price returned in a varaible named $price
  • 14. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } If the price is empty – calls a function named deliver_response with the “book not found” arguments
  • 15. Creating RESTful Web Service <?php header("Content-Type:application/json"); include("functions.php"); if(!empty($_GET['name'])) { $name = $_GET['name']; $price = get_price($name); if(empty($price)) deliver_response(200, "book not found", NULL); else deliver_response(200, "book found", $price); } Else { deliver_response(400, "Invalid Request", NULL); } If the price is not empty – call the same method deliver_response with “book found ” arguments
  • 16. Creating RESTful Web Service function deliver_response($status,$status_message,$data) { header("HTTP/1.1 $status $status_message"); $response['status']=$status; $response['status_message']=$status_message; $response['data']=$data; $json_response = json_encode($response); echo $json_response; } ?> Sets the header witht the status code and message
  • 17. Creating RESTful Web Service function deliver_response($status,$status_message,$data) { header("HTTP/1.1 $status $status_message"); $response['status']=$status; $response['status_message']=$status_message; $response['data']=$data; $json_response = json_encode($response); echo $json_response; } ?> Creates an array of status, messafe and the data(ie price of book)
  • 18. Creating RESTful Web Service function deliver_response($status,$status_message,$data) { header("HTTP/1.1 $status $status_message"); $response['status']=$status; $response['status_message']=$status_message; $response['data']=$data; $json_response = json_encode($response); echo $json_response; } ?> Create a json object using an inbuilt function in php named json_encode() and passing the array we just created as argument
  • 19. Creating RESTful Web Service function deliver_response($status,$status_message,$data) { header("HTTP/1.1 $status $status_message"); $response['status']=$status; $response['status_message']=$status_message; $response['data']=$data; $json_response = json_encode($response); echo $json_response; } ?> Echo the json object so that any clients request the book will be get the details as a json object
  • 20. How to call a Web Service – request.php <?php $url= 'http://127.1.1.0/library/?name=php'; $response = file_get_contents($url); $data = json_decode($response, true); var_dump($data); ?> Creates a url to which we pass an argument name=php
  • 21. How to call a Web Service – request.php <?php $url= 'http://127.1.1.0/library/?name=php'; $response = file_get_contents($url); $data = json_decode($response, true); var_dump($data); ?> Makes the request with the url we created and stores the result in a variable named $response(obviously the result will be returned in json format)
  • 22. How to call a Web Service – request.php <?php $url= 'http://127.1.1.0/library/?name=php'; $response = file_get_contents($url); $data = json_decode($response, true); var_dump($data); ?> Decodes the json result into array format and displays it on the screen
  • 23. Questions? “A good question deserve a good grade…”
  • 25. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 26. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com