ในบทความนี้ผมจะยกตัวอย่างการสร้างเอกสาร WSDL อย่างง่าย ๆ ก่อนเลยนะครับ ที่บอกว่าอย่างง่ายก็เพราะว่าผมจะใช้ Data Type ที่มีอยู่เท่านั้น จะไม่สร้างพวก ComplexType หรือ SimpleType ขึ้นมาใช้เองครับ เพื่อให้ผู้อ่านได้เข้าใจในเบื้องต้นกันก่อนครับ เราไปดูกันเลย
เริ่มต้นด้วยการกำหนด namespace ต่าง ๆ ให้กับเอกสาร WSDL ก่อนครับ ซึ่งสิ่งที่ขาดไม่ได้เลยก็คือในบรรทัดแรกครับ เราจะต้องทำการประกาศแท็ก xml ไว้ด้วย และสามารถกำหนด encoding ให้กับเอกสารได้ เราไปดูกันเลยครับว่าโครงสร้างเอกสารนี้เป็นอย่างไร
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/PlusService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="PlusService" targetNamespace="http://www.example.org/PlusService/"> </wsdl:definitions>
เมื่อเราได้โครงสร้างของเอกสาร WSDL นี้แล้ว ขั้นต่อไปเราจะต้องสร้าง message ขึ้นมาเพื่อใช้เป็น input หรือ output ให้กับ operation ต่าง ๆ (ในตัวอย่างนี้ผมจะสร้าง Service ที่มี operation ตัวเดียวนั่นก็คือ Plus ครับ) เราไปดูกันครับว่า Service นี้มี message อะไรบ้าง
<wsdl:message name="PlusRequest"> <wsdl:part name="num1" type="xsd:integer" /> <wsdl:part name="num2" type="xsd:integer" /> </wsdl:message> <wsdl:message name="PlusResponse"> <wsdl:part name="result" type="xsd:integer" /> </wsdl:message>
เมื่อเราได้กำหนด message ให้กับ Service เรียบร้อยแล้ว จากนั้นเราก็ต้อสร้าง portType ที่เป็นตัวกำหนดว่า Service นี้มี operation อะไรบ้าง และ input, output จะใช้ message ตัวไหน (ที่เราได้สร้างไว้ด้านบนครับ) เราลองไปดูกันเลย
<wsdl:portType name="CalculatePort"> <wsdl:operation name="Plus"> <wsdl:input message="tns:PlusRequest" /> <wsdl:output message="tns:PlusResponse" /> </wsdl:operation> </wsdl:portType>
หลังจากเราสร้าง portType แล้ว ขั้นตอนต่อไปเราก็จะต้องสร้าง Binding เพื่อเป็นตัวกลางที่ใช้เชื่อม Service กับ PortType เข้าหากันครับ ซึ่งถ้าเราทำการกำหนด operation ต่าง ๆ ไว้ใน portType กี่ตัว เราก็จะต้องกำหนด operation ในส่วนของ Binding ให้ครบด้วย (ถ้าไม่ครบมันจะไม่เกิด Error ครับ แต่ service จาก client จะไม่สามารถเข้าถึง operation นั้น ๆ ได้) ซึ่งในส่วนนี้จะเป็นตัวกำหนดด้วยว่า input และ output เราจะส่งและรับในรูปแบบใด จะเป็น rpc/document และเราจะทำการ encode มันด้วยวิธีไหน ดังตัวอย่างครับ
<wsdl:binding name="CalculateSOAP" type="tns:CalculatePort"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="Plus"> <soap:operation soapAction="http://www.example.com/Calculate#Plus" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding>
หลังจากที่เรากำหนด binding แล้ว ส่วนสุดท้ายก็คือส่วนที่กำหนด service ซึ่งจะใช้ระบุว่า Service นี้อยู่ที่ไหน (Directory) ในที่นี้ผมได้ระบุว่า Service นี้อยู่ที่ http://www.basic-skill.com/Calculate.php ครับ
<wsdl:service name="CalculateService"> <wsdl:port name="CalculatePort" binding="tns:CalculateSOAP"> <soap:address location="http://www.basic-skill.com/Calculate.php" /> </wsdl:port> </wsdl:service>
Login
Search