How To Build A Web Service

18
HOW {TO} BUILD A WEB SERVICE 1 Sunday, October 9, 11

description

Covers the concepts on how to build a web service (Rest API), data formats (JSON vs XML), HTTP, troubleshooting your API, tools and how to bring it all together in PHP.

Transcript of How To Build A Web Service

Page 1: How To Build A Web Service

HOW {TO}BUILD A WEB SERVICE

1

Sunday, October 9, 11

Page 2: How To Build A Web Service

OVERVIEWI. REST API

II. DATA FORMATS - (JSON vs XML)

III. Request

IV. Response

2

Sunday, October 9, 11

Page 3: How To Build A Web Service

NEED TO KNOWI. HTTP

I. Methods

II. Headers

III. Status Codes

3

Sunday, October 9, 11

Page 4: How To Build A Web Service

HOW TOI. Troubleshooting

II. Tools

III. PHP Frameworks

4

Sunday, October 9, 11

Page 5: How To Build A Web Service

REST

5

/*** Representational State Transfer*/

Sunday, October 9, 11

Page 6: How To Build A Web Service

6

/*** Representational State Transfer*/

- use HTTP methods to determine action.

- use HTTP headers identify specifics of the request and how to handle the response

Sunday, October 9, 11

Page 7: How To Build A Web Service

7

/*** USEFUL HTTP METHODS*/

- GET: retrieve resource representation (read*)

- POST: create* new resource

- PUT: update* resource

- DELETE*

Sunday, October 9, 11

Page 8: How To Build A Web Service

8

/*** USEFUL HTTP HEADERS*/

- Accept and Content-Type: used for content format negotiation

- User-Agent: Identify what made the request

Sunday, October 9, 11

Page 9: How To Build A Web Service

9

/*** HTTP STATUS CODES*/

- 200: OK

- 301: Moved

- 302: Found

- 401: Not Authorized

- 403: Forbidden

- 404: Not Found

- 500: Server Error

- Custom *

Sunday, October 9, 11

Page 10: How To Build A Web Service

DATA FORMATS

10

/** * JSON vs XML */

Sunday, October 9, 11

Page 11: How To Build A Web Service

11

/*** JSON: JavaScript Object Notation*/

Pros

- Comes standard in PHP

- Light

- Great for devices

Sunday, October 9, 11

Page 12: How To Build A Web Service

12

/*** JSON: JavaScript Object Notation*/

Cons

- Hard to distinguish Object vs Array in php

- Not as descriptive

Sunday, October 9, 11

Page 13: How To Build A Web Service

13

/** * XML*/

Pros

- Verbose and Precise

- Great for Machine to Machine

- Inherent style depending on the client.

Sunday, October 9, 11

Page 14: How To Build A Web Service

14

/*** XML*/

Cons

- Heavy

- Multiple PHP libraries / parsers

- Multiple standard formats (plain, rss, atom etc)

Sunday, October 9, 11

Page 15: How To Build A Web Service

15

/** * Request and Respond */

- Resource are always in pluralhttp://api.local.ws/users/1

- When no identifier is present, the collection is the resourcehttp://api.local.ws/users

- Respond with appropriate status code

Sunday, October 9, 11

Page 16: How To Build A Web Service

16

/** * Tools */

- Apigee

- YQL

- Charles

- Browser (GET, POST)

- Packet Sniffers

- PHP (curl,file_get_contents)

- any http client*

- RESTClient

Sunday, October 9, 11

Page 17: How To Build A Web Service

DO IT17

Sunday, October 9, 11