งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Suranglutch Chaokoksoohng

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Suranglutch Chaokoksoohng"— ใบสำเนางานนำเสนอ:

1 Suranglutch Chaokoksoohng (suranglutch@gmail.com)
3# SOAP Simple Object Access Protocol and 4# Overview Web service by PHP Informatics BUU. Suranglutch Chaokoksoohng

2 Bind | SOAP: Simple Object Access Protocol
Service Registry Provider Requestor Publish Bind Find UDDI SOAP WSDL Bind | SOAP: Simple Object Access Protocol

3 SOAP in a nutshell “SOAP is a protocol specification for invoking methods on servers, services, components and objects. SOAP codifies the existing practice of using XML and HTTP as a method invocation mechanism. The SOAP specification mandates a small number of HTTP headers that facilitate firewall/proxy filtering. The SOAP specification also mandates an XML vocabulary that is used for representing method parameters, return values, and exceptions.” [DevelopMentor] Like XML-RPC, SOAP was developed to be a platform and language independent method of passing information between services, objects, and servers. Provides a simple standardized mechanism for moving structured information.

4 What is SOAP? Protocol ในการสื่อสารกันระหว่าง Application
กำหนดรูปแบบในการส่งผ่านข้อความโดยผ่านเครือข่ายอินเทอร์เน็ต ไม่ขึ้นกับ Platform และภาษาคอมพิวเตอร์ที่พัฒนา มีโครงสร้างแบบ XML SOAP Message เป็นข้อความที่ใช้ส่งผ่านเครือข่ายอินเทอร์เน็ต และเมื่อใช้ร่วมกับเว็บ สามารถส่งผ่าน Firewall ได้ไม่ยาก ได้รับการดูแลโดย W3C *** - เดิมย่อมาจาก Simple Object Access Protocol แต่ถูก drop ไป เพราะไม่เกี่ยวกับ OO บางคนก็ไม่ถือว่าเป็นชื่อย่อ บางคนเรียกว่าเป็น Service-Oriented Access Protocol เพราะนำมาใช้ในโลกของ Web Services - SOAP เป็น XML messaging protocol ที่ lightweight เพราะ เมื่อเทียบกับพวก CORBA, DCOM, RMI จะเบากว่าเพราะไม่มีองค์ประกอบ เช่น การ register และ locate service แต่อาศัยว่าไปพึ่งมาตรฐานอื่น เช่น UDDI (register) และ WSDL (locate) - SOAP ทำ type mapping จาก native language object โดยการ marshall ไปเป็น XML เพื่อจะใส่ใน SOAP message ได้ ปกติ SOAP toolkit จะมี predefined translator สำหรับ primitive types, collections, MIME attachment แต่ถ้าเป็น custom (class) object ก็ต้องสร้าง (class ที่เป็น) serializer และ deserializer เองและไป register ไว้ทั้งฝั่ง client และ server - สามารถใช้ติดต่อระหว่างโปรแกรมคนละภาษาคนละ OS ได้ เช่น Perl บน Solaris คุยกับ Java บน Windows 2000 ได้ และมี SOAP toolkit สำหรับพัฒนา SOAP message ออกมาโดยหลาย vendor เช่น Apache, IBM, Microsoft, BEA, Sun (SAAJ) ภาษาที่ใช้พัฒนาเช่น Java, C++, C#, VB, Perl - SOAP สามารถใช้ได้กับหลาย transport protocol เช่น HTTP, SMTP, FTP, JMS แต่ที่นิยมคือ HTTP - SOAP สามารถผ่าน firewall ได้ ไม่ต้องทำอะไรเพิ่ม ถ้าเป็น protocol เช่น CORBA, DCOM ต้องไปเปิด port บน firewall ดังนั้นในแง่ของ distributed system ซึ่งต้องติดต่อกับ external system นอก firewall จึงสะดวกขึ้น - SOAP ยังมีปัญหาเรื่อง interoperability ระหว่าง SOAP toolkit อยู่ อันเนื่องมาจาก SOAP spec ยังไม่ mature เต็มที่ และก็มีปัญหาเกี่ยวกับพวก guaranteed messaging และ security policy

