A Web Service Platform for Building InteroperableA Web Service
08 - Web Service
-
Upload
tran-thanh -
Category
Documents
-
view
36 -
download
0
Transcript of 08 - Web Service
1
Lê Đình ThanhBộ môn Mạng và Truyền thông Máy tính
Khoa Công nghệ Thông tinTrường Đại học Công nghệ, ĐHQGHN
E-mail: [email protected], [email protected]: 0987.257.504
Bài giảng
LẬP TRÌNH WEB
2
Web service
Bài 8
Lê Đình Thanh, Bài giảng Lập trình web.
3
Nội dung
• Web service (WS) là gì?
• Vì sao dùng WS?
• Tạo WS
• Triệu gọi phương thức của WS
– Sử dụng PHP
– Sử dụng AJAX
– Sử dụng jQuery
Lê Đình Thanh, Bài giảng Lập trình web.
Web service (WS)
• WS là thành phần phần mềm cung cấp các phương thức(hàm) có thể triệu gọi qua mạng– Tài liệu WSDL (Web Services Description Language) xác định các
phương thức mà WS cung cấp– Các hệ thống khác triệu gọi phương thức của WS bằng cách gửi
cho WS thông báo WS Request và nhận kết quả (giá trị trả vềcủa phương thức) trong thông báo WS Response
Lê Đình Thanh, Bài giảng Lập trình web.
f(x)g(x, y)
h(z)…
…v = f(10)
…
Web ServiceApplication
WS Request
WS Response
Vì sao dùng web service
• Cho phép tích hợp qua mạng giữa các ứng dụngđược viết bằng các ngôn ngữ khác nhau trên cácnền khác nhau– Tích hợp trong cùng host (thường yêu cầu cùng nền,
cùng ngôn ngữ lập trình)• Bao hàm tệp mã nguồn: .h, .inc, …
• Thư viện liên kết tĩnh: .lib
• Thư viện liên kết động: .dll
– Tích hợp qua mạng• Remote Procedure Call (RPC)
• Web service
Lê Đình Thanh, Bài giảng Lập trình web.
WSDL
• Tài liệu WSDL (Web Service Definition Language) được sử dụng để xác định cácphương thức mà WS cung cấp, có thể triệu gọiqua mạng
• Mỗi WS cần có một tài liệu WSDL để mô tả vềnó
Lê Đình Thanh, Bài giảng Lập trình web.
Triệu gọi phương thức của WS bằngSOAP
• SOAP (Simple Object Access Protocol) là giao thứcđược sử dụng nhiều nhất để triệu gọi phương thức củaWS– Ngoài ra có thể dùng XML+RPC, REST+JSON, …
• SOAP = HTTP + XML (thông báo SOAP là thông báoHTTP với nội dung là tài liệu XML có cấu trúc đượcSOAP quy định)
Lê Đình Thanh, Bài giảng Lập trình web.
f(x)g(x, y)
h(z)…
…r = f(10)
…
SOAP WSApplication=SOAP client
SOAP WS request
SOAP WS response
Thông báo SOAP WS Request
• POST /labs/webapp-development/ws/name-service.phpHTTP/1.0
• Host: localhost• Content-Type: text/xml; charset=ISO-8859-1 • Content-Length: 526
• <?xml version="1.0" encoding="ISO-8859-1"?>• <SOAP-ENV:Envelope SOAP-
ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns2820:normalizeName xmlns:ns2820="http://tempuri.org"><namexsi:type="xsd:string"> hoang THu</name></ns2820:normalizeName></SOAP-ENV:Body></SOAP-ENV:Envelope>
HTTP Header
HTTP Body
Lê Đình Thanh, Bài giảng Lập trình web.
Thông báo SOAP WS Response
• HTTP/1.1 200 OK • Date: Wed, 27 Mar 2013 15:00:17 GMT • Server: Apache/2.2.21 (Win32) • Content-Length: 531 • Connection: close • Content-Type: text/xml; charset=ISO-8859-1
• <?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:normalizeNameResponse xmlns:ns1="http://tempuri.org"><returnxsi:type="xsd:string">Hoang Thu</return></ns1:normalizeNameResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Lê Đình Thanh, Bài giảng Lập trình web.
HTTP Header
HTTP Body
Tạo SOAP WS
• Cho phép mở rộng php_soap
• Tạo thư viện các hàm cho WS
• Tạo tài liệu WSDL mô tả về WS
• Tạo SOAP WS
Lê Đình Thanh, Bài giảng Lập trình web.
Cho phép mở rộng php_soap
• Cấu hình trong php.ini
• extension=php_soap.dll
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo thư viện các hàm cho WS
<?phpfunction normalizeName($name, $type) {
$ss = explode(" ", $name);$ret = "";for ($i = 0; $i < count($ss); $i++) {
if (strcmp($ss[$i], "") != 0) {if (strcmp($ret, "") != 0) $ret .= " ";$ret .= $ss[$i];
}}if ($type == "UCW") return ucwords(strtolower($ret));return ucfirst(strtolower($ret));
}
function ageFromDoB($vd) { //$vd: dd/mm/yyyy$ss = explode("/", $vd); //tach xau$age = intval(date("Y")) - intval($ss[2]);return $age;
}?>
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo tài liệu WSDL mô tả WS (1/5)
<?xml version ="1.0" encoding ="UTF-8" ?>
<definitions name=“myws"
targetNamespace="http://localhost/name"
xmlns:tns="http://localhost/name"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo tài liệu WSDL mô tả WS (2/5)
<message name="normalizeName_Parameters">
<part name="name" type="xsd:string"/>
<part name="type" type="xsd:string"/>
</message>
<message name="normalizeName_Return">
<part name="normalizedName" type="xsd:string"/>
</message>
<message name="ageFromDoB_Parameters">
<part name="vndate" type="xsd:string"/>
</message>
<message name="ageFromDoB_Return">
<part name="age" type="xsd:string"/>
</message>
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo tài liệu WSDL mô tả WS (3/5)
<portType name="myport">
<operation name="normalizeName">
<input message="tns:normalizeName_Parameters"/>
<output message="tns:normalizeName_Return"/>
</operation>
<operation name="ageFromDoB">
<input message="tns:ageFromDoB_Parameters"/>
<output message="tns:ageFromDoB_Return"/>
</operation>
</portType>
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo tài liệu WSDL mô tả WS (4/5)
<binding name="mybinding" type="tns:myport"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="normalizeName">
<soap:operation /> <input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input> <output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output> </operation> <operation name="ageFromDoB">
<soap:operation /> <input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input> <output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output> </operation>
</binding>
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo tài liệu WSDL mô tả WS (5/5)
<service name="myservice">
<port binding="mybinding">
<soap:address location="http://localhost/labs/webapp-development/ws/mywebservice.php"/>
</port>
</service>
</definitions>
Lê Đình Thanh, Bài giảng Lập trình web.
Tạo SOAP WS
<?phprequire_once("lib.php");
ini_set("soap.wsdl_cache_enabled", "0"); $server = new SoapServer("mywebservice.wsdl"); $server->addFunction("normalizeName"); $server->addFunction("ageFromDoB"); $server->handle();
?>
Lê Đình Thanh, Bài giảng Lập trình web.
Xem WSDL của WS
Lê Đình Thanh, Bài giảng Lập trình web.
Triệu gọi phương thức của WS bằngPHP
<?php
$client = new SoapClient("mywebservice.wsdl", array('trace' => 1));
$response = $client->normalizeName(" trịnh tố nHư ", "UCW");
if (is_soap_fault($response)) {
echo "<pre>".$response->faultcode."</pre>";
echo "<pre>".$response->faultstring."</pre>";
} else echo $response;
$r = $client->ageFromDoB("23/5/1992");
if (is_soap_fault($r)) {
echo "<pre>".$r->faultcode."</pre>";
echo "<pre>".$r->faultstring."</pre>";
} else echo " $r";
?>
Lê Đình Thanh, Bài giảng Lập trình web.
Triệu gọi phương thức của WS bằngPHP
• Xem SOAP WS Request vừa gửi
echo $client->__getLastRequestHeaders();
echo htmlentities($client->__getLastRequest());
• Xem SOAP WS Response vừa nhận
echo $client->__getLastResponseHeaders();
echo htmlentities($client->__getLastResponse());
Lê Đình Thanh, Bài giảng Lập trình web.
Triệu gọi phương thức của WS bằngAJAX
var httpRequest = getAjaxEngine();
var url = “url-of-web-service”;
var soapMessage = '<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope … </SOAP-ENV:Envelope> ';
httpRequest.open("POST", url, true);
if (httpRequest.overrideMimeType) { httpRequest.overrideMimeType("text/xml"); }
httpRequest.setRequestHeader("Man", url + " HTTP/1.1")
httpRequest.setRequestHeader("MessageType", "CALL");
httpRequest.setRequestHeader("Content-Type", "text/xml");
httpRequest.onreadystatechange= function() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
document.write( httpRequest.responseText);
}
}
};
httpRequest.send(soapMessage);
Lê Đình Thanh, Bài giảng Lập trình web.
Triệu gọi phương thức của WS bằngjQuery
• Sử dụng thư viện jQuerySOAP tạihttps://github.com/zachofalltrades/jquery.soap
$.soap({
url: 'http://localhost/labs/webapp-development/ws/mywebservice.php/',
method: 'normalizeName',
params: { name: ' hoàng tHANH tÙNG ', type: 'UCW’ },
returnJson: false,
success: function (data) {
//data là thân của SOAP response.
},
error: function (string) { document.write("<pre>"+string+"</pre>"); }
});
Lê Đình Thanh, Bài giảng Lập trình web.
Triệu gọi phương thức của WS bằngjQuery
• Phân tích SOAP Response
• success: function (data) {
• var xmlDoc = null;
• if (window.DOMParser) {
• xmlDoc = new DOMParser().parseFromString(data,"text/xml");
• } else {
• xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
• xmlDoc.async = false;
• xmlDoc.loadXML(data);
• }
•
• if (xmlDoc != null) {
• document.write(xmlDoc.getElementsByTagName("normalizedName")[0].childNodes[0].nodeValue);
• }
• document.write("<br><textarea>"+data+"</textarea>");
• }
Lê Đình Thanh, Bài giảng Lập trình web.
NuSOAP
• Là SOAP toolkit dùng cho phát triển SOAP server và SOAP client
• Có thể được sử dụng để phát triển SOAP WS.
Lê Đình Thanh, Bài giảng Lập trình web.
Tựhọc
Công bố và sử dụng WSKiến trúc WS
Nguồn: http://en.wikipedia.org/wiki/Web_service
Nhà cung cấp WS cần quảng cáo vềWS của mình chokhách hàng biết
Lê Đình Thanh, Bài giảng Lập trình web.
Nhà môi giới tạodanh bạ WS
UDDI (Universal Description, Discovery and Integration)
Khách hàng cần sửdụng WS tìm WS trên danh bạ
Ví dụ:http://www.xmethods.net
Hết
Lê Đình Thanh, Bài giảng Lập trình web.