php and web services

33
PHP and Web Services Bruno Pedro <[email protected] > CITOT - May 20 th 2007

Upload: bruno-pedro

Post on 05-Dec-2014

44.728 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: PHP and Web Services

PHP and Web Services

Bruno Pedro <[email protected]>

CITOT - May 20th 2007

Page 2: PHP and Web Services

PHP and Web Services

Summary

• What is PHP?

• Web Services

• SOAP

• REST

• JSON

• Conclusions

2

Page 3: PHP and Web Services

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

Page 4: PHP and Web Services

PHP and Web Services

How is PHP used?

• Content Management

• Forums

• Blogging

• Wiki

• CRM

4

Page 5: PHP and Web Services

PHP and Web Services

Who uses PHP?

Source: Zend

5

Page 6: PHP and Web Services

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

Page 7: PHP and Web Services

PHP and Web Services

The PHP language

7

Easy syntax!

Page 8: PHP and Web Services

PHP and Web Services

Creating a function

8

function declaration

return value

function call

Page 9: PHP and Web Services

PHP and Web Services

Creating a class

9

class declaration

public method

method call

Page 10: PHP and Web Services

PHP and Web Services

Web Services

10

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

Page 11: PHP and Web Services

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

Page 12: PHP and Web Services

PHP and Web Services

Requirements

• PHP 5.2.2

• SOAP

• simplexml

• DOM

• JSON

12

Page 13: PHP and Web Services

PHP and Web Services

SOAP

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

13

Page 14: PHP and Web Services

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

Page 15: PHP and Web Services

PHP and Web Services

Server implementation

15

namespace

start a server

assign a class

handle incoming calls

Page 16: PHP and Web Services

PHP and Web Services

Client implementation

16

namespace

endpoint

display result

Page 17: PHP and Web Services

PHP and Web Services

Output

17

namespace

result

Page 18: PHP and Web Services

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

Page 19: PHP and Web Services

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

Page 20: PHP and Web Services

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

Page 21: PHP and Web Services

PHP and Web Services

Server implementation

21

handle incoming call

create anXML document

add elements

output result

Page 22: PHP and Web Services

PHP and Web Services

Client implementation

22

endpoint

arguments

load XML document

output result

Page 23: PHP and Web Services

PHP and Web Services

Output

23

result

Page 24: PHP and Web Services

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

Page 25: PHP and Web Services

PHP and Web Services

JSON

25

JSON LogoSource: json.org

Page 26: PHP and Web Services

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

Page 27: PHP and Web Services

PHP and Web Services

Server implementation

27

handle incoming call

generate result

outputJSON string

Page 28: PHP and Web Services

PHP and Web Services

Client implementation

28

endpoint

arguments

output result

Page 29: PHP and Web Services

PHP and Web Services

Output

29

result

Page 30: PHP and Web Services

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

Page 31: PHP and Web Services

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

Page 32: PHP and Web Services

PHP and Web Services

Questions?

32

Page 33: PHP and Web Services

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/