5 SOAP Messaging Service Application Internet Client Application SOAP
HTTP SOAP Message HTTP SOAP Message Service Application Client Application Internet

6 Where did it come from? Microsoft, UserLand, DevelopMentor, IBM, Ariba, Commerce One, Compaq, HP, IONA, Lotus, SAP ~ Submitted to W3C May 2000. XML based protocol Intended to be a simple wire-protocol for exchange of info. Largest use may be over Internet HTTP.

7 W3C July 9, W3C Releases First Public Working Draft for SOAP Version 1.2 Working draft produced by the XML Protocol Working Group. Specification can be found:

8 Motivation Many Distributed applications communicate using remote procedure calls (RPC) between distributed objects like DCOM and CORBA. HTTP isn’t designed for those objects, so RPC calls aren’t easily adapted to the Internet. Security problems exist for those methods of RPC, so most firewalls and proxy servers are set to block this traffic. HTTP is supported by all Internet browsers and servers, so SOAP presents a nice protocol for doing RPC.

9 SOAP suds SOAP IS: Lightweight communication protocol
For communication between applications one-way, request/response, multicast, etc.. Designed to communicate via HTTP Not tied to any component technology Not tied to any programming language Based on XML Simple and extensible

10 Loose Coupling หลักการออกแบบ Application ที่เน้นการปรับ
เข้ากับสิ่งที่เปลี่ยนแปลงไปได้โดยง่าย (Agility) ลดความขึ้นต่อกัน ระหว่างองค์ประกอบ เพื่อให้มีความคล่องตัว เป็นเป้าหมายของ SOA

11 Loose Coupling in SOA Heterogeneous Technologies // Tightly Coupled
SOA ใช้ Standardized Interface แทนการใช้ Standardized Code อิสระในการพัฒนา Service ด้วยเทคโนโลยีใดก็ได้ ได้ Technology Decoupling โดยยอมเสีย Interface Coupling Data Exchange // all platforms SOA ใช้รูปแบบข้อมูลที่อิสระกับเทคโนโลยี หลีกปัญหา Low-Level Data-Type Compatibility Published Schema // Easy to find all service Service Directory ช่วยในการตกลงเรื่องการให้บริการอย่าง Dynamic Delayed Binding // Can change Version, no new compile – no new application Binding ทำตอนเรียกใช้ Service และยึด Standardized Interface การเปลี่ยนแปลง Service Implementation ไม่กระทบผู้ใช้ แง่ Heterogeneous Technology - Service จะ implement ด้วยภาษาใดบนแพลตฟอร์มใดก็ได้ แต่ในยุคก่อน ซอฟต์แวร์จะติดต่อกันได้ต้องมีอะไรบางอย่างเหมือนกัน จึง tightly coupled กว่า แง่ Data Exchange - การใช้ข้อมูลรูปแบบมาตรฐานที่ไม่ขึ้นกับแพลตฟอร์ม ทำให้ไม่มีปัญหาเรื่อง representation ของข้อมูลที่แลกเปลี่ยน แง่ Published Schema – Service Directory ทำให้ค้นหา Service ที่ต้องการติดต่อได้แบบ dynamic และช่วยให้ศึกษารูปแบบ Interface และรูปแบบข้อมูลเพื่อติดต่อได้อย่างสะดวก แทนการตกลงติดต่อกันแบบ manual แง่ Delayed Binding – การเปลี่ยนแปลง Service Implementation หรือเปลี่ยนเวอร์ชันไม่ได้ต้องการ Application ให้ compile ใหม่ เพราะ bind เมื่อเรียกใช้

12 General (Basic) Structure SOAP Message
Envelope Defines the content of the message Header (optional) contains header information Body contains call and response information

