Php day 2011 cnc ph ptrongphattrien mobile

23
PHP TRONG PHÁT TRIỂN MOBILE WEB SERVICE Hanoi – PHP Day 2011 Ngô Xuân Lộc CNC Mobile - Android Technical Leader

Transcript of Php day 2011 cnc ph ptrongphattrien mobile

Page 1: Php day 2011 cnc ph ptrongphattrien mobile

PHP TRONG PHÁT TRIỂN MOBILE WEB SERVICE

Hanoi – PHP Day 2011

Ngô Xuân Lộc

CNC Mobile - Android Technical Leader

Page 2: Php day 2011 cnc ph ptrongphattrien mobile

NỘI DUNG

Web Services? 

Các chuẩn Web Services

Nguyên tắc thiết kế Web Services theo chuẩn RESTful

Tạo một Web Service đơn giản theo chuẩn RESTful bằng

PHP

Page 3: Php day 2011 cnc ph ptrongphattrien mobile

WEB SERVICES ?

“Web Services are frequently just Web APIs that can be accessed over a network, such as the Internet, and executed on a remote system hosting the requested services” - WikiPedia -

Page 4: Php day 2011 cnc ph ptrongphattrien mobile
Page 5: Php day 2011 cnc ph ptrongphattrien mobile

CÁC CHUẨN WEB SERVICES

XML – RPC (remote procedure call)

SOAP (Simple Object Access Protocol)

RESTful (Representational State Transfer)

Page 6: Php day 2011 cnc ph ptrongphattrien mobile

CÁC CHUẨN WEB SERVICES

XML – RPC

Ra đời vào năm 1998

là một giao thức gọi thủ tục từ xa (RPC) trong đó sử

dụng XML để mã hóa dữ liệu và sủ dụng các giao thức

HTTP để truyền tải

Page 7: Php day 2011 cnc ph ptrongphattrien mobile

SOAP

Có thể coi là một protocol giống như HTTP-Get, và

HTTP-Post

SOAP dựa trên nền tảng XML.

SOAP là một chuẩn Web Services dựa trên chức

năng

Một application có thể request một dịch vụ (web

service) thông qua các SOAP envelop và nhận kết

quả cũng thông qua SOAP envelop

Page 8: Php day 2011 cnc ph ptrongphattrien mobile

RESTful

Giới thiệu lần đầu vào năm 2000Là một chuẩn Web Service hướng đến tài nguyên và dễ dàng sử dụng để thực thi và cung cấp dịch vụSử dụng các phương thức HTTP

Page 9: Php day 2011 cnc ph ptrongphattrien mobile
Page 10: Php day 2011 cnc ph ptrongphattrien mobile

So sánh tốc độ Download và thực thi Web Service theo hai chuẩn SOAP và RESTful (login api)

Page 11: Php day 2011 cnc ph ptrongphattrien mobile

THIẾT KẾ WS THEO RESTful

Phương pháp truy xuất chỉ dựa trên POST, GET

[GET/POST] server/api/user/adduser?

name=username&[email protected]

[GET] server/api/user/userdetail?

name=username&[email protected]

[GET/POST] server/api/user/updateuser?

name=username&[email protected]

[GET] server/api/user/deleteuser?name=username

Page 12: Php day 2011 cnc ph ptrongphattrien mobile

THIẾT KẾ WS THEO RESTful

Các phương thức truy xuất dữ liệu

Page 13: Php day 2011 cnc ph ptrongphattrien mobile

POST

 POST /api/feeds/ HTTP 1.1

HOST : https://go.urbanairship.com

Content-Type: application/json

{ "feed_url": "http://example.com/atom.xml", "template": { "aps":

{ "badge": 1, "sound": "cat.caf", "alert": "New item from some

place! {{ title }}" } }, "broadcast": true }

Page 14: Php day 2011 cnc ph ptrongphattrien mobile

GET

GET /api/feeds/ HTTP 1.1

HOST : https://go.urbanairship.com

Content-Type: application/json

GET /api/push/stats/?start=<timestamp>&end=<timestamp>

HTTP 1.1

HOST : https://go.urbanairship.com

Content-Type: application/json

Page 15: Php day 2011 cnc ph ptrongphattrien mobile

PUT

PUT /api/apids/<apid> HTTP/1.1

Host: https://go.urbanairship.com

Content-Type: application/json

{ "alias": "example_alias", "tags": ["tag1", "tag2"] }

PUT /api/device_tokens/<token>/ HTTP 1.1

HOST : https://go.urbanairship.com

Content-Type: application/json

{"alias": "myalias"}

Page 16: Php day 2011 cnc ph ptrongphattrien mobile

DELETE

DELETE to /api/apids/<apid>

HOST : https://go.urbanairship.com

Content-Type: application/json

Page 17: Php day 2011 cnc ph ptrongphattrien mobile

RESTful TRONG PHP

Các thành phần chính trong xây dựng RESTful web service

The URL (required)

The Method – GET, POST, PUT, or DELETE (required)

The Agruments  (required)

Credentials (opt)

The Response Body 

The Response Status Code

Other Response Info (opt)

Page 18: Php day 2011 cnc ph ptrongphattrien mobile
Page 19: Php day 2011 cnc ph ptrongphattrien mobile
Page 20: Php day 2011 cnc ph ptrongphattrien mobile
Page 21: Php day 2011 cnc ph ptrongphattrien mobile
Page 23: Php day 2011 cnc ph ptrongphattrien mobile

THANKS YOU

Q & A