You are on page 1of 27

Bi ging

LP TRNH WEB
L nh Thanh B mn Mng v Truyn thng My tnh Khoa Cng ngh Thng tin Trng i hc Cng ngh, HQGHN E-mail: thanhld@vnu.edu.vn, thanhld.vnuh@gmail.com Mobile: 0987.257.504

Bi 8

Web service

L nh Thanh, Bi ging Lp trnh web.

Ni dung

Web service (WS) l g? V sao dng WS? To WS Triu gi phng thc ca WS


S dng PHP S dng AJAX S dng jQuery

L nh Thanh, Bi ging Lp trnh web.

Web service (WS)


WS l thnh phn phn mm cung cp cc phng thc (hm) c th triu gi qua mng
Ti liu WSDL (Web Services Description Language) xc nh cc phng thc m WS cung cp Cc h thng khc triu gi phng thc ca WS bng cch gi cho WS thng bo WS Request v nhn kt qu (gi tr tr v ca phng thc) trong thng bo WS Response
WS Request

v = f(10)

WS Response

f(x) g(x, y) h(z)

Application

Web Service

L nh Thanh, Bi ging Lp trnh web.

V sao dng web service


Cho php tch hp qua mng gia cc ng dng c vit bng cc ngn ng khc nhau trn cc nn khc nhau
Tch hp trong cng host (thng yu cu cng nn, cng ngn ng lp trnh)
Bao hm tp m ngun: .h, .inc, Th vin lin kt tnh: .lib Th vin lin kt ng: .dll

Tch hp qua mng


Remote Procedure Call (RPC) Web service
L nh Thanh, Bi ging Lp trnh web.

WSDL
Ti liu WSDL (Web Service Definition Language) c s dng xc nh cc phng thc m WS cung cp, c th triu gi qua mng Mi WS cn c mt ti liu WSDL m t v n

L nh Thanh, Bi ging Lp trnh web.

Triu gi phng thc ca WS bng SOAP


SOAP (Simple Object Access Protocol) l giao thc c s dng nhiu nht triu gi phng thc ca WS
Ngoi ra c th dng XML+RPC, REST+JSON,

SOAP = HTTP + XML (thng bo SOAP l thng bo HTTP vi ni dung l ti liu XML c cu trc c SOAP quy nh)
r = f(10) SOAP WS request f(x) g(x, y) h(z)

SOAP WS response

Application = SOAP client


L nh Thanh, Bi ging Lp trnh web.

SOAP WS

Thng bo SOAP WS Request


POST /labs/webapp-development/ws/name-service.php HTTP/1.0 Host: localhost Content-Type: text/xml; charset=ISO-8859-1 Content-Length: 526

HTTP Header HTTP Body

<?xml version="1.0" encoding="ISO-8859-1"?> <SOAP-ENV:Envelope SOAPENV:encodingStyle="http://schemas.xmlsoap.org/soap/e ncoding/" xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/"><SO AP-ENV:Body><ns2820:normalizeName xmlns:ns2820="http://tempuri.org"><name xsi:type="xsd:string"> hoang THu </name></ns2820:normalizeName></SOAPENV:Body></SOAP-ENV:Envelope>

L nh Thanh, Bi ging Lp trnh web.

Thng bo 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"?><SOAPENV:Envelope SOAPENV:encodingStyle="http://schemas.xmlsoap.org/soap/ encoding/" xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/"><S OAP-ENV:Body><ns1:normalizeNameResponse xmlns:ns1="http://tempuri.org"><return xsi:type="xsd:string">Hoang Thu</return></ns1:normalizeNameResponse></SOAPENV:Body></SOAP-ENV:Envelope>

HTTP Header HTTP Body

L nh Thanh, Bi ging Lp trnh web.

To SOAP WS
Cho php m rng php_soap To th vin cc hm cho WS To ti liu WSDL m t v WS To SOAP WS

L nh Thanh, Bi ging Lp trnh web.

Cho php m rng php_soap


Cu hnh trong php.ini
extension=php_soap.dll

L nh Thanh, Bi ging Lp trnh web.

To th vin cc hm cho WS
<?php function 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, Bi ging Lp trnh web.

To ti liu 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, Bi ging Lp trnh web.

To ti liu 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, Bi ging Lp trnh web.

To ti liu 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, Bi ging Lp trnh web.

To ti liu 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, Bi ging Lp trnh web.

To ti liu WSDL m t WS (5/5)


<service name="myservice"> <port binding="mybinding"> <soap:address location="http://localhost/labs/webappdevelopment/ws/mywebservice.php"/> </port> </service> </definitions>

L nh Thanh, Bi ging Lp trnh web.

To SOAP WS
<?php require_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, Bi ging Lp trnh web.

Xem WSDL ca WS

L nh Thanh, Bi ging Lp trnh web.

Triu gi phng thc ca WS bng PHP


<?php $client = new SoapClient("mywebservice.wsdl", array('trace' => 1)); $response = $client->normalizeName(" trnh 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, Bi ging Lp trnh web.

Triu gi phng thc ca WS bng PHP


Xem SOAP WS Request va gi
echo $client->__getLastRequestHeaders(); echo htmlentities($client->__getLastRequest());

Xem SOAP WS Response va nhn


echo $client->__getLastResponseHeaders(); echo htmlentities($client->__getLastResponse());

L nh Thanh, Bi ging Lp trnh web.

Triu gi phng thc ca WS bng AJAX


var httpRequest = getAjaxEngine(); var url = url-of-web-service; var soapMessage = '<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope </SOAPENV: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, Bi ging Lp trnh web.

Triu gi phng thc ca WS bng jQuery


S dng th vin jQuerySOAP ti https://github.com/zachofalltrades/jquery.soap $.soap({ url: 'http://localhost/labs/webapp-development/ws/mywebservice.php/', method: 'normalizeName', params: { name: ' hong tHANH tNG ', type: 'UCW }, returnJson: false, success: function (data) { //data l thn ca SOAP response. }, error: function (string) { document.write("<pre>"+string+"</pre>"); } });
L nh Thanh, Bi ging Lp trnh web.

Triu gi phng thc ca WS bng jQuery


Phn tch 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, Bi ging Lp trnh web.

NuSOAP
L SOAP toolkit dng cho pht trin SOAP server v SOAP client C th c s dng pht trin SOAP WS.

T hc
L nh Thanh, Bi ging Lp trnh web.

Cng b v s dng WS Kin trc WS


V d: http://www.xmethods.net

Nh mi gii to danh b WS
UDDI (Universal Description, Discovery and Integration)

Khch hng cn s dng WS tm WS trn danh b

Nh cung cp WS cn qung co v WS ca mnh cho khch hng bit Ngun: http://en.wikipedia.org/wiki/Web_service

L nh Thanh, Bi ging Lp trnh web.

Ht

L nh Thanh, Bi ging Lp trnh web.

You might also like