13 SOAP Message Structure
SOAP Envelope SOAP Message SOAP Header SOAP Body XML Message XML Message (RPC or Doc) SOAP สามารถ extend ได้โดยเพิ่มข้อมูลในส่วน Header เช่น ข้อมูลเพื่อทำ authentication ข้อมูลบัญชีเพื่อให้หักเงิน pay-per-use ข้อมูล context สำหรับใช้ปรับการให้บริการ etc. SOAP Message ≠ XML Message

14 SOAP Messaging Service Application Internet Client Application SOAP
HTTP SOAP Message HTTP SOAP Message Service Application Client Application Internet

15 SOAP with Attachment (SwA)
SOAP มักจะมี XML Message อยู่ในส่วน Body SwA เป็นทางเลือกในการส่ง XML Message ขนาดใหญ่มาก หรือ Message ที่ไม่ได้เป็น XML-based เช่น รูปภาพ Binary ขนาดใหญ่ หรือแฟ้มเสียง MP3 ขนาดใหญ่ เป็นต้น ใช้ MIME multipart/related media type และการอ้างอิงโดยURI ในส่วนต่าง ๆ ของ MIME ถึง Attachment ที่ต้องการ ได้รับการดูแลโดย W3C ถ้าต้องการส่งนอกเหนือจาก text message คือ ส่ง custom data type เช่น array of data structures หรือ binary file, complex data file บาง SOAP toolkit ไม่ support attachment (SAAJ มี ส่วน Microsoft ใช้แบบ based on DIME)

16 SwA Structure Part 1 SOAP Message Part 2 Attachment Image ****
MIME-Version:1.0 Content-Type:Multipart/Related;boundary=MIME_boundary;type=text/xml; start=“ --MIME boundary Part 1 Content-Type:text/xml;charset=utf-8 Content-Transfer-Encoding:8bit Content-ID:<data061400a.xml.apache-soap.joshyjjp”/> <?xml version=“1.0”?> <SOAP:Envelope> <theSignedForm href=“cid:gif061400a.xml.apache-soap.joshyjjp”/> </SOAP:Envelope> SOAP Message **** - ข้อมูลที่เกี่ยวข้องที่อยู่นอก SOAP Envelope เป็น format อะไรก็ได้ - MIME คือ Multipurpose Internet Mail Extension เป็น Internet standard สำหรับส่งข้อมูลไปใน นอกเหนือจาก US-ASCII (ภาษาอังกฤษ) เช่น text ในภาษาอื่น และ binary content ได้แก่ image, sound, movie, computer program หรือ multi-part message MIME เป็น component พื้นฐานใน HTTP ด้วย คือใช้แม้ว่าจะไม่ได้เป็นการส่ง - จากรูป ทั้งหมดนี้เป็น SOAP message package ที่สร้างโดยใช้ MIME Multipart/Related media type แต่ละส่วนของ message จะอยู่ใน MIME boundary คืออยู่ภายใต้ Content-Type header ซึ่งบอกถึงชนิดข้อมูลที่อยู่ในส่วนนี้ ส่วน Content-Transfer-Encoding บอกถึงการ encoding สำหรับส่วนนี้ ส่วน Content-ID จะบอก ID สำหรับใช้อ้างถึงข้อมูลส่วนนี้จากส่วนอื่นใน MIME package - ส่วน root ของ MIME message (สีชมพู) จะห่อ SOAP Envelope และมี Content-Type เป็น text/xml - ส่วนของ SOAP Header และ SOAP Body จะอ้างอิง attachment โดยใช้ attribute href จากรูปแสดงถึงข้อมูล theSignedForm ที่ส่งใน SOAP message จะอ้างถึง URI ของ ส่วน attachment - ถ้าใช้ HTTP binding ส่วน MIME parts ทั้งหมดจะเป็น HTTP body Part 2 Content-Type:image/gif Content-Transfer-Encoding:binary Content-ID:<gif061400a.xml.apache-soap.joshyjjp> …gif image Attachment Image

17 Envelope MUST be associated with SOAP envelope namespace: SOAP serialization namespace: Encoding Style attributes can contain a URI describing how the data should be serialized. SOAP message MUST NOT contain a DTD or Processing Instructions.

