Vietnam mobile day 2012 trends in mobile marketing - fifth i media
Php day 2011 cnc ph ptrongphattrien mobile
1. PHP TRONG PHÁT TRIỂN
MOBILE WEB SERVICE
Hanoi – PHP Day 2011
Ngô Xuân Lộc
CNC Mobile - Android Technical Leader
2. 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
3. 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 -
4.
5. CÁC CHUẨN WEB SERVICES
XML – RPC (remote procedure call)
SOAP (Simple Object Access Protocol)
RESTful (Representational State Transfer)
6. 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
7. 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
8. RESTful
Giới thiệu lần đầu vào năm 2000
Là 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
9.
10. So sánh tốc độ Download và thực thi Web Service theo hai
chuẩn SOAP và RESTful (login api)
11. 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=email@mail.com
[GET]
server/api/user/userdetail?name=username&email=email2@mail.com
[GET/POST]
server/api/user/updateuser?name=username&email=email2@mail.com
[GET] server/api/user/deleteuser?name=username
12. THIẾT KẾ WS THEO RESTful
Các phương thức truy xuất dữ liệu
13. 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 }
14. 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
15. 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"}
16. DELETE
DELETE to /api/apids/<apid>
HOST : https://go.urbanairship.com
Content-Type: application/json
17. 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)