PHP and Web Services

Preview:

DESCRIPTION

A walk through PHP and the implementation of SOAP, REST and JSON servers and clients using only native functions.

Citation preview

PHP and Web Services

Bruno Pedro <bpedro@computer.org>

CITOT - May 20th 2007

PHP and Web Services

Summary

• What is PHP?

• Web Services

• SOAP

• REST

• JSON

• Conclusions

2

PHP and Web 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 and Web Services

How is PHP used?

• Content Management

• Forums

• Blogging

• Wiki

• CRM

4

PHP and Web Services

Who uses PHP?

Source: Zend

5

PHP and Web 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 and Web Services

The PHP language

7

Easy syntax!

PHP and Web Services

Creating a function

8

function declaration

return value

function call

PHP and Web Services

Creating a class

9

class declaration

public method

method call

PHP and Web Services

Web Services

10

Source: The Server Side(http://tinyurl.com/38umxt)

PHP and Web Services

What are Web Services?

• Interoperable interactions

• Calls are made over a network

• Lots of different standards

• Popular Web sites use them

11

PHP and Web Services

Requirements

• PHP 5.2.2

• SOAP

• simplexml

• DOM

• JSON

12

PHP and Web Services

SOAP

Source: Prentice Hall(http://tinyurl.com/33dcch)

13

PHP and Web 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 and Web Services

Server implementation

15

namespace

start a server

assign a class

handle incoming calls

PHP and Web Services

Client implementation

16

namespace

endpoint

display result

PHP and Web Services

Output

17

namespace

result

PHP and Web 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 and Web Services

REST

http://example.com/calculator/sum/?x=121&y=233

GETPUTPOSTDELETE

«interface»

ResourceGET - perform a calculationPUT - not usedPOST - not usedDELETE - not used

/calculator/sum/

19

PHP and Web 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 and Web Services

Server implementation

21

handle incoming call

create anXML document

add elements

output result

PHP and Web Services

Client implementation

22

endpoint

arguments

load XML document

output result

PHP and Web Services

Output

23

result

PHP and Web Services

REST

24

• Interaction needs to be implemented

• XML schema must be know beforehand

• Complex types not handled natively

• Output is usually RSS

PHP and Web Services

JSON

25

JSON LogoSource: json.org

PHP and Web 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 and Web Services

Server implementation

27

handle incoming call

generate result

outputJSON string

PHP and Web Services

Client implementation

28

endpoint

arguments

output result

PHP and Web Services

Output

29

result

PHP and Web 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 and Web 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 and Web Services

Questions?

32

PHP and Web 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/

Recommended