18 SOAP Namespaces The SOAP envelope has the namespace identifier “ The SOAP serialization has the namespace identifier " The SOAP mustUnderstand fault namespace identifier " The SOAP upgrade namespace identifier "

19 Header Optional. If present, must immediately follow the SOAP Envelope XML element followed by any header entries. Often contains meta-information regarding the method call. Actor attribute who should process message mustUnderstand attribute how to process(default is “0” if not present) <env:Header> <t:Transaction xmlns:t=“some-URI” env:mustUnderstand=“1”> </t:Transaction> </env:Header>

20 Body Message to exchange. Most often for RPC calls and error reporting. Immediate child element of SOAP Envelope XML element (follows Header, if present). Contains serialized method arguments. Remote method name is used to name the method call’s XML element and must immediately follow the SOAP body opening XML tag.

21 That’s the basics…example
A simple SOAP XML document requesting the price of soap. <env:Envelope> <env:Body> <m:GetPrice> <Item>Lever2000</Item> </m:GetPrice> </env:Body> </env:Envelope>

22 SYNTAX RULES MUST be encoded using XML MUST have a SOAP Envelope
CAN have a SOAP header MUST have a SOAP Body MUST use the SOAP Envelope namespace MUST use the SOAP Encoding namespace Must NOT contain a DTD reference Must NOT contain XML Processing

23 ERRORS: SOAP Fault Element
Error messages from a SOAP application are carried inside a Fault element. Must appear as an element w/in the <Body> element and can only appear once in a SOAP message. Fault sub elements: <faultcode> MUST: --code identifying the error, for use by the software <faultstring> MUST: --error as a string <faultactor> MUST(Apps not acting as ultimate destination of SOAP message) who caused the error <detail> MUST(if contents of Body could not be successfully processed) specific error information Fault Codes: VersionMismatch Invalid namespace for the SOAP Envelope Element MustUnderstand A child element of the Header element, with the mustUnderstand attribute set to “1”, was not understood Client The message was incorrectly formed or contained incorrect information. Server There was a problem with the server so the message could not be process Example: <env:Fault> <faultcode>env:MustUnderstand</faultcode> <faultstring>SOAP Must Understand Error</faultstring> </env:Fault>

24 Easy Transport….HTTP Why HTTP? HTTP has a request/response model allowing SOAP to embed request parameters in HTTP request, and SOAP response parameters into HTTP responses. HTTP apps MUST use the media type “text/xml” when including SOAP entity bodies in HTTP messages. Content-Type: text/xml; charset=“utf-8” SOAP HTTP Header info Basicly…sending XML formatted messages across the network using HTTP.

25 Part of the HTTP payload
Commonly used with HTTP protocol (response/request model) thereby making it very scalable. SOAPAction HTTP Header Field. SOAPAction: “Some-URI” Firewall access? Most firewalls/proxies are opened to port 80 for HTTP giving SOAP a widely used transport protocol across distributed systems. The SOAPAction HTTP header field allows firewalls/proxys to filter SOAP messages if desired. Security? No security has been implemented with SOAP, but consider HTTPS/SSL.

26 Stock Quote example embedded in an HTTP POST Request
POST /StockQuote HTTP/1.1 Host: Content-Type: text/xml; charset=“utf-8” SOAP HTTP Header info Content-Length: nnnn SOAPAction: “Some-URI” SOAP HTTP Header info <env:Envelope xmlns:env=“ <env:Body> <m:GetStockQuote xmlns:m=“Some-URI” env:encodingStyle=“ <symbol>SUNW</symbol> </m:GetStockQuote > </env:Body> </env:Envelope> Sent from the client to the service. First few lines are HTTP headers and SOAP HTTP headers. Followed by the SOAPAction HTTP header and SOAP Envelope XML element. The SOAP Body contains application defined element(s), as defined by the service.

27 Stock Quote Response example embedded in HTTP Response
HTTP/ OK Content-Type: text/xml; charset=“utf-8” SOAP HTTP Header info Content-Length: nnnn <env:Envelope xmlns:env= <env:Body> <m:GetStockQuoteResponse xmlns:m=“Some-URI” env:encodingStyle=“ <price>5.00</price> </m:GetStockQuoteResponse> </env:Body> </env:Envelope>

