Upload
maria-bong
View
218
Download
3
Embed Size (px)
DESCRIPTION
Web API PHP
Citation preview
LOGO
ng dng phn tn Xy dng UD Web
Web API
GV: Nguyn Trng Sn
Ni dung
Gii thiu Cc loi Web API:
RESTful API SOAP API
Lp trnh Web API: RESTful API SOAP API
JSON, JSON & PHP, JSON & Javascript, 2
Gii thiu
Web API l g ? Cc chc nng/phng thc c thc hin qua internet WEB API = WEB SERVICE
c im: S dng giao thc HTTP S dng mt s ngn ng nh du m t ni dung d liu trao
i cng nh cc thng ip truyn i: JSON, XML. c nh danh thng qua URL (Uniform Resource Locason) c lp nn tng, ngn ng lp trnh
ng dng: L mt k thut xy dng ng dng phn tn: cc ng dng trn
mng internet c th trao i thng sn (tng tc) vi nhau. Hoc mt ng dng c tng hp t cc Web API: mashup
C ch hot ng ca Web API
HTTP Request Message
HTTP Response Message
Client Server HTTP GET message
HTTP POST message
HTTP POST & HTTP GET
GET /path/script.php?a=1&b=2 HTTP/1.0
POST /path/sum.php HTTP/1.0 From: [email protected] User-Agent: HTTPTool/1.0 Content-Type: applicason/x-www-form-urlencoded Content-Length: 7 a=1&b=2
2 loi Web API: RESTful Web Service
RESTful Web Service: S dng cc ngn ng nh du (JSON, XML) m t kiu d liu,
cc i tng truyn nhn. Tuy nhin Khng c giao thc chun. Khng c ti liu m t web Web API.
S phn phi v s dng cc Resul web service ph thuc vo cc ti liu hng dn ca t chc (nh cung cp web service).
S dng n gin: Khng cn th vin, cng c. Ch s dng giao thc truyn d liu bnh thng ging nh 1 trang web (HTTP).
2 loi Web API: SOAP-base Web Service
SOAP-base Web Service: S dng HTTP truyn v nhn cc thng ip S dng giao thc SOAP m t thng ip, kiu d liu truyn
nhn: SOAP request, SOAP response C ti liu m t cc Web Serivce (Cc phng thc, kiu d liu,
tham s, ..) S dng cng c/ th vin gi thc hin. Hoc gi thc hin theo kiu ca RESTful Service nhng phc tp.
RESTful v SOAP-base Web Service
The main advantages of REST web services are: Kch thc nh (c th s dng JSON thay v XML) Human Readable Results D to, d s dng, khng cn cng c i km
SOAP also has some advantages: D s dng c t cht ch: Kiu d liu, M t web service
SOAP-base Web Service: SOAP
What is SOAP? SOAP stands for Simple Object Access Protocol SOAP is a communica_on protocol SOAP is for communica_on between applica_ons SOAP is a format for sending messages SOAP communicates via Internet SOAP is plaaorm independent SOAP is language independent SOAP is based on XML SOAP is simple and extensible SOAP allows you to get around rewalls SOAP is a W3C recommendason
SOAP: SOAP Request
SOAP Request & SOAP Response l nhng thng ip da vo HTTP POST
SOAP: SOAP Response
SOAP-base Web Service: WSDL
WSDL: WSDL: Web Service Descripson Language M t thng sn v webservice:
a ch Cc kiu d liu Cc phng thc: tn, danh sch tham s
c vit bng ngn ng XML
LOGO
Lp trnh SOAP-base Web Service vi
- php_soap - nuSOAP
Lp trnh WS vi php_soap extension
S dng php_soap extension To web service
Lp SoapServer addFuncson Handle
S dng web service Lp SoapClient
__call __soapCall SoapParam
c im: Khng c WSDL
client
server
Lp trnh WS vi php_soap extension
V D 1: XY DNG 2 HM WEBSERVICE echoo v add
client
server
Lp trnh WS vi php_soap extension
V D 1: XY DNG CLIENT S DNG 2 HM WEBSERVICE echoo v add
Lp trnh WS vi nuSOAP
S dng th vin nuSOAP To Web service:
Lp soap_server congureWSDL register service
S dng web service Lp nusoap_client
nusoap_client call
C to ra ti liu WSDL
new confureWSDL register service
new call
client
server
Xy dng Web service vi NuSOAP
V D 2: XY DNG HM WEBSERVICE Hello v Sum S DNG nuSOAP
Lp trnh WS vi nuSOAP To WS
Kt qu thc thi chng trnh: V D 2: XY DNG WEBSERVICE Hello v Sum S DNG nuSOAP
WSDL V D 2: XY DNG WEBSERVICE Hello v Sum S DNG nuSOAP
Client
callHello.php
callSum.php
V D 2: XY DNG CLIENT S DNG HM WEBSERVICE Hello v Sum
V D 3: XY DNG HM WEBSERVICE Search DNG nuSOAP thc hin m kim trn CSDL
WSDL V D 3: XY DNG HM WEBSERVICE Search DNG nuSOAP thc hin m kim trn CSDL
V D 3 V D 3: XY DNG CLIENT (callSearch.php)S DNG HM WEBSERVICE Search
LOGO
Lp trnh RESTful Web Service vi
- php_curl - json
php_curl
CURL: Th vin cho php to v gi thc hin nhng HTTP Request: Ly ni
dung trang web, gi thc hin nhng Web API Enable PHP Extension: php_curl
HTTP Request Message
HTTP Response Message
Client Server
V d: Ly ni dung trang web bng CURL
JSON
JSON = JavaScript Object Notason Mt s c im:
Ngn ng trao i thng sn: JSON l mt ngn ng nh du s dng truyn nhn cc i tng trn mi trng internet.
nh dng vn bn c lp ngn ng T m t
So snh JSON v XML u l ngn ng trao i thng sn vn bn. Tp sn JSON c kch thc nh hn XML, Parser nhanh hn v d hn JSON c ch hp sn trong ngn ng Javascript, c th khai bo cc
i tng Javascript s dng JSON.
JSON & XML
Much Like XML JSON is plain text JSON is "self-describing" (human readable) JSON is hierarchical (values within values) JSON can be parsed by JavaScript JSON data can be transported using AJAX
Much Unlike XML No end tag Shorter Quicker to read and write Can be parsed using built-in JavaScript eval() Uses arrays No reserved words
Why JSON ?
For AJAX applicasons, JSON is faster and easier than XML: Using XML
Fetch an XML document Use the XML DOM to loop through the document Extract values and store in variables
Using JSON Fetch a JSON string eval() the JSON string
C php JSON
C php ngha name=value M t d liu
v name: t trong cp du " " v value:
S Chui (t trong cp du " ") Nh phn (true /false) Mng (t trong cp du [ ]) i tng (t trong cp du { }) Null
, Phn tch d liu v cc thnh phn ca mng {} nh ngha mt i tng JSON [] nh ngha mng cc gi tr hoc i tng JSON
C php JSON
V d 1: JSON object { "rstName":"John" , "lastName":"Doe" }
V d 2: JSON array { "employees": [
{ "rstName":"John" , "lastName":"Doe" } , { "rstName":"Anna" , "lastName":"Smith" } , { "rstName":"Peter" , "lastName":"Jones" } ]
}
V d 3: {
"name": { "rst":"John", "last":"Adams"} , "age":"40"
}
C php JSON
V d 4: {"person":[
{"name":{"rst":"John","last":"Adams"}, "age":"40"
}, {"name":{"rst":"Thomas","last":"Jeerson"}, "age":"35" } ]
}
JSON Parser - PHP
S dng json_decode chuyn mt chui JSON sang i tng JSON
JSON Parser - PHP
JSON Parser - PHP
JSON Parser - PHP
S dng json_encode() chuyn mt i tng sang mt chui JSON
M ngun:
Kt qu: {"a":1,"b":2,"c":3,"d":4,"e":5}
VD Xy dng REST API vi PHP & JSON
M ngun: JSON_ListOfNhanVien.php
Web API Server
VD Xy dng REST API vi PHP & JSON
hp://localhost/DemoWebAPI/JSON_Database/00JSONAPI/JSON_ListOfNhanVien.php
VD S dng REST API vi PHP & JSON
M ngun: JSON_ViewListOfNhanVien.php
VD S dng Twier Search REST API
Tm kim vi t kha hello, ly 100 kt qu u sn Twier API: hp://search.twier.com/search.json?q=hello&rpp=100 Kt qu:
V d S dng Twier Search REST API
JSON Parser - JavaScript
V d 1: Khai bo JSON array v truy xut
JSON Parser - JavaScript
V d 2: To JSON object t JSON string Hm eval: Chuyn 1 JSON String JSON object
JSON Parser - JavaScript
V d 3: Truy xut cc thnh phn ca Json Object
JSON Parser - JavaScript
V d 4: Truy xut cc thnh phn ca Json Object
JSON Parser - JavaScript
V d 5: Convert JSON object to String
S dng AJAX & JSON
funcson showResult() { if(xmlHp.readyState == 4 && xmlHp.status == 200) { var kq = xmlHp.responseText; var jSONObject = eval("(" + kq + ")"); document.getElementById("MaNV").innerHTML = jSONObject.MaNV; document.getElementById("HoTen").innerHTML = jSONObject.HoTen; document.getElementById("NgaySinh").innerHTML = jSONObject.NgaySinh; document.getElementById("DiaChi").innerHTML = jSONObject.DiaChi; document.getElementById("Phai").innerHTML = jSONObject.Phai; document.getElementById("Luong").innerHTML = jSONObject.Luong; document.getElementById("Phong").innerHTML = jSONObject.Phong;
} }
Tm tt
2 loi Web API SOAP-base Web service RESTfull Web service
2 cch s dng: Ti WEB Server
Ti Client (Trnh duyt): AJAX
Ti liu tham kho
Pro PHP XML and Web Services
RESTful Web Services
www.google.com
LOGO
Bi tp
Bi tp: SearchPortal
WSCompanyA
Webservice: Search (string keyword)
WSCompanyB Webservice: DataSet Search (string keyword)
WSCompanyDBA
WSCompanyDBB
SearchPortal
Index.php
.
.
. WSCompanyN
Webservice: DataSet Search (string keyword)
WSCompanyDBN
Default: Tm kim nhn vin trn cc h thng con WSCompanyX v hin th kt qu m kim tng hp Danh sch cc WS h thng con c lu trong CSDL
SearchPortalDB
ID WSURL COMPANY
A
B
MANV HoTen
Bi tp: SearchPortal
SearchPortal
T kha:
Kt qu:
MANV H TN COMPANY
A
B