php day 2011 cnc ph ptrongphattrien mobile
TRANSCRIPT
PHP TRONG PHÁT TRIỂN MOBILE WEB SERVICE
Hanoi – PHP Day 2011
Ngô Xuân Lộc
CNC Mobile - Android Technical Leader
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
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 -
CÁC CHUẨN WEB SERVICES
XML – RPC (remote procedure call)
SOAP (Simple Object Access Protocol)
RESTful (Representational State Transfer)
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
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
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
So sánh tốc độ Download và thực thi Web Service theo hai chuẩn SOAP và RESTful (login api)
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
THIẾT KẾ WS THEO RESTful
Các phương thức truy xuất dữ liệu
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 }
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
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"}
DELETE
DELETE to /api/apids/<apid>
HOST : https://go.urbanairship.com
Content-Type: application/json
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)
TÀI LIỆU THAM KHẢO
http://blog.garethj.com/2009/02/17/building-a-restful-web-application-with-php
/
http://www.gen-x-design.com/archives/making-restful-requests-in-php/
http://
www.ibm.com/developerworks/vn/edu/ws-understand-web-services1/section2.html
http://www.ibm.com/developerworks/vn/library/ws-restful/
THANKS YOU
Q & A