28 Client/Server… In order for SOAP to work, the client must have code running that is responsible for building the SOAP request. In response, a server must also be responsible for understanding the SOAP request, invoke the specified method, build the response message, and return it to the client. These details are up to you. There already exist SOAP implementations for languages such as Perl and Java.

29 SOAP box Inherently stateless if it uses HTTP for transport.
Doesn’t implement security. However transport with HTTP allows for SSL and HTTPS at the application level. SOAPAction HTTP header field allows your firewall to filter SOAP method invocations/deny SOAP processing altogether. Firewall filters SOAP packets based on the object name, particular method, or a combo of the two.

30 SOAP for RPC Design goals of SOAP: simplicity & extensibility.
RPC with XML allows for uniform representation of remote procedure calls and responses. You’re not limited to HTTP protocol binding…just a natural choice for the request/response model.

31 W3C Working Draft: 1. 3 Examples of SOAP Messages http://www. w3
“The first example shows a simple notification message expressed in SOAP. The message contains the header block "alertcontrol" and the body block "alert" which are both application defined and not defined by SOAP. The header block contains the parameters "priority" and "expires" which may be of use to intermediaries as well as the ultimate destination of the message. The body block contains the actual notification message to be delivered.” Example 0 <env:Envelope xmlns:env=" <env:Header> <n:alertcontrol xmlns:n=" <n:priority>1</n:priority> <n:expires> T14:00:00-05:00</n:expires> </n:alertcontrol> </env:Header> <env:Body> <m:alert xmlns:m=" <m:msg>Pick up Mary at school at 2pm</m:msg> </m:alert> </env:Body> </env:Envelope>

32 Publish | Web Services Description Language: WSDL
Registry Provider Requestor Publish Bind Find UDDI SOAP WSDL Publish | Web Services Description Language: WSDL

33 Web Services Description Language (WSDL)
WSDL ได้รับการออกแบบพัฒนาโดยกลุ่มบริษัทผู้ผลิต ได้แก่ Microsoft, Ariba, IBM และบริษัทผู้ผลิตอื่น ๆ อีกกว่า 20 บริษัทร่วมกันสนับสนุน ข้อกำหนด WSDL ได้รับการส่งต่อไปให้ W3C (World Wide Web Consortium) ดูแลปรับปรุงต่อไป WSDL เป็นเสมือน De Facto Standard ในปัจจุบันสำหรับ การพัฒนา Web Services

34 WSDL Main Components Data types Message Operation Port type Binding
Service ส่วนประกอบตาม WSDL 1.1 ปัจจุบัน (เม.ย 50) WSDL 2.0 ยังเป็น draft ที่ call for review อยู่ ยังไม่เป็น recommendation WSDL 2.0 มีความแตกต่างออกไป เช่น ไม่มี tag message แล้ว, เปลี่ยนชื่อ tag port type เป็น interface, มี inheritance (extend)

35 Data Type and Message ส่วน Data Type เป็นส่วนที่ใช้อธิบาย Data แต่ละ ชนิดที่มีใช้และอ้างอิงถึงใน WSDL การอธิบาย Data Type นั้นจะทำโดยใช้ XML Schema ส่วน Message เป็นการกำหนดชุดของข้อมูลที่ใช้ใน การส่งเป็น Input Parameter และชุดข้อมูลที่ใช้ในการ ตอบกลับของบริการ Web Service

36 Operation and Port Type
ส่วน Operation เป็นการกำหนดชื่อ Operation พร้อม รายละเอียดการส่งข้อมูลนำเข้า Input Message เข้าสู่ การทำงาน และอธิบายผลลัพธ์ที่ได้ว่าเป็น Output Message ใด ส่วน Port type เป็นการจัดกลุ่มของ Operation ที่มีอยู่ ใน Web Service โดยเรียกได้ว่าเป็น Set of Operations ที่ได้รับการจัดรวมกลุ่มเพื่อใช้ในการทำ Binding ต่อไป

