PHP and Web Services
Bruno Pedro <bpedro@computer.org>
CITOT - May 20th 2007
PHP andWeb Services
Summary
• What is PHP?
• Web Services
• SOAP
• REST
• JSON
• Conclusions
2
PHP andWeb Services
What is PHP?
• PHP: Hypertext Preprocessor
• Open Source Web scripting language
• Created in 1994
• Syntax inherited from C, Java and Perl
• Powerful, yet easy to learn
3
PHP andWeb Services
How is PHP used?
• Content Management
• Forums
• Blogging
• Wiki
• CRM
4
PHP andWeb Services
Who uses PHP?
Source: Zend
5
PHP andWeb Services
Why use PHP?
• Used by 20M+ Web Sites around the globe
• Community-maintained documentation
• Code runs on UNIX, OSX and Windows
• Backed commercially by Zend
• Easy syntax
6
PHP andWeb Services
The PHP language
7
Easy syntax!
PHP andWeb Services
Creating a function
8
function declaration
return value
function call
PHP andWeb Services
Creating a class
9
class declaration
public method
method call
PHP andWeb Services
Web Services
10
Source:The Server Side
(http://tinyurl.com/38umxt)
PHP andWeb Services
What are Web Services?
• Interoperable interactions
• Calls are made over a network
• Lots of different standards
• Popular Web sites use them
11
PHP andWeb Services
Requirements
• PHP 5.2.2
• SOAP
• simplexml
• DOM
• JSON
12
PHP andWeb Services
SOAP
Source: Prentice Hall
(http://tinyurl.com/33dcch)
13
PHP andWeb Services
SOAP
• Simple Object Access Protocol
• Usually an HTTP POST request
• Call is encapsulated in XML
• Response is an XML document
• PHP handles everything
14
PHP andWeb Services
Server implementation
15
namespace
start a server
assign a class
handle incoming calls
PHP andWeb Services
Client implementation
16
namespace
endpoint
display result
PHP andWeb Services
Output
17
namespace
result
PHP andWeb Services
SOAP
18
• Support for SOAP 1.1, 1.2 and WSDL 1.1
• Handles simple and complex types
• Very easy implementation
• Output is quite bulky
PHP andWeb Services
REST
http://example.com/calculator/sum/?x=121&y=233
GET
PUT
POST
DELETE
«interface»
Resource
GET - perform a calculation
PUT - not used
POST - not used
DELETE - not used
/calculator/sum/
19
PHP andWeb Services
REST
• Representational State Transfer
• Usually an HTTP GET request
• Call is made through GET parameters
• Response is an XML document
• Create response with DOM
• Interpret response with simplexml
20
PHP andWeb Services
Server implementation
21
handle incoming call
create an
XML document
add elements
output result
PHP andWeb Services
Client implementation
22
endpoint
arguments
load XML document
output result
PHP andWeb Services
Output
23
result
PHP andWeb Services
REST
24
• Interaction needs to be implemented
• XML schema must be know beforehand
• Complex types not handled natively
• Output is usually RSS
PHP andWeb Services
JSON
25
JSON Logo
Source: json.org
PHP andWeb Services
JSON
• JavaScript Object Notation
• REST approach
• Except response is not XML
• Used on the client side
• Create response with json_encode()
• Interpret response in JavaScript
26
PHP andWeb Services
Server implementation
27
handle incoming call
generate result
output
JSON string
PHP andWeb Services
Client implementation
28
endpoint
arguments
output result
PHP andWeb Services
Output
29
result
PHP andWeb Services
JSON
30
• REST approach
• Handles simple and complex types
• Calls can be made with AJAX
• Can also be used on the server side
PHP andWeb Services
Conclusions
• PHP is a powerful web scripting language
• Too many Web Services standards
• PHP handles all the internals
• REST requires more coding than SOAP
• Use JSON for client side applications
• XML-RPC support is still experimental
31
PHP andWeb Services
Questions?
32
PHP andWeb Services
Resources
33
• My blog: http://unfoldingtheweb.com/
• PHP Manual: http://www.php.net/
• SOAP: http://www.w3.org/TR/soap/
• REST: http://tinyurl.com/akhc7
• JSON: http://www.json.org/

Php and webservices