37 Binding, Port and Service
ส่วน Binding กำหนดให้กลุ่ม Operation ที่มีอยู่ใน Port Type ใด ๆ สามารถให้บริการผ่านทาง Transport ใดได้บ้าง นั่นหมายถึง Port Type หนึ่ง ๆ สามารถจะให้บริการผ่าน Transport ได้หลายชนิด Transport ใน Web Service มักจะใช้ HTTP ส่วน Port กำหนดตำแหน่ง Address ที่ผู้ใช้บริการจะเข้ามาใช้ บริการจาก Web Service ได้ โดยกำหนด Endpoint Address เข้า กับส่วน Binding ใด ๆ ที่มีอยู่ ส่วน Service เป็นกลุ่มของ Port ที่ระบุ Endpoint Address สำหรับ ให้บริการแล้ว ผู้ใช้บริการ Service Consumer ที่อยู่ภายนอก สามารถอ้างถึง Service นี้ได้

38 Service Group Service Port Port Port Port Type Port Type Port Type Operation1 Operation1 Operation1 Operation2 Operation2 Operation2 Network Address เป็นแบบ point-to-point Binding … Binding … Binding … Network Address Network Address Network Address Services group operations in the same way that objects or classes group methods

39 Find | Universal Description, Discovery, and Integration : UDDI
Service Registry Provider Requestor Publish Bind Find UDDI SOAP WSDL Find | Universal Description, Discovery, and Integration : UDDI Please see, Overview & Example UDDI Slide For Win!!! 

40 Universal Description, Discovery, and Integration (UDDI)
UDDI สนับสนุนโดย OASIS UDDI มีหน้าที่ในการช่วยค้นหา Web Services และให้ข้อมูลต่าง ๆ ที่จำเป็นต่อผู้ร้องขอ UDDI จะจัดเก็บข้อมูลเชื่อมโยงไปยัง WSDL ของบริการ Web Services ใหม่ ๆ ที่ได้รับการ Register เข้ามา รวมทั้งข้อมูลอื่น ๆ ที่อธิบาย Web Services เพื่อให้บริการค้นหาได้ UDDI ก็คือ Registry ของข้อมูลคำอธิบายเกี่ยวกับ Web Services (Web Services Metadata) นั่นเอง

41 Public UDDI Public UDDI คือกลุ่มผู้ทำหน้าที่ให้บริการการสืบค้นบริการ Web Services ในเครือข่ายอินเทอร์เน็ต กลุ่มผู้ดูแล Public UDDI IBM Microsoft SAP HP NTT Etc ข้อมูลที่ทำการ Register จะกระจายไปทั่วถึงกันในกลุ่มผู้ดูแล Public UDDI โดยอัตโนมัติ ผู้ใช้ Public UDDI สามารถส่ง SOAP Request ไปร้องขอการค้นหา Web Services ที่ตนต้องการได้จากบริษัทใด ๆ ในกลุ่มผู้ดูแล Public UDDI ปิดตัวลงเพราะการใช้งาน service ในธุรกิจมักเรียก service ที่รู้จักอยู่แล้วหรือเชื่อใจ และพวก service level agreement ต่าง ๆ ยังไม่ mature พอที่จะทำให้ลักษณะของ dynamic discovery มัน work อีกทั้งยังขาดการดูแลจัดการทำให้ข้อมูลที่ register เป็นข้อมูลของเล่นหรือไม่มีจริง ทำให้ลดคุณค่าของ UDDI ไป แต่องค์กรสามารถติดตั้ง private uddi ได้ซึ่งจะดูแลจัดการได้ดีกว่า ต้นปี 2006 IBM, Microsoft, SAP ประกาศปิดไป

42 UDDI Operators Microsoft UDDI IBM Server UDDI Server HP SAP UDDI UDDI
Others UDDI Server

43 UDDI Information Model
<businessEntity> name = xmethods categories = contact = xmethods.net <publisherAssertion> name = Australian Import relationship = business partner <businessService> (1..n) name = CurrencyExchangeServiceca tegories = <operationalInfo> created = 03/01/04 modified = 05/01/04 <bindingTemplate> (1..n) access point = <tModel> name = CurrencyExchangeModel URL =

44 Overview Web Service by PHP
Informatics BUU. Suranglutch Chaokoksoohng

45 Web Service by PHP Must to have PHP PHP NUSOAP Apache server
WSDL Editor

46 PHP NUSOAP สนับสนุน SOAP สามารถ Generate WSDL ได้
ดาวน์โหลดได้ที่

47 Server, Test Program For NUSOAP Service
อาจกำหนด $debug = 1; เพื่อ Debug error <?php require_once('nusoap.php'); // Pull in the NuSOAP code $server = new soap_server; // Create the server instance $server->register('hello'); // Register the method to expose // Define the method as a PHP function function hello($name) { return 'Hello, ' . $name; } // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ‘ '; $server->service($HTTP_RAW_POST_DATA); ?>

48 Client, Test Program For NUSOAP Service
<?php // Pull in the NuSOAP code require_once('nusoap.php'); // Create the client instance $client = new soapclient(' // Call the SOAP method $result = $client->call('hello', array('name' => 'Scott')); // Display the result print_r($result); ?> อาจจะกำหนดบรรทัดต่อไปนี้เพื่อทำ Debugging echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>'; echo '<h2>Response</h2>'; echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

49 Debugging on Client Side
if ($client->fault) { echo '<p><b>Fault: '; print_r($result); echo '</b></p>'; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo '<p><b>Error: ' . $err . '</b></p>'; // Display the result }

50 SOAP Request $result = $client->call('hello', array('name' => 'Scott')); POST /phphack/helloworld2.php HTTP/1.0 Host: localhost User-Agent: NuSOAP/0.6.8 (1.81) Content-Type: text/xml; charset=ISO SOAPAction: "" Content-Length: 538 <?xml version="1.0" encoding="ISO "?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=" xmlns:SOAP-ENV=" xmlns:xsd=" xmlns:xsi=" xmlns:SOAP-ENC=" xmlns:si=" <SOAP-ENV:Body> <ns1:hello xmlns:ns1=" <name xsi:type="xsd:string">Scott</name> </ns1:hello> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

51 SOAP Response $server->service($HTTP_RAW_POST_DATA);
HTTP/ OK Server: Microsoft-IIS/5.0 Date: Wed, 03 Nov :32:34 GMT X-Powered-By: ASP.NET X-Powered-By: PHP/4.3.4 Server: NuSOAP Server v0.6.8 X-SOAP-Server: NuSOAP/0.6.8 (1.81) Content-Type: text/xml; charset=ISO Content-Length: 556 <?xml version="1.0" encoding="ISO "?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=" xmlns:SOAP-ENV=" xmlns:xsd=" xmlns:xsi=" xmlns:SOAP-ENC=" xmlns:si=" <SOAP-ENV:Body> <ns1:helloResponse xmlns:ns1=" <return xsi:type="xsd:string">Hello, Scott</return> </helloResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

52 Web Service by PHP กำหนดตำแหน่งเพื่อเรียกใช้ NUSOAP ใน PHP โดยเลือกกำหนดอย่างใดอย่างหนึ่ง จาก include (‘nusoap.php’); include_once(‘nusoap.php’); require (‘nusoap.php’); require_once (‘nusoap.php’);

53 Making Service Part สร้าง Server Instant $service = new soap_server;
กำหนด WSDL สำหรับการเรียกใช้เซอร์วิส ซึ่ง NUSOAP จะทำการ Generate WSDL ให้เอง $service->configureWSDL(“ชื่อWSDL”, “urnของWSDL”); ลงทะเบียนเซอร์วิสกับ SOAP Server ทำการสร้างฟังก์ชั่นเพื่อให้บริการ กำหนดการส่งข้อมูลกลับไปยัง Client $service->service(HTTP_RAW_POST_DATA); NUSOAP API ดูได้ที่ Tuturial:

54 Example: Web Service in restaurant
Server รับประเภทอาหาร ส่งรายการอาหาร HTML Form User เลือก ประเภทอาหาร Client รับประเภทอาหาร 2 Call getFoodName() 1 แสดงรายการอาหาร CateringService.php CateringServiceClient.php

55 Catering Service Coding
<?php // Pull in the NuSOAP code require('../lib/nusoap.php'); // Create the server instance $server = new soap_server(); // Initialize WSDL support $server->configureWSDL('CateringService', 'urn:CateringServiceWsdl'); // กำหนด Type ต่าง ๆ ใน WSDL ….. $server-> register('getFoodName', // method name array('typefood' => 'xsd:string'), // input parameters array('food' => 'tns:foodnameArray'), // output parameters กำหนดใน SOAP 'urn:CateringServiceWsdl', // namespace 'urn:CateringServiceWsdl#getFoodName', // SOAPAction 'rpc', // style 'encoded', // use 'cateringbufService') ;

56 Catering Service Coding (con.2)
function getfoodName($typefood){ $food=array(); // อาจจะมีการติดต่อฐานข้อมูลก็ได้ return $food; } // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; // ส่งผลลัพธ์กลับไปยัง Client program $server->service($HTTP_RAW_POST_DATA); php?>

57 Catering Service Coding (con.3)
การกำหนด ComplexType สำหรับ Output message Output message ของโปรแกรม Server Output message ของ SOAP $server->wsdl->addComplexType( 'fooddetail', 'complexType', 'struct', 'all', '', array( 'foodname' => array('name'=>'foodname','type'=>'xsd:string'), ) );

58 Catering Service Coding (con.4)
การกำหนด ComplexType สำหรับ Output message $server->wsdl->addComplexType( 'foodnameArray', //name of output variable 'complexType', 'array', ' ', 'SOAP-ENC:Array', array(), array( array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:fooddetail[]') // Array foodname µÒÁ·Õè»ÃСÒÈäÇé¢éÒ§º¹ ), 'tns:fooddetail' );

59 ComplexType ตัวอย่าง ComplexType ที่มีหลาย Element
$server->wsdl->addComplexType( 'Person', 'complexType', 'struct', 'all', '', array( 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), 'age' => array('name' => 'age', 'type' => 'xsd:int'), 'gender' => array('name' => 'gender', 'type' => 'xsd:string') ) ); Output message function listAllPersons(Input_Message) { $pvar[0] = array (‘firstname’ => ‘Nid’, ‘age’=> 23, ‘gender’ =>’Female’); $pvar[1] = array (‘firstname’ => ‘Dang’, ‘age’=> 22, ‘gender’ =>’Female’); return $pvar; } จะ Bind ไปยัง Output message

60 Catering Service Client Coding
<?php if(!isset($_GET["typefood"])){ //กำหนด ฟอร์มเพื่อให้ User ทำการเลือกประเภทอาหาร // ส่วนนี้อาจจะไม่มีก็ได้หาก Client ไม่จำเป็นต้องทำการติดต่อกับ User }else{ //ส่วนนี้เป็นการเรียกไปยัง Service require('../lib/nusoap.php'); $s = new soapclient(' $result = $s ->call('getFoodName',$_GET["typefood"]); //แสดงผลลัพธ์ } ?>

61

62 Calling Service by WSDL
//Set proxy $client = new soapclient('GoogleSearch.wsdl', true, $proxyhost, $proxyport, $proxyusername, $proxypassword); $err = $client->getError(); if ($err) { echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; } $client->soap_defencoding = 'UTF-8'; // Set $params …. $result = $client->call('doGoogleSearch', $params);

63 References…and other great links!
how soap works Index for articles and various SOAP related resources. Older soap specification SOAP: Simple Object Access Protocol soap primer soap school Topics in Software Engineering, Web Infrastructure, Services, and Applications, understanding soap W3C SOAP W3c SOAP notes


ดาวน์โหลด ppt Suranglutch Chaokoksoohng

งานนำเสนอที่คล้ายกัน


Ads by Google